简体   繁体   English

转换图像灰度python错误

[英]convert image gray scale python error

I'm working on a 3D scanner; 我正在使用3D扫描仪; my first step is to convert image to gray scale: 我的第一步是将图像转换为灰度:

 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()

and this is the error message I get: 这是我收到的错误消息:

   `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'

` `

Regardless of the error message you got, you can reach your goal by setting the flag cv2.IMREAD_GRAYSCALE for cv2.imread() . 无论收到什么错误消息,都可以通过将标志cv2.IMREAD_GRAYSCALE设置为cv2.imread()来实现目标。 To type less, you can write 0 instead of cv2.IMREAD_GRAYSCALE . 要少输入,可以写0而不是cv2.IMREAD_GRAYSCALE

Note also that you have a typo in cv2.destroyALLWindows() . 还要注意,您在cv2.destroyALLWindows()有一个错字。 Change it to cv2.destroyAllWindows() instead 改为cv2.destroyAllWindows()

So your code becomes simply: 因此,您的代码将变得简单:

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()

Or: 要么:

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()

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

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