问题描述
我使用下面的链接对我的图像进行阈值处理。 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,我做了一些转换。其中一些@R_502_6329@"
img2.dtype='uint8'
或
img4=img2.astype(uint8)
或
npyage = np.array(img2)
img3=img2.np.uint8
但是当我保存时查看图像时,图像属性中的位深度是32。我完全糊涂了,我该怎么办? 位深 32 是什么意思? 我想要一个 8 位整数的图像
This is the properties of 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])