Find ones blocks in upper triangular matrix

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]}

 Réponse acceptée

John D'Errico
John D'Errico le 19 Mar 2020
Modifié(e) : John D'Errico le 19 Mar 2020

0 votes

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 commentaires

Not Image Analyst, but indeed bwconncomp with an 8-connectivity (the default) will identify all the blocks in one line. regionprops would then convert the PixelIdxList returned by bwconncomp into a bounding box. With a slight tweak to account for the weirdness of what matlab defines a bounding box, it's easy to convert into the box John is talking about:
cc = bwconncomp(A, 8); %don't even need to specify 8 as it's the default, always safe to be explicit
props = regionprops(cc, {'BoundingBox'});
boundingboxes = vertcat(props.BoundingBox); %returns a Nx4 matrix
boundingboxes(:, [2, 1]) = boundingboxes(:, [1, 2]) + 0.5; %convert x/y to row/column and convert to actual index
boundingboxes(:, [3, 4]) = boundingboxes(:, [1, 2]) + boundingboxes(:, [4, 3]) - 1;
Each row consists of [top left row, top left column, bottom right row, bottom right column]
NA
NA le 20 Mar 2020
Modifié(e) : NA le 20 Mar 2020
I appreciate your taking the time to answer my question.
I do not understant first and second element of ans
props.BoundingBox
ans =
4.5000 1.5000 3.0000 3.0000
---------------- -----------------
??? size of the block
John D'Errico
John D'Errico le 20 Mar 2020
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 le 20 Mar 2020
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).

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by