Filter löschen
Filter löschen

How do I detect the x,y co-ordinates of the location of the onion after it has been detected by the cross correlation program?

1 Ansicht (letzte 30 Tage)
I am using a cross correlation program to detect the location of a particle in many frames and I used the demo code below to develop my program, how do I get the x, y coordinates of the particle in each frame after it has been detected by the cross correlation program
% Demo to use normxcorr2 to find a template (a white onion)
% in a larger image (pile of vegetables)
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
smallSubImage = imcrop(rgbImage, [192 82 60 52]);
subplot(2, 2, 2);
imshow(smallSubImage, []);
axis on;
title('Template Image to Search For', 'FontSize', fontSize);
% Search the red channel for a match.
correlationOutput = normxcorr2(smallSubImage(:,:,1), rgbImage(:,:,1));
subplot(2, 2, 3);
imshow(correlationOutput, []);
title('Normalized Cross Correlation Output', 'FontSize', fontSize);
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));
[ypeak, xpeak] = ind2sub(size(correlationOutput),maxIndex(1));
corr_offset = [(xpeak-size(smallSubImage,2)) (ypeak-size(smallSubImage,1))];
subplot(2, 2, 4);
imshow(rgbImage);
hold on;
rectangle('position',[corr_offset(1) corr_offset(2) 50 50],...
'edgecolor','g','linewidth',2);
title('Template Image Found in Original Image', 'FontSize', fontSize);

Akzeptierte Antwort

Image Analyst
Image Analyst am 3 Apr. 2013
Looks like my code you got from this question. I noticed a small error when it's drawing the rectangle. It should read
rectangle('position',[corr_offset(1) corr_offset(2) 60 52],...
'edgecolor','g','linewidth',2);
so that the rectangle has the same width and height as the template.
Anyway, look at the 'position' property I sent into the rectangle() function. That defines the [xLeft, yTop, xWidth, yHeight] of where the template subimage occurs in the original image, as you can see because it places the rectangle in the proper location over the original image.
  18 Kommentare
EngStudent
EngStudent am 11 Apr. 2013
How do I make the cross correlation program to detect really small displacement across many frames
EngStudent
EngStudent am 18 Apr. 2013
What does 1 and 2 mean in size(sub_rgbImage,2) and size(sub_rgbImage,1) and also what does the rect_offset mean?
maxCorrValue, maxIndex] = max(abs(correlationOutput(:)))
[ypeak, xpeak] = ind2sub(size(correlationOutput),maxIndex(1))
corr_offset = [(xpeak-size(sub_rgbImage,2))
(ypeak-size(sub_rgbImage,1))]
%relative offset between position of subimages
rect_offset = [(rect_A(1)- rect_rgbImage(1))
(rect_A(2)- rect_rgbImage(2))]
%total offset
offset = corr_offset + rect_offset
xoffset= offset(1)
yoffset= offset(2)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by