Find a submatrix in a matrix

How can I find a sub matrix in a larger one. We learned how to extract objects from an image using filter2 and bwlabel. But I still can't understand how to find a sub matrix in a larger one. And to find out how many of them are in that matrix

Answers (1)

Image Analyst
Image Analyst on 20 Nov 2015
You can use normalized cross correlation, normxcorr2(). See attached demo.

2 Comments

Thank you sir. But I need to know how can I find the submatrix and how many of them are there in the original matrix. But using the bwlabel function. Here's the example we learned: Use 'circles.png' and extract how many objects are in that image. Solution:
a = imread('circles.png');
x= [ 1 -2 1];
y= [1; -2; 1];
gx= filter2(x,a);
gy= filter2(y,a);
g= sqrt(gx.^2 + gy.^2);
[r c] = bwlabel(g,4)
Now this will return the c = 5 which is the number of objects in the image (circles) But I really don't understand why we used this method. So if you could help me understand this example so I can solve my problem based on this method. I'd be so thankful to you sir.
Whoever used that method did so because they wanted to find edges, rather than find an exact submatrix like you asked for. They are basically finding the gradient magnitude manually rather than using the preferred built-in functions imgradient() or imgradientxy(). Then they label the edges, which, assuming the edges are completely closed around the circle, will identify each ring/perimeter as a separate connected blob. It doesn't have anything at all to do with circles, except that they were in the demo image. The same code would find perimeters of any objects of any shape whatsoever. That code doesn't have anything at all to do with what you asked for.

Sign in to comment.

Asked:

on 20 Nov 2015

Commented:

on 20 Nov 2015

Community Treasure Hunt

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

Start Hunting!