Find ones blocks in upper triangular matrix
2 次查看(过去 30 天)
显示 更早的评论
I have A
A=[0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 1 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 1 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 0 0 1 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 0 0 0 1 0;
0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0];
I want to find blocks of one in this matrix according to
blocks={[2,3,4,5,6,7],[6,7,8,9,10],[9,10,11,11,12]}
0 个评论
采纳的回答
John D'Errico
2020-3-19
编辑:John D'Errico
2020-3-19
Let me plead for you to change how you are doing things. What you are asking to do is perhaps the most silly form of information storage I have seen in a while. To understand what I mean by that, what block is decribed by the set:
[7 8 9 10 11]
That is, does this describe a 1x4 rectangle, thus with 1 row (row 7) and 4 columns (8:11)? Or perhaps a 2x3 rectangle? Or perhaps a 3x2 rectangle? Or perhaps a 4x1 rectangle?
Do you recognize this is just an information storage idea that is just begging for a future head pounding bug, where you then need to disambiguate which of those many possible boxes was intended?
A far better solution is to just indicate the box by 4 numbers, ALWAYS, thus the first and last row and the first and last column in said box.
As far as how to find those bounding boxes, this should not be that difficult. For any 1 in the array, if another element is within 1 pixel, you would necessarily merge the two corresponding boxes, though I'm not sure what rules you may have for those mergers, so I must stop here for the moment. In fact, I am pretty confidant that if Image Analyst views this question, his response will be a simple, direct solution based on the IPT. If he does, then use it! :)
4 个评论
John D'Errico
2020-3-20
Guillaume - you should post that as an answer. I lack the IPT, so I cannot do so.
To respond to Na, I think this is simply a problem of definition. Where does the edge of a bounding box live? Is it midway between pixels?
Guillaume
2020-3-20
Indeed, matlab considers the edge of the bounding box to be half-way between two pixels, so coordinates are always 0.5 less than the pixel index which is at the edge. That's why you've got + 0.5 in the code I wrote.
@John, the reason I didn't post it as an answer is because I feel your point about the data storage is a lot more important than the method by which the result is obtained.
@Na, Note that you may be more interested in some other properties returned by regionprops such as Image (which would be each submatrix) or PixelList (the coordinates of each 1 within each block).
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!