Azzera filtri
Azzera filtri

3D matrix with various chain!!

3 visualizzazioni (ultimi 30 giorni)
Hisham
Hisham il 17 Ago 2012
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

Risposta accettata

Image Analyst
Image Analyst il 17 Ago 2012
Modificato: Image Analyst il 17 Ago 2012
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)

Più risposte (3)

Oleg Komarov
Oleg Komarov il 17 Ago 2012
Modificato: Oleg Komarov il 17 Ago 2012
If you have the Image Processing Toolbox:
CC = bwconncomp(U3);
labelmatrix(CC)

Hisham
Hisham il 20 Ago 2012
Modificato: Hisham il 20 Ago 2012
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 il 20 Ago 2012
topPlane = squeeze(U3(1, :, :));
bottomPlane = squeeze(U3(end, :, :));
if any(topPlane(:)) || any(bottomPlane(:))
break; % Bail out of the while loop.
end
  6 Commenti
Hisham
Hisham il 20 Ago 2012
Thaaaaaaaaaaaaaaaaaaaaanks so much. It is work now.
Hisham
Hisham il 21 Ago 2012
Modificato: Hisham il 21 Ago 2012
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
]

Accedi per commentare.

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by