[英]OpenCV/PythonGUI, displaying openCV image into tkinter?
I'm trying to do the following, but I can not find the right code.我正在尝试执行以下操作,但找不到正确的代码。
GUI1:图形界面1:
OpenCV: OpenCV:
GUI2:图形用户界面2:
Right now I can't figure out how to open the (OpenCV) image into the GUI.现在我不知道如何在 GUI 中打开(OpenCV)图像。 It only opens into the python Console.它只打开 python 控制台。
My code is down below我的代码在下面
import cv2
import numpy as np
from matplotlib import pyplot as plt
from tkinter import *
from PIL import ImageTk,Image
from tkinter import filedialog
from tkinter import ttk
import tkinter as tk
root = Tk()
root.title("Armstrong Autonomous Moon Landing System")
root.geometry("1100x600")
root.iconbitmap('C:/Users/brett/')
mb = Menubutton(root, text="Armstrong Autonomouse Moon Lander System")
mb.menu = Menu(mb)
mb["menu"] = mb.menu
########## ALL OTHER CODE NEEDS TO GO HERE
def openfile():
return filedialog.askopenfilename()
def open():
global my_image
root.filename = filedialog.askopenfilename(initialdir="/test3/images", title="Select A File", filetypes=(("png files", "*.png"),("all files", "*.*")))
my_label = Label(root, text=root.filename).pack()
my_image = ImageTk.PhotoImage(Image.open(root.filename))
my_image_label = Label(image=my_image).pack()
def find_craters():
circles_image = cv2.imread(root.filename)
if circles_image.shape[-1] == 3: # color image
b,g,r = cv2.split(circles_image) # get b,g,r
rgb_img = cv2.merge([r,g,b]) # switch it to rgb
gray_img = cv2.cvtColor(circles_image, cv2.COLOR_BGR2GRAY)
else:
gray_img = circles_image
img = cv2.medianBlur(gray_img, 5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
plt.subplot(122),plt.imshow(cimg)
plt.show()
### HOW RETURN CIRCLES_IMAGE TO TKINTER
# circles_label = Label(root, text=root.filename).pack()
# circles_image = ImageTk.PhotoImage(Image.open(cimg))
# circles_image_label = Label(image=circles_image).pack()
#circles_img = Label(root, image= cimg).pack()
# plt.subplot(122),plt.imshow(cimg)
# plt.show()
my_btn = Button(root, text="Load Terrain", command=open).pack()
my_btn2 = Button(root, text="Find Craters", command=find_craters).pack()
#my_btn3 = Button(root, text=" About Me ").pack()
mb.pack()
root.mainloop()
I would like some help我想要一些帮助
Jackson Jackson
You can you Pillow with ImageTk and set it to label in Tkinter.您可以使用 ImageTk 枕头并将其设置为 Tkinter 中的 label。
import cv2
import PIL
from PIL import ImageTk, Image
'''convert cv2 image to image tkinter to set image to label'''
def cv2_to_imageTK(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGBA)
imagePIL = PIL.Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image = imagePIL)
return imgtk
imgtk = cv2_to_imageTK(img)
label.imgtk = imgtk
label.configure(image = imgtk)
You can convert the PIL.Image
to OpenCV
image, find the circles using the OpenCV
image.您可以将PIL.Image
转换为OpenCV
图像,使用OpenCV
图像找到圆圈。 Then draw those found circles onto the OpenCV
image and convert back to PIL.Image
.然后将找到的圆圈绘制到OpenCV
图像上并转换回PIL.Image
。
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk, Image
import numpy as np
import cv2
root = tk.Tk()
root.title("Armstrong Autonomous Moon Landing System")
root.geometry("1100x600")
root.iconbitmap('C:/Users/brett/')
########## ALL OTHER CODE NEEDS TO GO HERE
def open():
global my_image
filename = filedialog.askopenfilename(initialdir="images", title="Select A File", filetypes=(("png files", "*.png"),("all files", "*.*")))
my_label.config(text=filename)
my_image = Image.open(filename)
tkimg = ImageTk.PhotoImage(my_image)
my_image_label.config(image=tkimg)
my_image_label.image = tkimg # save a reference of the image
def find_craters():
# convert PIL image to OpenCV image
circles_image = np.array(my_image.convert('RGB'))
gray_img = cv2.cvtColor(circles_image, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(gray_img, 5)
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 20,
param1=50, param2=30, minRadius=0, maxRadius=0)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0]:
# draw the outer circle
cv2.circle(circles_image, (i[0],i[1]), i[2], (0,255,0), 2)
# draw the center of the circle
cv2.circle(circles_image, (i[0],i[1]), 2, (0,0,255), 3)
# convert OpenCV image back to PIL image
image = Image.fromarray(circles_image)
# update shown image
my_image_label.image.paste(image)
tk.Button(root, text="Load Terrain", command=open).pack()
tk.Button(root, text="Find Craters", command=find_craters).pack()
# for the filename of selected image
my_label = tk.Label(root)
my_label.pack()
# for showing the selected image
my_image_label = tk.Label(root)
my_image_label.pack()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.