I have a dicom
image but the image is padded. I have code to remove the padding from the image so that only the scan is left but I have to open the image using ImageJ and manually find min and max values for the x and y axis for where the image starts and ends. The scan has a gray value range of -3000 to 2000
. The padded area has a value of 0
. Is there a way to find these min and max values without having do it manually?
Original Image:
Desired Image:
Below a Python script using SimpleITK that crops out the background.
The basic idea is that it creates a mask image of pixels that are not the background value. Then it uses SimpleITK's LabelShapeStatisticsImageFilter to find the bounding box for the non-zero pixels in that mask image.
Because I used your 8-bit PNG image, the background value is set to 161. If you use your original 16-bit DICOM CT, you'd use a background value of 0. SimpleITK can read DICOM, along with a number of other image formats.
For more info about the LabelShapeStatisticsImageFilter class, here's the documentation: https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1LabelShapeStatisticsImageFilter.html#details