gpt4 book ai didi

python - 转换图像灰度python错误

转载 作者:行者123 更新时间:2023-12-02 17:45:31 26 4
gpt4 key购买 nike

我正在使用3D扫描仪;我的第一步是将图像转换为灰度:

 from PIL import *
import scipy
import scipy.ndimage as ndimage
import scipy.ndimage.filters as filters
from numpy import *
from pylab import *
import cv2

cv2.namedWindow("Image")

image = cv2.imread('/home/mehdi/Bureau/002.jpg')
im = scipy.misc.imread(image,flatten=1)
cv2.imshow("Image",im)
cv2.waitKey(0)
cv2.destroyALLWindows()

这是我收到的错误消息:
   `opengl support available 
Traceback (most recent call last):
File "mehdi01.py", line 12, in <module>
im = scipy.misc.imread(image,flatten=1)
File "/usr/lib/python2.7/dist-packages/scip/misc
/pilutil.py", line 97, in imread
im = Image.open(name)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py",
line 1959, in open
prefix = fp.read(16)
AttributeError: 'numpy.ndarray' object has no attribute 'read'

`

最佳答案

无论收到什么错误消息,都可以通过将cv2.IMREAD_GRAYSCALE设置为cv2.imread()来达到目标​​。要减少键入,您可以编写0而不是cv2.IMREAD_GRAYSCALE

还要注意,您在cv2.destroyALLWindows()中有一个错字。改成cv2.destroyAllWindows()
因此,您的代码将变得简单:

from numpy import *
import cv2

cv2.namedWindow("Image")
im = cv2.imread('/home/mehdi/Bureau/002.jpg',0)
cv2.imshow("Image",im)
cv2.waitKey(0)
cv2.destroyAllWindows()

要么:
from numpy import *
import cv2

cv2.namedWindow("Image")
im = cv2.imread('/home/mehdi/Bureau/002.jpg',cv2.IMREAD_GRAYSCALE)
cv2.imshow("Image",im)
cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - 转换图像灰度python错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326576/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com