1
$\begingroup$

Let's say we have the following matrix:

0 0 0 0 0 0
0 1 0 0 0 0
1 1 0 0 0 0
1 1 0 0 0 0
0 0 0 0 0 0
0 0 0 1 1 1
0 0 0 0 1 1
0 0 0 0 0 0

I want to calculate the size of every connected region of 1s and also get the corresponding matrix elements of each region. The size should be computed as (width + heigth) / 2.

In the example the bottom left region would have size (2 + 3) / 2 and the bottom right region would have size (3 + 2) / 2.

Additional to the size of the regions I also want the corresponding entries of the regions, i.e. region 1 has entries (2,2), (3,1), (3,2) and so on.

How can this be done in Matlab?

I'm looking forward for the answers and wish everybody all the best.

$\endgroup$
  • $\begingroup$ I don't see any submatrix f the size 2,3 could you be more specific? $\endgroup$ – Royi May 13 '14 at 18:43
  • $\begingroup$ What's your definition of connectedness? Path connected? If yes, what paths? Are diagonal steps allowed? $\endgroup$ – Jazzmaniac May 14 '14 at 16:00
4
$\begingroup$

Lets take the given matrix as $$A=\left[\begin{array}{cccccc}&0 &0 &0 &0 &0 &0 &\\ &0 &\color{red}{1} &0 &0 &0 &0 \\ &\color{red}{1} &\color{red}{1} &0 &0 &0 &0 \\ &\color{red}{1} &\color{red}{1} &0 &0 &0 &0 \\ &0 &0 &0 &0 &0 &0 \\ &0 &0 &0 &\color{blue}{1} &\color{blue}{1} &\color{blue}{1} \\ &0 &0 &0 &0 &\color{blue}{1} &\color{blue}{1} \\ &0 &0 &0 &0 &0 &0\end{array}\right]$$

Then use the MATLAB function bwlabel to find the connected components in the matrix.

cc=bwlabel(A);

when you run this in MATLAB you will get cc as,

$$ cc = \left[\begin{array}{cccccc} &0& 0& 0& 0& 0& 0& \\ &0& \color{red}{1}& 0& 0& 0& 0& \\ &\color{red}{1}& \color{red}{1}& 0& 0& 0& 0& \\ &\color{red}{1}& \color{red}{1}& 0& 0& 0& 0& \\ &0& 0& 0& 0& 0& 0& \\ &0& 0& 0& \color{blue}{2}& \color{blue}{2}& \color{blue}{2}& \\ &0& 0& 0& 0& \color{blue}{2}& \color{blue}{2}& \\ &0& 0& 0& 0& 0& 0& \end{array}\right]$$

1 indicate connected region 1, 2 indicates connected region 2, etc

If you want 2D pixel locations use, find function in cc

i.e.:

[r c]=find(cc==1) gives the row (r) and column (c) locations of the 1st connected region.

[r c]=find(cc==2) gives the row (r) and column (c) locations of the 2nd connected region, and so on.

Now you can get what ever you want..

$\endgroup$
0
$\begingroup$

In order to get the regions, you need to solve first a problem that is called connected component labeling. There are classical algorithms to solve it. They will give you another matrix where each connected region has a unique ID (or label). Then, you'll be able to compute all the properties that you need.

$\endgroup$
0
$\begingroup$

In matlab you can use the regionprops function.

The 'BoundingBox' option will give you the x & y sizes of each region or you can use 'Area' to get the number of pixels in each region.

The 'PixelList' option will give the (x, y) corrds of the pixels in region.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.