当前位置: 动力学知识库 > 问答 > 编程问答 >

Find Length of Paths Through Boolean Matrix in MatLab

问题描述:

I have a set of 5x5 boolean matrices, one for example 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 a way to output, for every connected series of 1's (connected meaning a 1 is to the left, right, above or below another 1, diagonals do not count) The length it takes up on the x axis and the length it takes up in the y axis. E.G. for this matrix There would be one that goes all the way from the left to the right, so 5 units in the x, and 4 units in the y:

 1

1 1 1

1 1 1

1 1 1 1

There would also be a set in the top right corner that has 2 units in the x and only 1 in the y:

 1 1

and there would be one in the bottom right that only takes up 1 in the x and 1 in the y.

I would like this to output for this matrix something along the line of xmax1=5 ymax1=4,

xmax2=2 ymax2=1, xmax3=1 ymax3=1. I've managed to make some code that is getting along the right lines, but only works for simple matrices. E.g. one's where there is only one connected set of 1s, and where the xmax and ymax are just simple straight lines e.g. I can do it 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

As the longest x path is a straight line across the top, and the longest y max is a straight line downwards. However if it starts to 'snake' e.g.:

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 get out a result of ymax=3 and xmax=3 as they are the longest straight lines, rather than what it should actually be which is the length from the furthest apart ones, so in this case should be ymax=5 and xmax=4

I am new with MatLab and trying to teach myself, and hence I think I can think of many methods that would work, however I can not think of how to implement them. I have tried multiple methods but I can't quite think how to properly implement them.

Thank you for any help.

网友答案:

Identify 4-connected components and their features with bwconncomp() then call regionprops() to ease 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 example:

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

Note that there 3 connected components (a single element counts as well):

bwconncomp(A,4)
ans = 
    Connectivity: 4
       ImageSize: [5 5]
      NumObjects: 3
    PixelIdxList: {[11x1 double]  [2x1 double]  [25]}
网友答案:

If you have the Image Processing Toolbox, the function regionprops may be helpful. It returns all kinds of information about the connected components (regions of adjacent 1's).

One of the features it can return identifies the indices of each element in the connected component - you can convert these to subscripts using ind2sub, and then look at the max and min values of the 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 of the other features returned by regionprops are helpful for whatever you're doing. Take a look. :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: