finding longest length

조회 수: 3 (최근 30일)
Mohammad Golam Kibria
Mohammad Golam Kibria 2012년 1월 14일
댓글: Ashish Uthama 2025년 5월 16일
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
nick
nick 2025년 4월 14일
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

댓글을 달려면 로그인하십시오.

답변 (2개)

DGM
DGM 2025년 4월 14일
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
Image Analyst
Image Analyst 2025년 4월 14일
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.
L = 10×10
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 2 0 0 1 0 1 1 0 1 0 0 2 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
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
DGM 2025년 4월 14일
편집: DGM 2025년 4월 14일
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
Walter Roberson 2025년 4월 14일
Consider using bwskel -- possibly with the 'MinBranchLength' option.
  댓글 수: 5
Ashish Uthama
Ashish Uthama 2025년 4월 16일
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.
Ashish Uthama
Ashish Uthama 2025년 5월 16일
@Image Analyst - sorry for the delay in looking into this. I
MinBranchLength prunes branches. i.e parts of a skeleton.
The max branch I can see is 6, which looks right to me. Would you mind explaining what you expected to see for 10 (in case I misundertood).
longestBranch = bwskel(bw, "MinBranchLength", 6)
longestBranch =
9×10 logical array
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 0 0 0 1 0 1
0 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

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by