【问题标题】:SimpleITK: tif to numpy array and back to tif makes file size biggerSimpleITK:tif 到 numpy 数组并返回到 tif 使文件大小更大
【发布时间】: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


【解决方案1】:

原始图像经过压缩(LZW 压缩),simpleITK 将它们上传并保存为原始大小。我在 WriteImage 中使用了选项 'useCompression'=True,但图像的大小几乎相同,为 3.8 Gb。因此,我需要在 SimpleITK 之外使用超出本文讨论的压缩方法。无论如何,谢谢你的cmets。 WriteImage(Image image, std::string const & fileName, bool useCompression=False)

【讨论】:

    猜你喜欢
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    相关资源
    最近更新 更多