【问题标题】:Anyone knows what is in Skimage TIfffile save, unknown error "type b".?任何人都知道 Skimage TIfffile 保存中的内容,未知错误“类型 b”。?
【发布时间】:2020-09-16 11:08:17
【问题描述】:

我在保存 tiff 文件(堆栈灰度)时遇到一个奇怪的错误,知道吗?:

文件 "C:\Users\ptyimg_np.MT00200169\Anaconda3\lib\site-packages\tifffile\tifffile.py", 第 1241 行,保存中 sampleformat = {'u': 1, 'i': 2, 'f': 3, 'c': 6}[datadtype.kind] KeyError: 'b'

我的代码是

 #!/usr/bin/env python
# -*- coding: utf-8 -*-
from skimage.morphology import watershed
from skimage.feature import peak_local_max
from scipy import ndimage
from skimage import img_as_float
from skimage import exposure,io 
from skimage import external 
from skimage.color import rgb2gray
from skimage.filters import threshold_local  , threshold_niblack
import numpy as np
import  tifffile 
from joblib import Parallel, delayed 
import sys

# Load an example image
input_namefile = sys.argv[1]
output_namefile = 'seg_'+ input_namefile  

#Settings 
block_size = 25 #Size block of the local thresholding 


img = io.imread(input_namefile, plugin='tifffile') 
thresh =  threshold_niblack(img, window_size=block_size , k=0.8) # 
res = img > thresh
res = np.asanyarray(res)
print("saving segmentation")
tifffile.imsave(output_namefile, res , photometric='minisblack' )

【问题讨论】:

  • 失败的 tifffile 版本是什么?我无法使用最新版本 2020.5.25 重现此内容。
  • @cgohlke 是的,在最新版本中,dict 访问由if bilevel and not datadtype.kind == 'u': 保护,所以你不要点击它。

标签: save tiff scikit-image


【解决方案1】:

先分析一下。违规行:

sampleformat = {'u': 1, 'i': 2, 'f': 3, 'c': 6}[datadtype.kind]

导致KeyError 异常,因为datadtype.kind(NumPy 数据类型)的值设置为b,并且该字典中没有b。它仅适用于iufc 类型(分别为有符号整数、无符号整数、浮点数和复数浮点数)。类型 b 是布尔值。

这看起来像是您正在使用的代码中的一个错误。如果它是不受支持的,代码应该真正捕获异常并以更用户友好的方式报告它,而不是仅仅转储一个异常让您找出。

我的建议是向作者提出这个错误。


就问题的根本原因而言(这是基于分析的推测,因此可能是错误的,我只是将其作为可能原因提供) ,对您的代码的检查显示:

img = io.imread(input_namefile, plugin='tifffile') 
thresh =  threshold_niblack(img, window_size=block_size , k=0.8) # 
res = img > thresh
res = np.asanyarray(res)
tifffile.imsave(output_namefile, res , photometric='minisblack' )

上面的第三行会将res 设置为一个布尔值或一个布尔数组,该值取决于imgthresh 中每个像素的各自值(我对NumPy 的了解还不够多,无法断言关于这个)。

不过,不管怎样,它们都是一个或多个 布尔值,因此,当您尝试使用 imsave() 调用编写它们时,它会抱怨正在使用的类型(如上所述,它似乎不能正确地满足布尔值)。

基于在其他地方找到的一些示例代码:

image = data.coins()
mask = image > 128
masked_image = image * mask

我怀疑你应该使用类似于最后一行的东西来应用蒙版到图像,然后写入结果值:

img = io.imread(input_namefile, plugin='tifffile') 
thresh =  threshold_niblack(img, window_size=block_size , k=0.8)

mask = image > 128  # <-- unsure if this is needed.
res = img * thresh  # <-- add this line.

res = np.asanyarray(res)
tifffile.imsave(output_namefile, res , photometric='minisblack' )

将蒙版应用于原始图像应该会为您提供一组可用值,您可以将其写回图像文件。请注意,我不确定您是否需要 res &gt; thresh 行,因为在我看来阈值已经 为您提供了掩码。我可能在这一点上是错误的,所以我的建议仍然是向作者提出。

【讨论】:

  • > 我可能是错的,但我怀疑 :-) 对不起,你是。 =P 不是全部,而是一些关键细节。 thresh 始终是一个阈值,可以是全局(一个浮点数)或本地(一个图像)。您总是需要使用img &gt; threshold 应用阈值。布尔图像本身绝对有用,例如“前景物体的表面积”或“前景物体的大小分布”都是有用的度量。
【解决方案2】:

看起来错误是由在安装版本的tifffile中编写布尔图像中的错误引起的错误。但是,该错误已在更新的版本中修复(我当前环境中有2020.2.16)。在我的机器上,这工作正常:

import numpy as np
import tifffile

tifffile.imsave('test.tiff', np.random.random((10, 10)) > 0.5)

和在布尔映像的情况下,从不执行在您的版本中导致崩溃的线。

所以,长话短语短,使用python -m pip install -U tifffile升级您的TIFFFILE版本,您的程序应该工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多