使用OpenCV读取图像并使用Tkinter进行显示 [英] Read an image with OpenCV and display it with Tkinter

查看:894
本文介绍了使用OpenCV读取图像并使用Tkinter进行显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu 14.04 LTS上有一个非常简单的程序,可以使用OpenCV读取和显示图像:

I have a very simple program on Ubuntu 14.04 LTS to read and display an image using OpenCV:

import cv2 #import OpenCV

img = cv2.imread('picture.jpg') #read a picture using OpenCV
cv2.imshow('image',img) # Display the picture
cv2.waitKey(0) # wait for closing
cv2.destroyAllWindows() # Ok, destroy the window

我的问题:

如何继续在OpenCV中读取图片,但使用Tkinter进行显示?

How can I keep reading the picture in OpenCV but display it using Tkinter ?

我问这个问题是因为我想为我的程序创建一个接口,但是OpenCV无法做到这一点,因此我需要Tkinter.但是,所有图像处理我都必须使用OpenCV在后台进行.仅显示结果必须使用Tkinter完成.

I ask this because I want to make an interface for my program but OpenCV is not able to do it so I need Tkinter for this. However, all the image processing I must do it on the background using OpenCV. Only displaying the results must be done using Tkinter.

根据上面的答案,我更改了这一行:

From the answer above, I change the line:

im = Image.open('slice001.hrs').convert2byte()

收件人:

im=cv2.imread() # (I imported cv2) 

但是我遇到了一个错误.

But I got an error.

任何提示,我将不胜感激.

I would appreciate any hints.

推荐答案

您可能想看看

You might want to take a look at this one. Here is something works for me:

import numpy as np
import cv2
import Tkinter 
import Image, ImageTk

# Load an color image
img = cv2.imread('img.png')

#Rearrang the color channel
b,g,r = cv2.split(img)
img = cv2.merge((r,g,b))

# A root window for displaying objects
root = Tkinter.Tk()  

# Convert the Image object into a TkPhoto object
im = Image.fromarray(img)
imgtk = ImageTk.PhotoImage(image=im) 

# Put it in the display window
Tkinter.Label(root, image=imgtk).pack() 

root.mainloop() # Start the GUI

这篇关于使用OpenCV读取图像并使用Tkinter进行显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆