Cropping a 3D matrix
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello, I need the sizes of two 3D matrices to match. These matrices have several slices of zero only matrices, which I need to remove. I've done this separately but the final 3D matrices mismatch, so I have written a code that will count how many slices are cut at the beginning and at the end for each dimension (xmin, xmax, ymin, ymax, zmin, zmax) of the first. What I want to do now is remove an (xmin) number of slices from the beginning of the second 3D matrix in the x direction, an (xmax) from the end, and so on.
How can I do this, preferably without using more loops so my program isn't slow? (or with loops if there's no other way)
Thank you.
0 Commenti
Risposta accettata
Matt J
il 25 Giu 2015
A=A(xmin+1:xmax-1,ymin+1:ymax-1,zmin+1:zmax-1);
4 Commenti
Image Analyst
il 6 Giu 2017
Try this:
A = A(ymin+1:ymax-1, xmin+1:xmax-1, zmin+1:zmax-1);
because usually people call the row (vertical coordinate) "y", not "x" which is the column or horizontal coordinate and comes second in the index order (like my code in my answer).
Più risposte (1)
Image Analyst
il 25 Giu 2015
Can you just threshold the image and ask regionprops to get you the bounding box?
labeledImage = bwlabel(grayImage > 0);
measurements = regionprops(labeledImage, 'BoundingBox');
allBoundingBoxes = [measurements.BoundingBox];
x1 = allBoundingBoxes(1:6:end);
x2 = allBoundingBoxes(1:6:end) + allBoundingBoxes(4:6:end);;
y1 = allBoundingBoxes(2:6:end);
y2 = allBoundingBoxes(2:6:end) + allBoundingBoxes(5:6:end);;
z1 = allBoundingBoxes(3:6:end);
z2 = allBoundingBoxes(3:6:end) + allBoundingBoxes(6:6:end);;
% Get the overall bounds
xLeft = floor(min(x1));
xRight = ceil(max(x2));
yLeft = floor(min(y1));
yRight = ceil(max(y2));
zLeft = floor(min(z1));
zRight = ceil(max(z2));
% Crop the original image.
croppedImage = grayImage(yLeft : yRight, xLeft : xRight, zLeft : zRight);
That's just off the top of my head - not tested.
Then I guess you'd use imresize() to make two cropped images be the same standard size.
3 Commenti
Image Analyst
il 25 Giu 2015
I don't think you understand at all. What Matt told you was the same as what I told you - he just used different variable names. His line of code is the same as the last line of my code, just with different names. So his code is not a much simpler approach - it's the same simple indexing like I used.
But he left it up to you to figure out what xmin, xmax, ymin, ymax, zmin, and zmax are, whereas I figured that out for you . How do you know what xmin, etc. are? You have to figure it out somehow, so how do you do it? How do you know "how many slices are cut at the beginning and at the end for each dimension" if you don't do something like what I did?
And now that you've cropped this matrix, how are you going to use that code to make the cropped matrix match the size of some other matrix? Remember, you said you "need the sizes of two 3D matrices to match". What I suggested was to use imresize(). What are you going to do?
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!