Reputation:
I'm trying to do the following, but I can not find the right code.
GUI1:
OpenCV:
GUI2:
Right now I can't figure out how to open the (OpenCV) image into the GUI. It only opens into the python Console.
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
Upvotes: 1
Views: 1635
Reputation: 46669
You can convert the PIL.Image
to OpenCV
image, find the circles using the OpenCV
image. Then draw those found circles onto the OpenCV
image and convert back to 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()
Upvotes: 1
Reputation: 154
You can you Pillow with ImageTk and set it to label in 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)
Upvotes: 1