使用MATLAB计算连续图像之间的偏移量

4

我正在使用隧道显微镜拍摄图像。然而,仪器在连续的图像之间会漂移。我正在尝试使用MatLab计算图像之间的偏移量。下面的代码可以计算小图像(例如64x64像素)的秒数,但需要超过2小时才能处理我正在处理的512x512像素图像。您有任何加速此代码的建议吗?或者您是否知道在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)  

你考虑过KLT跟踪器吗? - rwong
4个回答

4
你正在计算的是两个图像的交叉相关性。你可以使用离散傅里叶变换(DFT或FFT)同时计算所有偏移量的交叉相关性。因此,可以尝试以下操作:
z = ifft2( fft2(dataA) .* fft2(dataB).' );

如果在傅里叶域中使用零填充,甚至可以使用这种数学方法来获取像素分数的偏移量,并将像素分数的偏移量应用于图像。

3
一种解决这类问题的典型方法是利用其在小图像上快速运行的事实。当您有大图像时,将其降采样以生成小图像。快速注册小图像并使用计算出的偏移量作为下一次迭代的初始值。在下一次迭代中,您不会对图像进行太多降采样,但是由于有了良好的偏移估计初始值,因此您可以将解决方案的搜索限制在初始估计附近的一个小邻域内。
虽然这篇文章不是针对隧道显微镜编写的,但可能会对您有所帮助:"基于互信息的医学图像配准:综述",作者为Pluim、Maintz和Viergever,发表在《IEEE医学成像交易》第22卷第8期,第986页。

1
以下链接可以帮助您找到两张图像之间的转换并纠正/恢复畸变(在您的情况下,是具有偏移的图像)。

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);

在lierPtsDistored和inlierPtsOriginal中,有名为locations的属性。 这些属性就是一个图像在另一个图像上的匹配位置。我认为从这一点很容易计算偏移量。

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];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接