finding a part of an image using convolution

How would I implement convolution using a 18x10 matrix to find something in a 570x760 image matrix?

Answers (3)

conv2() ?

4 Comments

i tried that but it gave me some weird matrix with values in the thousands...
Convolution is not like pattern matching. It won't necessarily give you the highest signal at the location where your template lies in the larger image, like the way I recommended in my answer.
oh....the way my Professor and Grad Student adviser explained it made it seem that by using convolution, you could find the region where values are highest...meaning that the template matches most with that specific position.
That is not true, in general. It may be true in some cases (like autocorrelation), but NOT true in general. However, this is a common misconception, probably because many people see convolution used for autocorrelation. Your Professor and Grad Student adviser should know that the highest point may not be where your target is. If they don't, then show them this example to prove my point:
signal = [ 9 9 9 9 0 0 0 3 6 3 0 0 0 8 8 8 8]
template = [ 3 6 3];
output = conv(signal, template, 'same')
% Note, the highest output does NOT occur at element 9
% where the template is overlapped with the part of the
% signal that contains the template.
signal =
9 9 9 9 0 0 0 3 6 3 0 0 0 8 8 8 8
output =
81 108 108 81 27 0 9 36 54 36 9 0 24 72 96 96 72
Like I said before in my comment above, and in my answer, you should use normxcorr2(), not conv2 like you, your Prof, or your adviser suggested if you want to find the pattern.

Sign in to comment.

I think you probably want normalized cross correlation, performed by normxcorr2(), rather than convolution. See this demo:
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('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);
Or see this thread for another example:

11 Comments

Ok i am going to try to manipulate this. In my case though i am taking an avi and converting it into png images. What i want to do with these images is find the center of a disk in each frame then crop around the center. I am trying to use convoluton to find the center with the lowest amount of error. The disk has two holes in it and i am trying to get it so i can use a matrix representing these holes to find the most probable location of the disk then get the coordinates for the center. I hope i can do this with the program you supplied.
Image Analyst also has a demo where he extracts "blobs" (nickels) from an image using the myriad of information provided by regionprops. Look at his file exchange submissions and they may help you as well.
wow, thanks a bunch for recommending that program. At first look this seems to be exactly what i was thinking of....hope i can manipulate it to work.... I will update if i can/can't.
Ok i changed the file from the test image to one that i am working with. I ran the program to see what would happen. Results: the binary image display is completely white, labeled image is completely white, "keeper blobs" is all black, and the pseudo colored labels shows red with a 1 that is nowhere near my disk.... I think the problem may lie in the fact that Image Analyst's program seems to be looking for brightness whereas my disk is the darkest thing in each of my images......therfore i tried again by changing the > to < and adjusted the threshold value to 60. now i see the disk but i am unsure how to proceed.
Image Analyst, is there a way i can send you an example image so you could recommend how to proceed?
I prefer http://www.imgur.com for image uploading. Just share the link in the comments if you're allowed (that way others could possibly try to help).
Ok, i uploaded the image that i am using to test the programs i am making. Here's the link: http://i.imgur.com/1bion.png I need the program to find the disk center without any human input besides uploading the image because the program will need to go through more than 800 images
The usual method for that would be to threshold and bwlabel and test the eccentricity of the blobs. Your disc is dark so you would want to threshold along the lines of
GrayLevel < 10
There's a gradient and weird checkerboard noise pattern over the image, but it looks like to me that a threshold of around 100 or 120 should do it. I ran blobsdemo on it and it did a pretty good job. You might run bwareaopen to get rid of the small noise blobs caused by your non-uniform background. If your backgrounds are more severely sloped, there are ways of handling that, like modeling the background or using a tophat filter, etc. But I'm not sure why you say you don't know how to proceed. It measures a bunch of stuff, like area, centroid, etc. What do you want to get out of it?
sorry, i think i was just getting frustrated, but i believe i was able to modify it to get what i wanted.
Ryan
Ryan on 23 Aug 2012
Edited: Ryan on 23 Aug 2012
You might be able to use medfilt2(Image,[m n]) to remove the grid pattern if you play with the size of the neighborhood - [m n]. A larger size might actually get rid of some of the specs as well. Using bwareaopen like Image Analyst suggested should help and eccentricity will also help as Walter suggested. Try using imfill(Image,'holes') to get a more accurate center reading once the blob is selected (IA's demo may do that already).
A 1 by 3 median filter was what I was thinking at first but it's such a perfectly symmetrical checkerboard that I don't think a median filter will work. A little more accuracy could be gotten using a sigma filter (since that won't replace "good" pixels that should be left alone like a median filter will), but since median filter is built in, I'd try that. Actually it looks like every other pixel in the checkerboard can just be removed since most of them are simply 255, except for the dark disc at the center. So maybe just replace a pixel by the darker pixel to the right, which would be a morphological erosion with a 1x2 window. But I'm not sure it's not just some kind of artifact of being uploaded so I'd have to know if that checkerboard noise was there in the original before I spent any more time playing around with it.

Sign in to comment.

what happens if i have one template and i want to find all matching object in the image if i take max it is finding only all rects in the first [ypeaka, xpeaka] = find(corrmatrx>0.9*max(corrmatrx(:)));
so all peaks very close want find the other objects

1 Comment

I'm not sure of the question. If you use normxcorr2() you can look for pixels where the output is 1 for an exact match. You can look for places where the output is less than 1 to find close, but not exact, matches. See attached demo. What exactly is your definition of "find" and "matching"? You might want to start your own thread with your own main image and template image attached.

Sign in to comment.

Categories

Tags

Asked:

on 21 Aug 2012

Commented:

on 12 Nov 2020

Community Treasure Hunt

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

Start Hunting!