gpt4 book ai didi

python - 使用python,opencv和libtiff无法保存tiff图像16位。生物科学

转载 作者:行者123 更新时间:2023-12-02 17:58:14 25 4
gpt4 key购买 nike

我尝试使用python将tif图像保存16位,遵循以下代码,我不使用open cv做到这一点,我下载了libtiff和pylibtff库来做到这一点,但是我遇到了同样的问题,现在说:

AttributeError: 'numpy.ndarray' object has no attribute 'write_image'


有人知道发生了什么吗?同时打开cv和libtiff。
@author: Albert Parra
"""
import numpy as np
import cv2
from matplotlib import pyplot as plt
#from tifffile import imsave
from libtiff import TIFF


img = cv2.imread('HSPl3_E7_LP6p5_Ch1-32_Z9_Maximum intensity.tif',-1)
img3= cv2.cvtColor(img,cv2.COLOR_GRAY2RGB)

img3 = (img3/256).astype('uint8')
#img = np.array(img, dtype=np.uint8) # This line only change the type, not values
#img = img/256 # Now we get the good values in 16 bit format
mask = np.zeros(img.shape[:2],np.uint8)

bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

rect = (160,180,700,700)
cv2.grabCut(img3,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)



mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img2 = img*mask2[:,:,np.newaxis]

masked_img = cv2.bitwise_and(img,img,mask = mask)

plt.imshow(img),plt.colorbar(),plt.show()



plt.show()



img.write_image('HSPl3_E7_LP6p5_Ch1-32_Z9_withmask.tif')
#imsave('HSPl3_E7_LP6p5_Ch1-32_Z9_withmask.tif',img2)

#cv2.waitKey()

最佳答案

该错误表明您不能使用img.write_image方法,因为imgnumpy矩阵。问题在以下语句中:

img.write_image('HSPl3_E7_LP6p5_Ch1-32_Z9_withmask.tif')
将以下语句替换为以下语句:
cv2.imwrite('HSPl3_E7_LP6p5_Ch1-32_Z9_withmask.tif', img)
您应该使用 cv2.imwrite使用 opencv保存图像

关于python - 使用python,opencv和libtiff无法保存tiff图像16位。生物科学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64206230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com