我写了一个代码,在matlab中对多个图像执行模板的互相关。这个代码是为了跟踪一个单元格在多个帧中的移动,代码有点交互,首先用户从第一个选择图像模板框架比他/她运行程序来执行互相关,我已经定义了一个感兴趣的区域,代码必须在其中找到这个模板。我的问题是如何在互相关检测到并在所有帧中定位后获取单元格位置的 x 和 y 坐标
format long
fontSize = 10;
file_name = 'stack0001.tif'; %TIFF Stack
image_info = imfinfo(file_name);
numImg = length(image_info) %Number of images in stack
rgbImage = imread(file_name,'Index', 1);
[sub_rgbImage,rect_rgbImage] = imcrop(rgbImage);
figure,
imshow(sub_rgbImage)
title({'Template Image ' ;'to Search For'});
h=figure;
for i1=1:numImg %Read Each Frame
fprintf('Now correlating frame #%d with frame #%d\n',1,i1);
rect_A= [247.5 134.5 35 81]; % region to look for object
A=imread(file_name,'Index', i1);%read the following image from image loop (in tiff stack)
sub_A = imcrop(A,rect_A); % Region of Interest
figure,
imshow(sub_A); % Show region of Interest
axis on;
% Search the red channel for a match.
correlationOutput = normxcorr2(sub_rgbImage(:,:,1), sub_A(:,:,1));
x=size(correlationOutput, 2);
y=size(correlationOutput, 1);
h=figure;
set(h,'visible','off');
figure, surf(correlationOutput),shading flat;
h=figure;
set(h,'visible','off');
figure('Position', [300 300 300 300]);
imshow(correlationOutput, []);
sprintf('Normalized Cross Correlation Output of frame #%d and #%d\n',i1,i1+1);
title('Cross Correlation');
%Offset between the images found by correlation
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)))
[ypeak,xpeak] = ind2sub(size(correlationOutput),maxIndex(1))
corr_offset = [(ypeak-size(sub_rgbImage,1))
(xpeak-size(sub_rgbImage,2))] ;
%relative offset between position of subimages
rect_offset = [(rect_A(1)- rect_rgbImage(1))
(rect_A(2)- rect_rgbImage(2))]
%total offset
offset = rect_offset+corr_offset;
xoffset= offset(1)
yoffset= offset(2)
end