2011-02-25 304 views
4

我使用隧道顯微鏡拍攝圖像。但是,範圍在連續的圖像之間漂移。我正在嘗試使用MatLab來計算圖像之間的偏移量。下面的代碼計算小圖像(例如64x64像素)的秒數,但需要2個小時才能處理我正在處理的512x512像素圖像。你對加速這段代碼有什麼建議嗎?或者你知道更好的方式來跟蹤MatLab中的圖像嗎?謝謝你的幫助!使用MATLAB來計算連續圖像之間的偏移量

%Test templates 
template = .5*ones(32); 
template(25:32,:) = 0; 
template(:,25:64) = 0; 
data_A = template; 
close all 
imshow(data_A); 
template(9:32,41:64) = .5; 
template(:,1:24) = 0; 
data_B = template; 
figure, imshow(data_B); 

tic 

[m n] = size(data_B); 
z = []; 

% Loop over all possible displacements 
for x = -n:n 

for y = -m:m 

    paddata_B = data_B; 
    ax = abs(x); 
    zerocols = zeros(m,ax); 

    if x > 0 
     paddata_B(:,1:ax) = []; 
     paddata_B = [paddata_B zerocols]; 

    else 
     paddata_B(:,(n-ax+1):n) = []; 
     paddata_B = [zerocols paddata_B]; 

    end 

    ay = abs(y); 
    zerorows = zeros(ay,n); 


    if y < 0 
     paddata_B(1:ay,:) = []; 
     paddata_B = vertcat(paddata_B, zerorows); 

    else 
     paddata_B((m-ay+1):m,:) = []; 
     paddata_B = vertcat(zerorows, paddata_B); 

    end 

% Full matrix sum after array multiplication 
C = paddata_B.*data_A;   
matsum = sum(sum(C)); 

% Populate array of matrix sums for each displacement  
z(x+n+1, y+m+1) = matsum; 

end 
end 

toc 

% Plot matrix sums 
figure, surf(z), shading flat 

% Find maximum value of z matrix 
[max_z, imax] = max(abs(z(:))); 
[xpeak, ypeak] = ind2sub(size(z),imax(1)) 

% Calculate displacement in pixels 
corr_offset = [(xpeak-n-1) (ypeak-m-1)]; 
xoffset = corr_offset(1) 
yoffset = corr_offset(2) 
+0

你有沒有考慮[康萊特追蹤器](http://www.ces.clemson.edu/~stb/klt/)? – rwong 2011-02-27 00:53:31

回答

4

你正在計算的是兩個圖像的cross-correlation。您可以一次計算所有偏移的互相關using Discrete Fourier Transforms(DFT或FFT)。因此,嘗試像

z = ifft2(fft2(dataA) .* fft2(dataB).'); 

如果你墊在傅立葉域零,你甚至可以用這種數學得到補償的像素的分數,並應用像素的分數偏移圖像。

3

這種問題的一個典型方法是使用這樣一個事實,即它可以快速爲小圖像工作,以使您受益。當你有大的圖像時,將它們抽成小圖像。快速註冊小圖像,並將計算出的偏移量用作下一次迭代的初始值。在下一次迭代中,您不會多次抽取圖像,但是您首先對偏移進行了良好的初始估計,以便您可以將搜索解決方案限制在最初估計附近的小鄰域。

儘管沒有用隧道顯微鏡記錄,但可能有一些幫助的評論文章是:Pluim,Maintz和Viergever在IEEE Transactions on Medical上發表的「基於互信息的醫學圖像註冊:調查」成像,卷。 22,No.8,p。 986.

0

下面的函數是我試圖手動計算兩個圖像的互相關。有些事情不太正確。如果我有時間的話,這週末再看一遍。您可以調用該函數的東西,如:

 
>> oldImage = rand(64); 
>> newImage = circshift(oldImage, floor(64/2)*[1 1]); 
>> offset = detectOffset(oldImage, newImage, 10) 

offset = 

    32 -1 
function offset = detectOffset(oldImage, newImage, margin) 

    if size(oldImage) ~= size(newImage) 
     offset = []; 
     error('Test images must be the same size.'); 
    end 

    [imageHeight, imageWidth] = size(oldImage); 

    corr = zeros(2 * imageHeight - 1, 2 * imageWidth - 1); 

    for yIndex = [1:2*imageHeight-1; ... 
        imageHeight:-1:1 ones(1, imageHeight-1); ... 
        imageHeight*ones(1, imageHeight) imageHeight-1:-1:1]; 
     oldImage = circshift(oldImage, [1 0]); 
     for xIndex = [1:2*imageWidth-1; ... 
         imageWidth:-1:1 ones(1, imageWidth-1); ... 
         imageWidth*ones(1, imageWidth) imageWidth-1:-1:1]; 
      oldImage = circshift(oldImage, [0 1]); 
      numPoint = abs(yIndex(3) - yIndex(2) + 1) * abs(xIndex(3) - xIndex(2) + 1); 
      corr(yIndex(1),xIndex(1)) = sum(sum(oldImage(yIndex(2):yIndex(3),xIndex(2):xIndex(3)) .* newImage(yIndex(2):yIndex(3),xIndex(2):xIndex(3)))) * imageHeight * imageWidth/numPoint; 
     end 
    end 

    [value, yOffset] = max(corr(margin+1:end-margin,margin+1:end-margin)); 
    [dummy, xOffset] = max(value); 
    offset = [yOffset(xOffset)+margin-imageHeight xOffset+margin-imageWidth]; 
1

下面的鏈接將幫助您找到2個圖像和正確/恢復失真之間的轉換(在你的情況下,圖像的偏移量)

http://in.mathworks.com/help/vision/ref/estimategeometrictransform.html

index_pairs = matchFeatures(featuresOriginal,featuresDistorted, 'unique', true); 
matchedPtsOriginal = validPtsOriginal(index_pairs(:,1)); 
matchedPtsDistorted = validPtsDistorted(index_pairs(:,2)); 
[tform,inlierPtsDistorted,inlierPtsOriginal] = estimateGeometricTransform(matchedPtsDistorted,matchedPtsOriginal,'similarity'); 
figure; showMatchedFeatures(original,distorted,inlierPtsOriginal,inlierPtsDistorted); 

inlierPtsDistored,inlierPtsOriginal具有稱爲位置的屬性。 這些只不過是一個圖像在另一個圖像上的匹配位置。我認爲從這一點來計算偏移量非常容易。