Help me understand this code

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) This code will find out how many objects are in the image 'circles.png' However I don't understand how it did it. Can someone please explane how this code works. Thanks

Answers (2)

Thorsten
Thorsten on 20 Nov 2015
gx and gy mark edges in the image in x and y direction. g is an image of the edges. You have 5 distinct edges in g, the outer boundary of all circles and 4 inner holes. These are returned by bwlabel.
Image Analyst
Image Analyst on 20 Nov 2015
I just answered that in your other question, but here it is again:
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.

This question is closed.

Asked:

on 20 Nov 2015

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!