简体   繁体   English

如何将枕头图像读取到 cv2

[英]How to read Pillow image to cv2

I want to do this without saving bw to avoid having to read the file every time I save, and transform it directly so that it can be read with opencv.我想在不保存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)

Any ideas?有任何想法吗?

If your goal is to transform the image into grayscale, then mask it so everything less bright than 80 is white, and everything above that is black, you can do it all rather quickly in OpenCV:如果您的目标是将图像转换为灰度,然后对其进行遮罩,使亮度低于 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.

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