【发布时间】:2017-03-21 20:37:57
【问题描述】:
我在 python 2.7 Anaconda 上使用 simpleITK,我做了一个测试,读取一个大 tif 文件(大约 2Gb),将其转换为 numpy 数组,将其转换回 tif 并保存在磁盘上,新文件大约为 4Gb(我尝试了 2 种不同的保存方法,但结果相同)。每一步的数据类型都是相同的,知道为什么会这样吗?谢谢 !。 :)
import SimpleITK as sitk
p1 = sitk.ReadImage('my_image.tif') #read image
# print properties
print 'Width ',p1.GetWidth()
print 'Height ',p1.GetHeight()
print 'Depth ',p1.GetDepth()
print 'Dimension ',p1.GetDimension()
print 'Pixel Id Value ',p1.GetPixelIDValue()
print 'Pixel ID Type ',p1.GetPixelIDTypeAsString()
print 'Size ',p1.GetSize()
print '__________ '
p1_np = sitk.GetArrayFromImage(p1) #get numpy array from Image
print 'Array type ',p1_np.dtype #print array type
print '__________ '
p1_np_img = sitk.GetImageFromArray(p1_np) #get image from array
# print properties
print 'Width ',p1_np_img.GetWidth()
print 'Height ',p1_np_img.GetHeight()
print 'Depth ',p1_np_img.GetDepth()
print 'Dimension ',p1_np_img.GetDimension()
print 'Pixel Id Value ',p1_np_img.GetPixelIDValue()
print 'Pixel ID Type ',p1_np_img.GetPixelIDTypeAsString()
print 'Size ',p1_np_img.GetSize()
sitk.WriteImage(p1_np_img,'my_image_test_a.tif') #save new image
sitk.WriteImage(sitk.Cast(p1_np_img,sitk.sitkUInt16),'my_image_test_b.tif') #save new image
print "End "
结果是这样的:
【问题讨论】:
-
如果我复制你写的相同指令,输出图像的维度是 3,而不是帖子中的 2。
-
您的输入图像是否启用了任何类型的压缩?
-
感谢您的 cmets。我猜维度是 2,因为我使用的是灰度图像。我没有检查原始图像的压缩,可能是的,现在检查是否可以保存为压缩的 tif。
标签: python image numpy tiff itk