这是一些代码:
if self.detect == True:
haars = self.loadHaars()
image = self.detectRedEyes(self.cam, haars[0], haars[1]) # -><type 'cv2.cv.iplimage'>
self.image = self.canvas.create_image(0,0,image=ImageTk.PhotoImage(image),anchor=tk.NW)
else:
我认为它应该工作,但它没有。错误代码是:
File "C:\Users\Andrzej\Desktop\PYTHON\cv\kamera.py", line 164, in update_video
self.image = self.canvas.create_image(0,0,image=ImageTk.PhotoImage(image),anchor=tk.NW) File "C:\Python27\lib\site-packages\PIL\ImageTk.py", line 109, in
__init__
mode = Image.getmodebase(mode) File "C:\Python27\lib\site-packages\PIL\Image.py", line 245, in getmodebase
return ImageMode.getmode(mode).basemode File "C:\Python27\lib\site-packages\PIL\ImageMode.py", line 50, in getmode
return _modes[mode]
KeyError: <iplimage(nChannels=3 width=640 height=480 widthStep=1920 )>
iplimage 是在 detectRedEyes 方法中创建的:
#convert numpy.ndarray to iplimage
ipl_img = cv2.cv.CreateImageHeader((img.shape[1], img.shape[0]), cv.IPL_DEPTH_8U, 3)
cv2.cv.SetData(ipl_img, img.tostring(),img.dtype.itemsize * 3 * img.shape[1])
其中 img 是 videocapture.read() 的输出 有什么问题?有人可以解释一下有什么问题吗?我使用 misha 的代码解决了这个问题:
#convert iplimage to PhotoImage via PIL image
pil_image = Image.fromstring(
'RGB',
cv.GetSize(image),
image.tostring(),
'raw',
'BGR',
image.width*3,
0)
tk_image = ImageTk.PhotoImage(pil_image)
但问题是非常实际的 - 为什么它不起作用,如果可能的话,它会比 misha 做得更好。