我正在嘗試執行以下操作,但找不到正確的代碼。
圖形界面1:
- 2 個按鈕,按鈕 1 = 加載地形,按鈕 2 = 查找隕石坑
- 如果您按下按鈕 1,它將打開檔案資源管理器,您可以選擇一個影像
- 您選擇的影像將顯示在 GUI 中
開放式簡歷:
- 它會讀取您選擇的影像
- 它會找到該影像中的所有圓圈
- 它將顯示在 python 控制臺中
圖形用戶界面2:
- 如果您按下按鈕 2,它將在 GUI 中打開(OpenCV)影像
現在我不知道如何在 GUI 中打開(OpenCV)影像。它只打開到 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()
我想要一些幫助
杰克遜
uj5u.com熱心網友回復:
您可以使用 ImageTk 枕頭并將其設定為 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)
uj5u.com熱心網友回復:
您可以將影像轉換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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/421574.html
標籤:
上一篇:錯誤:提供的關鍵元素與架構不匹配
下一篇:使用opencv訪問掩碼的像素值
