image matching with template in database
Mostra commenti meno recenti
hi,
how can i compare an image with a template in a database? the object of interest in this case is a marker consisted of dots. first, the marker template is save in the database, then another image with the marker is placed for comparison. i have to make sure that it can still recognise the marker regardless of the scale, orientation, illumination, etc. the method that i have in mind is SIFT but is that any other ways?
thanks for replying
Risposte (1)
Image Analyst
il 23 Giu 2013
You could use normalized cross correlation. Here's a demo:
% Demo to use normxcorr2 to find a template (a white onion)
% in a larger image (of a 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 long g;
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]);
% Let's get our template by extracting a small portion of the original image.
templateWidth = 71
templateHeight = 49
smallSubImage = imcrop(rgbImage, [192, 82, templateWidth, templateHeight]);
subplot(2, 2, 2);
imshow(smallSubImage, []);
axis on;
title('Template Image to Search For', 'FontSize', fontSize);
% Ask user which channel to search for a match.
% channelToCorrelate = menu('Correlate which color channel?', 'Red', 'Green', 'Blue');
% It actually finds the same location no matter what channel you pick,
% for this image anyway, so let's just go with red (channel #1).
channelToCorrelate = 1;
correlationOutput = normxcorr2(smallSubImage(:,:,1), rgbImage(:,:, channelToCorrelate));
subplot(2, 2, 3);
imshow(correlationOutput, []);
axis on;
title('Normalized Cross Correlation Output', 'FontSize', fontSize);
% Find out where the normalized cross correlation image is brightest.
[maxCorrValue, maxIndex] = max(abs(correlationOutput(:)));
[yPeak, xPeak] = ind2sub(size(correlationOutput),maxIndex(1))
% Because cross correlation increases the size of the image,
% we need to shift back to find out where it would be in the original image.
corr_offset = [(xPeak-size(smallSubImage,2)) (yPeak-size(smallSubImage,1))]
% Plot it over the original image.
subplot(2, 2, 4); % Re-display image in lower right.
imshow(rgbImage);
axis on; % Show tick marks giving pixels
hold on; % Don't allow rectangle to blow away image.
% Calculate the rectangle for the template box. Rect = [xLeft, yTop, widthInColumns, heightInRows]
boxRect = [corr_offset(1) corr_offset(2) templateWidth, templateHeight]
% Plot the box over the image.
rectangle('position', boxRect, 'edgecolor', 'g', 'linewidth',2);
% Give a caption above the image.
title('Template Image Found in Original Image', 'FontSize', fontSize);
uiwait(helpdlg('Done with demo!'));

5 Commenti
Ayush Sharma
il 13 Lug 2017
Modificato: Ayush Sharma
il 13 Lug 2017
Respected, I have edited your code to detect a pre-defined image from the user and fortunately, it is running well. But I want my system to show no box in case the matched image is not present. How can I edit the code if I need to avoid displaying the green rectangular box in case there is such match found.
Kindly help me in this regard.
Thank you
Image Analyst
il 13 Lug 2017
Just return/exit if maxCorrValue is not high enough:
if maxCorrValue < 0.8 % Whatever....
return;
end
Ayush Sharma
il 14 Lug 2017
Modificato: Ayush Sharma
il 14 Lug 2017
Respected, After trying what you suggested, the image is successfully omitting the green box.
But, when i try to implement it on a frame taken at a real time from the webcam, the output is not correct. I selected the template as my lunch box, as shown. and the webcam takes the snapshot as given.


But the code fails to recognize it correctly. It shows a mxCorrValue of ~0.55 and that too in a wrong position. Can you guide me where is the problem. Is the any error in selecting the template? Do I need to select the size of the template image as mentioned in your code? (ie: Width = 71,Height = 49) or is there any other issue!
Kindly revert on this. Regards
Ayush Sharma
il 14 Lug 2017
I modified the code and now the code is continuously taking each frame, detects and gives the out in frame by frame as a video.
The code is now somewhat detecting the template with less accuracy. I need to bring the template in the center of the camera and close to it and as i shifts the camera slightly backwards and at an angle, the detections stops detecting with accuracy.
Why soo much of error? How can I rectify this? I want this code to detect road sign on a moving car.
Shall wait for your revert.
Best Regards
Image Analyst
il 14 Lug 2017
or feautre based object recognition: https://www.mathworks.com/products/computer-vision/features.html#object-detection-and-recognition
Categorie
Scopri di più su Computer Vision Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!