Minimum width across any part of a connected component in matlab

In order to label connected components in a matrix composed of double format values (not binary values), I am using a Matlab Central File Exchange program: LABEL
So, let's say I have a double format matrix of the form:
A = [1 3 3 3 3 6 6;
1 3 3 0 6 6 6;
1 3 3 0 4 4 6;
1 2 2 0 4 4 6;
1 2 2 0 5 5 6;
1 1 1 0 5 5 6;
1 1 1 0 5 5 6]
If I run the above mentioned LABEL function with 4-connectivity, I get the results in the form of labels, area of each connected components and number of connected components.
Labels:
1 2 2 2 2 5 5
1 2 2 4 5 5 5
1 2 2 4 6 6 5
1 3 3 4 6 6 5
1 3 3 4 7 7 5
1 1 1 4 7 7 5
1 1 1 4 7 7 5
Area of each connected component:
11 8 8 8 8 10 10
11 8 8 6 10 10 10
11 8 8 6 4 4 10
11 4 4 6 4 4 10
11 4 4 6 6 6 10
11 11 11 6 6 6 10
11 11 11 6 6 6 10
and 7 number of components.
If we look at the A matrix that I have written above, we can see the connected component with value 1 is only 1 cell wide at some places (see first column of A matrix). Similarly for components with elements 0, 6, 3.
Is there any way that I can replace the cells which are only 1 cell wide (in any component) with NaN values. Basically, from all the components, I would like to remove the parts which are only 1 cell wide, and replace those parts (cells) with NaN values.
My attempts: I tried using the ratio of horizontal width to vertical width of a
BoundingBox
and
Eccentricity
from
regionprops
function, but I could not achieve any satisfactory results.
Can anybody please help me out with this? Thanks!
P.S. : The "1" cell wide value can be any threshold that we can decide in our code. If possible, I would like to be able to remove any parts which are only "threshold" cells wide (threshold can be 1 or 2 or any positive integer).

1 Kommentar

So I came across this answer by Image Analyst about Solidity, where he mentions that
"a thin "L" shape (or T or E, etc.) would have a very low solidity. The more bays, nooks, and crannies it has, the lower the solidity."
Maybe this might be of some use. I will give an update if this solves my problem.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 1 Sep. 2020
Bearbeitet: Cris LaPierre am 1 Sep. 2020
Here's a quick and simple way to do it
for c = 0:max(A,[],'all')
tmp = sum(A==c,2);
if any(tmp==1)
A(A==c)=missing;
end
end
A = 7×7
NaN 3 3 3 3 NaN NaN
NaN 3 3 NaN NaN NaN NaN
NaN 3 3 NaN 4 4 NaN
NaN 2 2 NaN 4 4 NaN
NaN 2 2 NaN 5 5 NaN
NaN NaN NaN NaN 5 5 NaN
NaN NaN NaN NaN 5 5 NaN

7 Kommentare

Ankit Sahay
Ankit Sahay am 1 Sep. 2020
Bearbeitet: Ankit Sahay am 1 Sep. 2020
This looks good, but I had a slightly different objective in mind. I would like to remove only those parts (of any component) that are 1 cell wide, not the whole component.
For example: In the original matrix A in my question, if we focus on the cells marked 1 (i.e. left and bottom-left parts of the matrix), I would like to have the following result:
NaN 3 3 3 3 6 6
NaN 3 3 0 6 6 6
NaN 3 3 0 4 4 6
NaN 2 2 0 4 4 6
NaN 2 2 0 5 5 6
1 1 1 0 5 5 6
1 1 1 0 5 5 6
Thank you for your prompt answer. It does solve a part of my problem (does remove the cells marked 0!). I will try to see if I can work further on your answer.
If I use a slightly modified version of your answer, I am able to solve a part of my problem, but then I get incorrect results with cells marked 0 (4th column), and cells marked 6 (right side of original A matrix).
for c = 0:max(A,[],'all')
tmp = sum(A==c,2);
tmp1 = sum(A==c,1);
if any(tmp==1) && any(tmp1==1)
A(A==c)=missing;
end
end
A
A =
NaN 3 3 3 3 NaN NaN
NaN 3 3 0 NaN NaN NaN
NaN 3 3 0 4 4 NaN
NaN 2 2 0 4 4 NaN
NaN 2 2 0 5 5 NaN
1 1 1 0 5 5 NaN
1 1 1 0 5 5 NaN
for c = 0:max(A,[],'all')
tmp = sum(A==c,2);
A(A==c & tmp==1)=missing;
end
A = 7×7
NaN 3 3 3 3 6 6
NaN 3 3 NaN 6 6 6
NaN 3 3 NaN 4 4 NaN
NaN 2 2 NaN 4 4 NaN
NaN 2 2 NaN 5 5 NaN
1 1 1 NaN 5 5 NaN
1 1 1 NaN 5 5 NaN
This is perfect. A very small modification I need to do is to repeat the same computation along dimension 1 also. Thank you so much!
A =
1 3 3 3 3 6 6
1 3 3 0 6 6 6
1 3 3 0 4 4 6
1 2 2 0 4 4 6
1 2 2 0 5 5 6
1 1 1 0 5 5 6
1 1 1 0 5 5 6
A_new = A;
for c = 0:max(A,[],'all')
tmp = sum(A==c,2);
A_new(A==c & tmp==1)=missing;
end
for c = 0:max(A,[],'all')
tmp = sum(A==c,1);
A_new(A==c & tmp==1)=missing;
end
A_new =
NaN 3 3 NaN NaN 6 6
NaN 3 3 NaN NaN 6 6
NaN 3 3 NaN 4 4 NaN
NaN 2 2 NaN 4 4 NaN
NaN 2 2 NaN 5 5 NaN
1 1 1 NaN 5 5 NaN
1 1 1 NaN 5 5 NaN
Hi Cris! Would it be possible for you to explain how your answer works?
for c = 0:max(A,[],'all')
tmp = sum(A==c,2);
A(A==c & tmp==1)=missing;
end
I have a few minor issues in my computations, and I would like to solve them myself. I tried debugging the code to understand each line of your answer, but I couldn't. Or you can direct to me some similar answer which I can refer to for understanding your code. I tried going through this answer, but it didn't help much.
Thanks!
Try removing the semi-colons and then run the code. You could also try splitting the code into parts to see what each bit is doing.
c=1
A==c
tmp = sum(A==c,2)
A(A==c)=missing
A(tmp==1)=missing
A(A==c & tmp==1)=missing
Change the value of c to see how it handles each value.
Okay. Thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by