Opencv 将iplimage转换为PhotoImage以在tkinter画布上绘制
下面是一些代码:Opencv 将iplimage转换为PhotoImage以在tkinter画布上绘制,opencv,tkinter,iplimage,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(
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:
其中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)
但问题是相当实际的——为什么它不起作用,如果可能的话,做得比米莎好
#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])
#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)