繁体   English   中英

如何使用 python PIL 在处理后的图像中获取一组颜色

[英]How to get set of colours in an processed image using python PIL

我有一张图像,当我处理它时,我无法获得 colors。 这是我的代码:

import cv2
from PIL import Image

im = Image.open  ('imag.jpg')
ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
from collections import defaultdict
by_color = defaultdict(int)
for pixel in thresh1.getdata():
    by_color[pixel] += 1
print (by_color)

如果我只运行原始图像( im ),效果很好。 但是当我运行处理后的图像( thresh1 )时,会出现以下错误:

Traceback (most recent call last):

       File "file.py", line 62, in <module>
            ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)
        TypeError: Expected cv::UMat for argument 'src'

对于这个 function:

ret,thresh1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY)

要工作,您应该使用cv2.imread (返回numpy.array )而不是PIL.Image.open (返回Image对象)读取图像。

暂无
暂无

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

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