将bool型图像转换为uint8类型



我使用下面的链接来设置图像的阈值。https://github.com/subokita/Sandbox/blob/master/otsu.py但我的图片是灰度的,我没有和链接一样。现在我想使用函数otsu2或otsu1

img=cv2.imread('E:/tilpixs2/23_1050_450_5.0X/til1.G.png')
rows,cols,channelsNo=shape(img)
# create 256 bins histogram
hist = histogram(img, 256)[0] 
# apply the otsu thresholding
thresh=otsu2(hist,rows*cols)
img2=img>=thresh
plt.imsave("E:/tilpixs2/img2.png", img2)
img2.dtype  #it is boolean 

将img2转换为uint8,我做了一些转换。有些在这里">

img2.dtype='uint8'

img4=img2.astype(uint8)

npyage = np.array(img2)
img3=img2.np.uint8

但是当我检查保存图像时,图像属性中的位深度是32。我完全糊涂了,我该怎么办?32位深度是什么意思?我想要一个8位的int

图像这是image 的属性

假设plt是对Matplotlib的对象引用。pyplot,模块不尊重它的imsave调用的颜色深度。OpenCV的imsave将尊重数据类型。请参阅pyplot的图像保存调用文档:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imsave.html

img = cv2.imread('E:/tilpixs2/23_1050_450_5.0X/til1.G.png')
rows, cols, channelsNo = shape(img)
# create 256 bins histogram
hist = histogram(img, 256)[0]
# apply the otsu thresholding
thresh = otsu2(hist, rows * cols)
img2 = img >= thresh
img3 = img2.astype(uint8)
# Choose one of the options below depending on required format
# Save all 3 channels as a 24bpp grayscale image
cv2.imwrite("E:/tilpixs2/img2.png", img3)
# Save a single channel as a 8bpp grayscale image
cv2.imwrite("E:/tilpixs2/img2.png", img3[:,:,1])

相关内容

  • 没有找到相关文章

最新更新