Compare commits

...

2 Commits

3 changed files with 309 additions and 0 deletions

195
src/gui/main.ui Normal file
View File

@ -0,0 +1,195 @@
<?xml version='1.0' encoding='utf-8'?>
<interface version="1.3">
<object class="tk.Toplevel" id="main" named="True">
<property name="geometry">800x600</property>
<property name="height">800</property>
<property name="title" translatable="yes">Tree Recogniser 7000</property>
<property name="width">200</property>
<containerlayout manager="pack">
<property name="propagate">false</property>
</containerlayout>
<child>
<object class="ttk.Labelframe" id="options" named="True">
<property name="height">200</property>
<property name="labelanchor">n</property>
<property name="padding">5</property>
<property name="text" translatable="yes">Options</property>
<property name="width">200</property>
<layout manager="pack">
<property name="anchor">n</property>
<property name="expand">false</property>
<property name="fill">x</property>
<property name="side">top</property>
</layout>
<containerlayout manager="grid">
<property name="anchor">n</property>
<property type="col" id="all" name="minsize">300</property>
</containerlayout>
<child>
<object class="ttk.LabeledScale" id="canny_thr1_slider" named="True">
<property name="compound">top</property>
<property name="from_">0</property>
<property name="to">1000</property>
<property name="variable">int:canny_thr1</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">0</property>
<property name="ipadx">80</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="canny_thr1_label" named="True">
<property name="text" translatable="yes">Canny Edge threshold 1</property>
<layout manager="grid">
<property name="column">0</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.LabeledScale" id="canny_thr2_slider" named="True">
<property name="from_">0</property>
<property name="to">1000</property>
<property name="variable">int:canny_thr2</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">1</property>
<property name="ipadx">80</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="canny_thr2_label" named="True">
<property name="text" translatable="yes">Canny Edge threshold 2</property>
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">1</property>
<property name="row">1</property>
<property name="rowspan">1</property>
</layout>
</object>
</child>
<child>
<object class="pygubu.builder.widgets.pathchooserinput" id="img_path_input" named="True">
<property name="mustexist">true</property>
<property name="textvariable">string:img_path</property>
<property name="type">file</property>
<bind sequence="&lt;&lt;PathChooserPathChanged&gt;&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">2</property>
<property name="ipadx">200</property>
<property name="row">4</property>
<property name="rowspan">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="img_path_label" named="True">
<property name="text" translatable="yes">Image path</property>
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">2</property>
<property name="row">5</property>
</layout>
</object>
</child>
<child>
<object class="ttk.LabeledScale" id="blur_rate_slider" named="True">
<property name="from_">0</property>
<property name="to">30</property>
<property name="variable">int:blur_rate</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">0</property>
<property name="ipadx">80</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="blur_rate_label" named="True">
<property name="text" translatable="yes">Blur kernel size</property>
<layout manager="grid">
<property name="column">0</property>
<property name="row">3</property>
</layout>
</object>
</child>
<child>
<object class="ttk.LabeledScale" id="img_size_slider" named="True">
<property name="compound">top</property>
<property name="from_">10</property>
<property name="to">1000</property>
<property name="variable">int:img_size</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">2</property>
<property name="ipadx">230</property>
<property name="row">6</property>
<property name="rowspan">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="img_size_label" named="True">
<property name="text" translatable="yes">Size</property>
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">2</property>
<property name="row">7</property>
<property name="rowspan">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Combobox" id="sobel_select_box" named="True">
<property name="textvariable">string:sobel_select</property>
<property name="values">x y both</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">1</property>
<property name="ipadx">60</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="sobel_select_label" named="True">
<property name="text" translatable="yes">Sobel edge</property>
<layout manager="grid">
<property name="column">1</property>
<property name="row">3</property>
</layout>
</object>
</child>
</object>
</child>
<child>
<object class="ttk.Labelframe" id="output" named="True">
<property name="height">200</property>
<property name="labelanchor">n</property>
<property name="text" translatable="yes">Output</property>
<property name="width">200</property>
<layout manager="pack">
<property name="expand">true</property>
<property name="fill">both</property>
<property name="side">top</property>
</layout>
<child>
<object class="tk.Canvas" id="output_canvas" named="True">
<layout manager="pack">
<property name="expand">true</property>
<property name="fill">both</property>
<property name="side">top</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</interface>

View File

View File

@ -0,0 +1,114 @@
#!/usr/bin/python3
import pathlib
import pygubu
from tkinter import *
from PIL import ImageTk, Image
import numpy as np
import cv2
PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = "./src/gui/main.ui"
class MainApp:
def __init__(self, master=None):
self.builder = builder = pygubu.Builder()
builder.add_resource_path(PROJECT_PATH)
builder.add_from_file(PROJECT_UI)
# Main widget
self.mainwindow = builder.get_object("main", master)
self.canvas = builder.get_object("output_canvas")
self.tk_imgs = []
self.canny_thr1 = None
self.canny_thr2 = None
self.img_path = None
self.blur_rate = None
self.img_size = None
self.sobel_select = None
builder.import_variables(self,
['canny_thr1',
'canny_thr2',
'img_path',
'blur_rate',
'img_size',
'sobel_select'])
builder.connect_callbacks(self)
def run(self):
self.mainwindow.mainloop()
def update(self, event=None):
path = self.img_path.get()
if path != None and path != "":
# Get all user vars
ct1 = self.canny_thr1.get()
ct2 = self.canny_thr2.get()
br = self.blur_rate.get()
sxy = self.sobel_select.get()
size = self.img_size.get()
# Keep array of cv2 images to output
output = []
# Clear previously printed images
self.tk_imgs = []
# Import and resize image
img = cv2.imread(path)
img = cv2.resize(img, (size, size), interpolation = cv2.INTER_AREA)
output.append(img) # First output image is original
# Set grayscale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
output.append(img_gray) # Second output is grayscale
# Blurred edition
if (br == 0):
# Disable
img_blur = img_gray
elif (br % 2 == 1):
img_blur = cv2.GaussianBlur(img_gray, (br, br), 0)
else:
print(f"Blur rate changed to {br - 1}")
img_blur = cv2.GaussianBlur(img_gray, (br-1, br-1), 0)
output.append(img_blur)
# Sobel edge
if sxy in ['x', 'y', 'both']:
if sxy == 'x':
dx = 1
dy = 0
elif sxy == 'y':
dx = 0
dy = 1
elif sxy == 'both':
dx = 1
dy = 1
img_sobel = cv2.Sobel(src=img_blur, ddepth=cv2.CV_8U, dx=dx, dy=dy, ksize=5)
else:
img_sobel = img_gray
output.append(img_sobel)
# Canny edge
img_canny = cv2.Canny(image=img_blur,threshold1=ct1,threshold2=ct2)
output.append(img_canny)
# Draw all output images
for idx, data in enumerate(output):
# Create ui image
tk_img = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
tk_img = ImageTk.PhotoImage(image=Image.fromarray(tk_img))
self.tk_imgs.append(tk_img)
self.canvas.create_image((size * idx),0,anchor=NW,image=self.tk_imgs[idx],tags="og")
self.canvas.draw()
if __name__ == "__main__":
app = MainApp()
app.run()