3D matrix with various chain!!

2 ビュー (過去 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 Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by