Find ones blocks in upper triangular matrix

조회 수: 1 (최근 30일)
NA
NA 2020년 3월 19일
편집: NA 2020년 4월 12일
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]}

채택된 답변

John D'Errico
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
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
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!

Translated by