MATLAB Answers

0

Automatically trim/remove/crop black borders/margins from images / volumes

Asked by Eric Diaz on 3 May 2014
Latest activity Commented on by Eric Diaz on 17 May 2014
Is there any way to easily and efficiently automate "trimming / removing / cropping" of "black / 0", "borders / margins" from "images / volumes" in Matlab?
In particular, I am working with 3D volumes and even 4D volumes of medical image datasets, in which the CT or MR reconstruction algorithms occasionally leave a geometric, contiguous, unused, black margin around the images.
For purposes of doing image analysis and statistics, it helps to remove this useless, spurious, zero data which tends to be problematic and contaminating when performing image analysis.

  5 Comments

I am unable to say for certain whether the black, border region is always the same, as it is an artifact of the reconstruction process. Thus, I think it would be safer to make a more generalizable algorithm.
Can anyone else help with this problem? It seems that everyone yet has given up or is no longer interested in helping.
Or is just too busy at the moment to spend the time needed.

Sign in to comment.

1 Answer

Answer by Image Analyst
on 3 May 2014
 Accepted Answer

Get the black pixels imageArray == 0. Then invert and call imclearborder and subtract or XOR the two to get only those black pixels touching the border. Then use that mask to set the pixels to the desired gray level imageArray(mask) = desiredGrayLevel.

  31 Comments

The "junk" is the data that is 0. It is an artifact in more ways than one.
The data that is not zero, but with very small grayscale value is actually useful. There are usually thousands of pixels with very small grayscale values which can be used to estimate the noise.
If noise estimation wasn't important, there wouldn't be a need to preserve anything below a certain threshold, but that's not the case.
The junk is always outside the main body. Unfortunately, there are sometimes digitization artifacts within the body, i.e. - sparsely scattered zero pixels. As these pixels are within the main body, they must be retained as pertinent digital artifacts.
The idea is that the noise estimation needs to be as robust and accurate as possible, because when trying to estimate the true signal, an accurate estimate of the noise is needed.
So you want to get all low value pixels outside the main body that are not exactly zero? If so, that's really easy. Just threshold at something like 300 and at zero and OR them and invert then extract
mask = grayImage > 300; % Mask is bright stuff.
% Fill in the body
mask = imfill(mask, 'holes'); % Mask is whole solid body.
% OR it in with the zeros
mask = mask | (grayImage == 0); % Mask now includes pure zeros.
% Extract pixels that are not masked
darkNonZeroOutsidePixels = grayImage(~mask);
That is a solution that works. I like it.
I think I have just figured out how to fuse the two solutions to beta solution which works best for me.
I can use the aggressive crop to retain main body and perform means clustering on that subset of images. And I can use the above, XOR solution to perform noise estimation on just the useful pixels.
The additional problem, which I needed to address was the degrees of freedom when performing clustering.
Thanks for your help and suggestions, Image Analyst.

Sign in to comment.