任何人都知道 Skimage TIfffile 保存中有什么,未知错误"type b"。?



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

文件 "C:\用户\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.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 版本,你的程序应该可以工作了!

先做一些分析。违规行:

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 > thresh行,因为在我看来,阈值已经为您提供了掩码。我在这一点上可能是错的,所以我的建议仍然是向作者提出。

最新更新