Cropping a 3D matrix

26 views (last 30 days)
Fer
Fer on 25 Jun 2015
Commented: Image Analyst on 6 Jun 2017
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.

Accepted Answer

Matt J
Matt J on 25 Jun 2015
A=A(xmin+1:xmax-1,ymin+1:ymax-1,zmin+1:zmax-1);
  4 Comments
Matt J
Matt J on 6 Jun 2017
Well are your subscript indices real positive integers?
Image Analyst
Image Analyst on 6 Jun 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).

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 25 Jun 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 Comments
Image Analyst
Image Analyst on 25 Jun 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?
Fer
Fer on 29 Jun 2015
Like I said in my question, I already had written a code that will count how many slices need to be removed from the start and the end of each dimension. This count is made over the other matrix, the one I want to match with.
Thanks again for your explanation.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!