필터 지우기
필터 지우기

finding a part of an image using convolution

조회 수: 17 (최근 30일)
jeremy wright
jeremy wright 2012년 8월 21일
댓글: Image Analyst 2020년 11월 12일
How would I implement convolution using a 18x10 matrix to find something in a 570x760 image matrix?

답변 (3개)

Walter Roberson
Walter Roberson 2012년 8월 21일
conv2() ?
  댓글 수: 4
jeremy wright
jeremy wright 2012년 8월 22일
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.
Image Analyst
Image Analyst 2012년 9월 2일
편집: Image Analyst 2012년 9월 2일
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
Image Analyst 2012년 8월 22일
편집: Image Analyst 2012년 8월 22일
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
Ryan
Ryan 2012년 8월 23일
편집: Ryan 2012년 8월 23일
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
Image Analyst 2012년 8월 23일
편집: Image Analyst 2012년 8월 23일
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
michael scheinfeild 2020년 11월 12일
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
Image Analyst
Image Analyst 2020년 11월 12일
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.

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by