How to obtain the x and y coordinate of images detected using cross correlation?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I have written a code that performs the cross correlation of a template over multiple images in matlab. This code is meant to track the movement of a cell across multiple frames, the code is somewhat interactive, first the user selects the image template from the first frame than he/she runs the program to perform the cross correlation, I have define a region of interest where the code has to find this template image. My question now is how do I obtain the x and y coordinates of the position of the cells after the cross correlation has detected and located it across all the frames. Below is my code I have tried making an array of the xpeak and ypeak,xoffset and yoffst but my results are not right
clc
format long
fontSize = 10;
file_name = 'stack0013.tif'; % TIFF Stack (not just a single TIFF image!!!)
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'});
for i1 = 1 : numImg % Read Each Frame
fprintf('Now correlating frame #%d with frame #%d\n',1,i1);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
rect_A= [226.5 13.5 72 200];
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 cropped bead
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);
figure, surf(correlationOutput),shading flat;
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))]
xpeak;
ypeak;
a=rect_A(3)*xpeak/x;
b=rect_A(4)*ypeak/y;
x1=rect_A(3)-a-rect_A(1);
y1=rect_A(4)-b-rect_A(2);
if i1==1
m = x1; n = y1;
xoffset = x1-m
yoffset = y1-n
end
xoffset(i1)= x1-m
yoffset(i1)= y1-n
xpeak(i1)=xpeak
ypeak(i1)=ypeak
end
6 comentarios
huylaidinh huy
el 24 de Mzo. de 2021
means the sample code above is still missing, can you write enough to help me
Respuestas (1)
Nick
el 20 de Nov. de 2018
normxcorr2 returns the x/y coordinates from the bottom right corner of your template so if you would want to make an array of your template in the coordinates of your current image you will have to subtract the size(then +1 due to matlabs indexing) to get your top left corner, after that it is merely selecting those coordinates:
% cutting some parts of the example code as its not realy relevant to the example
correlationOutput = normxcorr2(sub_rgbImage(:,:,1), sub_A(:,:,1));
x=size(correlationOutput, 2);
y=size(correlationOutput, 1);
%Offset between the images found by correlation
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));
[ypeak,xpeak] = ind2sub(size(correlationOutput),maxIndex(1));
yTopLeft = ypeak-size(sub_rgbImage,1)+1;
xTopLeft = xpeak-size(sub_rgbImage,2)+1; % +1 as matlabs indexing starts at 1
% draw a rectangle over original image to correspond with template (also shows how to overlay original image)
hFig = figure;
hAx = axes('Parent',hFig);
imshow(sub_rgbImage, 'Parent', hAx);
hold(hAx, 'on');
% get a mask in same pixels as image from template
mask = false(size(sub_rgbImage(:,:,1)));
mask(yTopLeft:ypeak,xTopLeft:xpeak) = true;
visboundries(mask);
hold(hAx,'off');
end
It was not really clear to me if you wanted an array of the correlation results, the original image or just the coordinates to draw a rectangle. But with this example you should be able to get the result yourself. You can find a full working example for a similar problem here: https://nl.mathworks.com/matlabcentral/answers/428546-cropped-image-vs-original-image#answer_345695?s_tid=prof_contriblnk
0 comentarios
Ver también
Categorías
Más información sobre Read, Write, and Modify Image en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!