OpenCV/PythonGUI,将 openCV 图像显示到 tkinter 中?
OpenCV/PythonGUI, displaying openCV image into tkinter?
我正在尝试执行以下操作,但找不到正确的代码。
GUI1:
- 2 个按钮,按钮 1 = 加载地形,按钮 2 = 寻找陨石坑
- 如果您按下按钮 1,它将打开文件资源管理器,您可以选择一个图像
- 您选择的图像将显示在 GUI 中
OpenCV:
- 它将读取您选择的图片
- 它将找到该图像中的所有圆圈
- 它将显示在 python 控制台中
GUI2:
- 如果您按下按钮 2,它将在 GUI 中打开 (OpenCV) 图像
现在我不知道如何将 (OpenCV) 图像打开到 GUI 中。
它只打开 python 控制台。
我的代码在下面
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()
我需要一些帮助
杰克逊
您可以使用 ImageTk 进行 Pillow 并在 Tkinter 中将其设置为标签。
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)
您可以将PIL.Image
图像转换为OpenCV
图像,使用OpenCV
图像查找圆圈。然后将找到的圆圈绘制到 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()
我正在尝试执行以下操作,但找不到正确的代码。
GUI1:
- 2 个按钮,按钮 1 = 加载地形,按钮 2 = 寻找陨石坑
- 如果您按下按钮 1,它将打开文件资源管理器,您可以选择一个图像
- 您选择的图像将显示在 GUI 中
OpenCV:
- 它将读取您选择的图片
- 它将找到该图像中的所有圆圈
- 它将显示在 python 控制台中
GUI2:
- 如果您按下按钮 2,它将在 GUI 中打开 (OpenCV) 图像
现在我不知道如何将 (OpenCV) 图像打开到 GUI 中。 它只打开 python 控制台。
我的代码在下面
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()
我需要一些帮助
杰克逊
您可以使用 ImageTk 进行 Pillow 并在 Tkinter 中将其设置为标签。
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)
您可以将PIL.Image
图像转换为OpenCV
图像,使用OpenCV
图像查找圆圈。然后将找到的圆圈绘制到 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()