필터 지우기
필터 지우기

3D matrix with various chain!!

조회 수: 3 (최근 30일)
Hisham
Hisham 2012년 8월 17일
If I have U3 matrix as:
U3(:,:,1) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
U3(:,:,2) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0
]
U3(:,:,3) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
I assumed that these coordinates can be represented as an individual chain U3(3,2,1) and U3(3,2,2)and U3(4,2,2) and U3(5,2,2)and U3(3,2,3). and the other coordinates as another individual chain U3(3,4,1) and U3(3,4,2) and U3(3,4,3).
How can I give each chain a similar individual number? EX.
U3(3,2,1) and U3(3,2,2)and U3(4,2,2) and U3(5,2,2)and U3(3,2,3)=2
U3(3,4,1) and U3(3,4,2) and U3(3,4,3)=3

채택된 답변

Image Analyst
Image Analyst 2012년 8월 17일
편집: Image Analyst 2012년 8월 17일
You can use bwconncomp. See this demo I wrote for you:
U3(:,:,1) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
U3(:,:,2) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0
]
U3(:,:,3) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
% Method 1
% Make the list for how you want the objects labeled:
desiredNumberList = [2 3 4 42 69 123 73]; % Whatever you want.
% Make an object for the output
labeledImage = zeros(size(U3), 'int16')
% Do connected components labeling.
connectivityObject = bwconncomp(U3)
% Get the number of object it found (optional).
numberOfObjects = connectivityObject.NumObjects
% Now go through the blobs, reassiging them with the labels Hisham wants.
for blob = 1 : numberOfObjects
% Get the linear indices of the pixels that
% identify this particular blob.
pixelIndexes = connectivityObject.PixelIdxList{blob}
% Make those pixels have the desired number.
labeledImage(pixelIndexes) = desiredNumberList(blob);
end
% Print out the output image to the command window.
labeledImage
% Method 2
% Use labelmatrix but cast to uint16 if you ever expect to have more than 255 blobs.
labeledImage = uint16(labelmatrix(connectivityObject))
% But you didn't want 1,2,3, etc. You wanted custom numbers.
% For that we need to remap the default label numbers using intlut().
% Right now the blobs are labeled 1,2,3, etc.
% Make those pixels have the desired number with intlut().
% Make the list for how you want the objects labeled:
% But first one has to be zero because zero must remain as zero.
desiredNumberList = zeros(1, int32(intmax('uint16'))+1, 'uint16');
desiredNumberList(1:8) = [0 2 3 4 42 69 123 73]; % Whatever you want.
labeledImage = intlut(labeledImage, desiredNumberList)

추가 답변 (3개)

Oleg Komarov
Oleg Komarov 2012년 8월 17일
편집: Oleg Komarov 2012년 8월 17일
If you have the Image Processing Toolbox:
CC = bwconncomp(U3);
labelmatrix(CC)

Hisham
Hisham 2012년 8월 20일
편집: Hisham 2012년 8월 20일
It is very nice. The first part of the problem has been solved.
The second part of the problem is:
How can I stop my "while" loop when the beginning of any chain start from the first row of the matrix, and its end is on the last row of the matrix?
for example:
the following matrix is inside the "while" loop and I want stop the loop at the previous condition "when any chain terminals are on the first row and the last row of the matrix as in the next example:
U3(:,:,1) = [
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
U3(:,:,2) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
U3(:,:,3) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0
]

Image Analyst
Image Analyst 2012년 8월 20일
topPlane = squeeze(U3(1, :, :));
bottomPlane = squeeze(U3(end, :, :));
if any(topPlane(:)) || any(bottomPlane(:))
break; % Bail out of the while loop.
end
  댓글 수: 6
Hisham
Hisham 2012년 8월 20일
Thaaaaaaaaaaaaaaaaaaaaanks so much. It is work now.
Hisham
Hisham 2012년 8월 21일
편집: Hisham 2012년 8월 21일
Sorry it is not the one that I want ):
Actually, the code that you provided is going to check whether the top row and the bottom row contain number one, and then break the "while" loop.
However, the code that I want it is check for the chain starting and ending. If the same chain starts from the first row and finishes at the bottom row, then stop the "while" loop working.
Example of a chain starts from the stopped row and finishes at the bottom row:
U3(:,:,1) = [
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
U3(:,:,2) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0
]
U3(:,:,3) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
whereas the next example shows there is no chain starts from the topped row and finishes at the bottom row.
U3(:,:,1) = [
0 , 0 , 0 , 1 , 0;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
U3(:,:,2) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0
]
U3(:,:,3) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by