[英]Center cropping of 3D images for MRI images
I have a list of 3D MRI images, and I would like to crop these images to the center so that I leave only the ROI, any suggestions on how to do that?我有一个 3D MRI 图像列表,我想将这些图像裁剪到中心,以便只留下 ROI,关于如何做到这一点的任何建议?
I tried the following code, but it returned the original size at the end... Any idea how to crop all the images with different dimensions to a center?我尝试了以下代码,但它最后返回了原始大小......知道如何将所有不同尺寸的图像裁剪到中心吗?
path1=(r"\*.*")
#ALL after resliced and resized
#path_2(r"C")
#Reading multiple files ED images for all pateints 1 to 72 and reslice them to 3D.
dimensions_img =np.zeros((len(glob.glob(path1)),3)) # this help to as counter
for i, img in enumerate(sorted(glob.glob(path1))):
print(img)
a= nib.load(img)
img_data = a.get_data()
#print(a.shape)
if len(a.shape)== 4:# if the shape is 4
resliced = img_data[:,:,:,0]
print(resliced.shape)
dimensions_img[i,:]=resliced.shape
#print(dimensions_img)
#print(resliced.shape)
#dimensions_img[i,:]=resliced.shape
#print(dimensions_img)
#new_dim = np.max(dimensions_img,axis=0).astype(int)
#center = (new_dim/2).astype(int)
#new_image_zeros[int(center[0] - resliced.shape[0]/2):int(resliced.shape[0]/2+center[0]),int(center[1] - resliced.shape[1]/2):int(resliced.shape[1]/2+center[1]),int(center[2] - resliced.shape[2]/2):int(resliced.shape[2]/2+center[2])] = resliced
You might try this function and loop over your 3D MRI volumes to apply it.您可以尝试此功能并遍历您的 3D MRI 体积以应用它。
def crop3D(path_scan):
start = (90,90) # You change the values here to fit well your ROI
end = (290,290)
slices = tuple(map(slice, start, end))
return scan[slices]
A link to the dataset would be good, but posting a single image would be helpful too: makes it much easier for everyone to replicate your issue.到数据集的链接会很好,但发布单个图像也会有帮助:让每个人都更容易复制您的问题。
Normally with a numpy image you should be able to do something like this:通常使用 numpy 图像,您应该能够执行以下操作:
cropped_image = your_image[crop_y_start_index:cropy_y_stop_index,crop_x_start_index:crop_x_stop_index]
It may depend a bit on how the image is loaded / what the shape of the image numpy array is.它可能取决于图像的加载方式/图像 numpy 数组的形状。
You would need to offset the start/end indices based on the image dimensions (and crop size).您需要根据图像尺寸(和裁剪大小)偏移开始/结束索引。
Once you can crop one image, wrap that in a reusable function so you can apply different crop sizes per slice if you need to.一旦您可以裁剪一张图像,请将其包装在一个可重复使用的函数中,以便您可以在需要时为每个切片应用不同的裁剪尺寸。
I'd recomment slowing down a bit, breaking the problem further, then confidentely moving forward:我建议放慢速度,进一步解决问题,然后自信地前进:
image[0:50,0:50]
)image[0:50,0:50]
) (If later on you need to use the data in PyTorch it's worth checking out Kornia (which has a centre_crop
function among many useful features) (and will can work in Tensor format directly)) (如果以后您需要使用 PyTorch 中的数据,值得一试Kornia (它在许多有用的功能中具有
centre_crop
功能)(并且可以直接以 Tensor 格式工作))
Update Re-reading your code I noticed you're using the NiBabel library.更新重新阅读您的代码我注意到您正在使用NiBabel库。 Looking at the image slicing reference I see you can achieve similar results with numpy directly, however I see the NiBabel image has an affine transformation that determines the world-coordinates of the image elements.
查看图像切片参考,我看到您可以直接使用 numpy 获得类似的结果,但是我看到 NiBabel 图像具有确定图像元素的世界坐标的仿射变换。 I recommend going through the tutorial, then appling the same with a single slice of your data and ideally double checking that the affine data doesn't get lost in the process.
我建议阅读本教程,然后将相同的数据应用于单个数据切片,并在理想情况下仔细检查仿射数据是否在此过程中丢失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.