finding a part of an image using convolution
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How would I implement convolution using a 18x10 matrix to find something in a 570x760 image matrix?
1 Kommentar
Walter Roberson
am 2 Sep. 2012
Please see the guide to tags and retag this question; see http://www.mathworks.co.uk/matlabcentral/answers/43073-a-guide-to-tags
Antworten (3)
Walter Roberson
am 21 Aug. 2012
conv2() ?
4 Kommentare
Image Analyst
am 2 Sep. 2012
Bearbeitet: Image Analyst
am 2 Sep. 2012
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.
Image Analyst
am 22 Aug. 2012
Bearbeitet: Image Analyst
am 22 Aug. 2012
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 Kommentare
Ryan
am 23 Aug. 2012
Bearbeitet: Ryan
am 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).
Image Analyst
am 23 Aug. 2012
Bearbeitet: Image Analyst
am 23 Aug. 2012
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.
michael scheinfeild
am 12 Nov. 2020
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 Kommentar
Image Analyst
am 12 Nov. 2020
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.
Siehe auch
Kategorien
Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!