Find the length of paths through a boolean matrix in MatLab

I have a set of 5x5 Boolean matrices, one of which is below:

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

And I would like to get an output method for each connected series of 1 (the connected meaning of a 1 is on the left, on the right, above or below another 1, the diagonals are not taken into account). The length it occupies on the x axis and the length it occupies on the y axis. EG. for this matrix. There will be one that goes completely from left to right, so 5 units in x and 4 units in y:

  1 1 1 1 1 1 1 1 1 1 1 

In the upper right corner there will also be a set that has 2 units in x and only 1 in y:

  1 1 

and in the lower right corner there will be one that takes only 1 in x and 1 in y.

I would like for this matrix to output something along the line xmax1 = 5 ymax1 = 4, xmax2 = 2 ymax2 = 1, xmax3 = 1 ymax3 = 1. I managed to make a code that goes along the correct line, but only works for simple matrices. For instance. one where there is only one connected set of 1s, and where xmax and ymax are just simple straight lines, for example. I can do this for:

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

Since the longest path x is a straight line from above, and the longest y max is a straight line down. However, if he begins to "snake", for example:

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

I would choose the result ymax = 3 and xmax = 3, since they are the longest straight lines, and not what they really should be, this is the length from the most distant from each other, so in this case there should be ymax = 5 and xmax = 4

I am new to MatLab and trying to teach myself, and so I think I can come up with a lot of methods that will work, however I can not figure out how to implement them. I tried several methods, but I can not imagine how to implement them correctly.

Thanks for any help.

+4
source share
2 answers

Define 4-related components and their functions with bwconncomp() , then call regionprops() to facilitate the calculation of some properties of these components:

 A = [0 1 0 1 1 1 1 1 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 0 1] s = regionprops(bwconncomp(A,4),'BoundingBox'); 

s is a non-scalar structure and will contain the bounding box parameters for each component, i.e. [upper-left corner x, upper-left corner y, xwidth, ywidth] .

For instance:

 s(1) ans = BoundingBox: [0.5 0.5 5 4] 

Note that there are 3 related components (the same element):

 bwconncomp(A,4) ans = Connectivity: 4 ImageSize: [5 5] NumObjects: 3 PixelIdxList: {[11x1 double] [2x1 double] [25]} 
+1
source

If you have an Image Processing Toolbox, the regionprops function can be useful. It returns all kinds of information about related components (neighboring areas 1).

One of the functions that it can return identifies the indices of each element of the connected component - you can convert them to indices using ind2sub, and then look at the values ​​of max and min indices.

 foo = rand(5,5) < 0.5; stats = regionprops(foo, 'PixelIdxList'); [y,x] = ind2sub(stats(1).PixelIdxList) % index list for first region max(y) - min(y) % length in y max(x) - min(x) % length in x 

You may find that some other functions returned by regionprops are useful for everything you do. Take a look. :)

0
source

Source: https://habr.com/ru/post/1498277/


All Articles