简体   繁体   English

bool类型图像到uint8的转换

[英]Conversion of bool type image to uint8

I used the link below for thresholding of my images.我使用下面的链接对我的图像进行阈值处理。 https://github.com/subokita/Sandbox/blob/master/otsu.py but my images was grayscale and I didn't as same as the link. https://github.com/subokita/Sandbox/blob/master/otsu.py但我的图像是灰度的,我与链接不同。 now I want to use the function otsu2 or otsu1现在我想使用 function 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 

To convert img2 to uint8, I did some convertions.为了将 img2 转换为 uint8,我做了一些转换。 Some of them are here"他们中的一些人在这里”

img2.dtype='uint8'

or或者

img4=img2.astype(uint8)

or或者

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

But when I check the image when saved, the bit depths in properties of image is 32. I'm totally confused, What should I do?但是当我保存时检查图像时,图像属性中的位深度是32。我完全糊涂了,我该怎么办? what does 32 in bit depth means? 32位深度是什么意思? I want an image of 8 bit int我想要一个 8 位整数的图像

This is the properties of image这是图像的属性

Assuming that plt is an object reference to Matplotlib.pyplot, the module doesn't respect color depth for it's imsave call.假设 plt 是 object 对 Matplotlib.pyplot 的引用,则该模块不尊重其imsave调用的颜色深度。 OpenCV's imsave will respect the datatype. OpenCV 的 imsave 将尊重数据类型。 See the documentation for pyplot's image save call here: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.imsave.html在此处查看 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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM