将iplimage转换为PhotoImage以在tkinter画布上绘制

时间:2013-07-03 09:37:41

标签: opencv tkinter iplimage

以下是一些代码:

      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做得好。

0 个答案:

没有答案