Matching a Feature and a Character using Sliding Window
5 views (last 30 days)
Show older comments
I'm having a problem where i want to compare two image matrices, say A and B, wherein B is a feature of A. I have tried with sliding window comparison where a 10x10 window(same size as the image matrix B) need to be traversed through 40x40 matrix( same size a A) pixel by pixel. i'm making use of isequal(A,B) for comparing the two images. Pls help me out sliding window concept for comparing the two matrices. The code i tried is given below:
A = imread('1.jpg'); imageWidth = size(A, 40); imageHeight = size(A, 40);
windowWidth = 20; windowHeight = 20;
B=imread('1f.jpg');
for j = 1:imageHeight - windowHeight + 1 for i = 1:imageWidth - windowWidth + 1 window = image(j:j + windowHeight - 1, i:i + windowWidth - 1); C=isequal(A,B); end
end
P.S. "1.jpg" is the Character and "1f.jpg" is the feature i'm trying to match for.
-Thank You
0 Comments
Answers (1)
Image Analyst
on 5 May 2013
Sounds like normalized cross correlation will work for you. It is done by the function normxcorr2() in the Image Processing Toolbox. Here is my demo for that:
% 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!'));
2 Comments
Image Analyst
on 7 May 2013
You have to images, A and B. A is the full size image, and B is the smaller template image that is, or might be, contained in A somewhere. Just like what I did. You have to adapt my code to look for your character images (not the onion), and do it for every character you want to try to find. Surely you must know that I didn't provide you with a 100% complete turnkey solution to your problem, and that some modification of my demo code would be involved to apply it to your situation.
See Also
Categories
Find more on Explore and Edit Images with Image Viewer App in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!