![](/img/trans.png)
[英]How to decode this binary image file if none of image processing python libraries (Pillow and cv2) are unable to read?
[英]How to read Pillow image to cv2
我想在不保存bw
的情况下执行此操作,以避免每次保存时都必须读取文件,并直接对其进行转换,以便可以使用 opencv 读取它。
#Image Pillow open
img = Image.open('/content/drive/My Drive/TESTING/Placas_detectadas/HCPD24.png')
gray = img.convert('L')
bw = gray.point(lambda x: 0 if x<80 else 255, '1')
bw.save('xd.png')
#Imagen from opencv
im = cv2.imread('/content/xd.png')
im = ~im
cv2_imshow(im)
cv2.imwrite('wena.png',im)
有任何想法吗?
如果您的目标是将图像转换为灰度,然后对其进行遮罩,使亮度低于 80 的所有内容均为白色,而高于 80 的所有内容均为黑色,您可以在 OpenCV 中快速完成所有操作:
import cv2
im = cv2.imread('a.jpg')
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # Grayscale image
_, im = cv2.threshold(im, 80, 255, cv2.THRESH_BINARY_INV) # Threshold at brightness 80 and invert
cv2.imwrite('converted.png', im) # Write output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.