How to find multiple rectangles in an image
Show older comments
Hi,
I have been going through the demo for normxcorr2 and looking for a way to adapt it to find multiple instances of a similarly shaped rectangle from within a test image. I can see that it requires finding a set of multiple peaks but I'm lost after that. Ideally I would want to see the rectangles all highlighted on the test image and a list of their coordinates. Attached are two simplified images where I want to find every rectangle in example1.png (no matter what the shade of it). Any ideas?
clear variables
close all
clc
%==========================================================================
% % store the photos into memory
file1=('example1.tif');
file2=('exampleBar.tif');
%==========================================================================
%%read in the images
img=imread(file1);
img=img(:,:,1);
imgBar=imread(file2);
imgBar=imgBar(:,:,1);
%%check images are what you expect
subplot(1,2,1)
imshow(img)
title(file1);
subplot(1,2,2)
imshow(imgBar)
title(file2);
%==========================================================================
%%how to check how many times the bar is in the original image?
c = normxcorr2(imgBar,img);
% figure
% surf(c)
% shading flat
% Find the peak in cross-correlation.
[ypeak, xpeak] = find(c==max(c(:)));
% Account for the padding that normxcorr2 adds.
yoffSet = ypeak-size(imgBar,1);
xoffSet = xpeak-size(imgBar,2);
% Display the matched area.
figure
imshow(img);
imrect(gca, [xoffSet+1, yoffSet+1, size(imgBar,2), size(imgBar,1)]);
1 Comment
Malte Herrmann
on 24 Aug 2018
2 thoughts:
1. You can check the color value for each pixel and see if it is different from [1, 1, 1] (or whatever your background is) and then change its color to [0, 0, 0]. That way all rectangles become black and are separated from the shades. Maybe this will help the cross correlation.
2. IIRC, there's 2D-Fast Fourier Transformation. This might help in recognizing existing shapes by identifying shapes and sizes? Don't know about the implementation though...
Accepted Answer
More Answers (0)
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!