finding longest length
16 views (last 30 days)
Show older comments
Hi ,I have a matrix as follows I =
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 1 0 1 0 0 0 1 0
0 1 0 1 1 0 1 0 0 1
1 0 0 0 1 0 1 1 0 0
0 0 0 0 1 0 1 0 0 0
0 1 1 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
I need the output as follows:
I =
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 0 0 0
0 0 1 0 0 0 0 0 1 0
0 1 0 1 0 0 0 0 0 1
1 0 0 0 0 0 0 1 0 0
0 0 0 0 1 0 1 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
i.e only the longest length of pixel that touches two opposite boundary.can any body help please?Thanks
1 Comment
nick
on 14 Apr 2025
Hello Golam,
To transform the matrix as desired, you can use the 'bwconncomp' function to identify all connected groups of 1s.
For each of these components, check whether it touches both the first and last row or the first and last column. This will help you determine if it spans two opposite boundaries. While iterating through the connected groups, keep track of the longest component.
Kindly refer to the documentation by executing the following command in MATLAB Command Window to know more about 'bwconncomp' function :
help bwconncomp
Answers (2)
DGM
on 14 Apr 2025
The written description doesn't really describe what the example describes. The example returns the last true element from only the first run of true elements in each column. Here's one way.
% the test sample
A = [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 1 0 1 0 0 0 1 0;
0 1 0 1 1 0 1 0 0 1; 1 0 0 0 1 0 1 1 0 0; 0 0 0 0 1 0 1 0 0 0; 0 1 1 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];
% the expected output
B = [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 0 0 0; 0 0 1 0 0 0 0 0 1 0;
0 1 0 1 0 0 0 0 0 1; 1 0 0 0 0 0 0 1 0 0; 0 0 0 0 1 0 1 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];
% replicate the given output
sz = size(A);
d = [A; zeros(1,sz(2))]; % trailing pad to catch runs that end at image boundary
d = diff(d,1,1) == -1; % transitions from 1 to 0
[~,row] = max(d,[],1); % find the first transition in each column
idx = sub2ind(sz,row,1:sz(2)); % convert to linear indices
idx = idx(any(d,1)); % account for cases where there are zero runs in a column
C = false(sz); % allocate
C(idx) = true; % assign
% show that they match
outpict = cat(3,A,B,C);
imshow(outpict,'initialmagnification','fit')
2 Comments
Image Analyst
on 14 Apr 2025
Yeah, he does not have a single line that stretches all the way across the width of the array. He has two that go only partway across. See, let's label them to see what pixels belong to what blobs:
bw = logical([...
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 1 0 1 0 0 0 1 0
0 1 0 1 1 0 1 0 0 1
1 0 0 0 1 0 1 1 0 0
0 0 0 0 1 0 1 0 0 0
0 1 1 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]);
L = bwlabel(bw) % Give each blob a unique label number.
See? The two ones on the right hand side (blob #2) are not connected to the one starting from the left hand side (Blob #1). It's at least 2 pixels away.
Can we assume that it was just a typo and that blob #2 should really have been connected to blob #1?
DGM
on 14 Apr 2025
Edited: DGM
on 14 Apr 2025
I'd sooner take the arrays as the problem description, given that the text "longest length" doesn't have much to do with the given result. If we read the text with the arrays in mind, "longest length of pixel that touches two opposite boundary" comes across as plausibly "last position at a transition between pixel values" -- which is still not quite the whole description.
I suppose it's a thoroughly dead question, so we can choose to assert our own interpretation at this point. I suspect it's possible that the 1x3 horizontal strip wasn't supposed to be deleted, but I'm just choosing to assume that the arrays are the accurate description.
I was going to post this earlier, regarding the comment about bwconncomp(). Given my assumption based on the given arrays, I don't really see how blob-based analysis really buys us anything.
A = [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 1 0 1 0 0 0 1 0;
0 1 0 1 1 0 1 0 0 1; 1 0 0 0 1 0 1 1 0 0; 0 0 0 0 1 0 1 0 0 0; 0 1 1 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];
% 4-connectivity gives us 9 blobs
[L nl] = bwlabel(A,4);
op4 = labeloverlay(A,L,'colormap',lines(nl),'transparency',0);
imshow(op4,'initialmagnification','fit')
% 8-connectivity gives us 2 blobs
[L nl] = bwlabel(A,8);
op8 = labeloverlay(A,L,'colormap',lines(nl),'transparency',0);
imshow(op8,'initialmagnification','fit')
Neither case makes it any easier to find the target pixels. If anything, it makes it more difficult.
Walter Roberson
on 14 Apr 2025
4 Comments
DGM
on 15 Apr 2025
Edited: DGM
on 15 Apr 2025
... iinteresting. I thought it would have actually had a steady-state. I guess it doesn't.
EDIT: hmm. It's all done in MEX. Maybe I was thinking about the bwmorph() modes, wherein connected groups can at most be reduced to single pixels. Maybe bwskel() doesn't have that constraint.
Ashish Uthama
on 16 Apr 2025 at 12:23
This is a bug, likely in the pruning logic (images.internal.pruneEdges3)within bwkel. We are tracking this at Mathworks and will work on fixing it.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!