From 9d00fa7f077c80cbbedcdceb175ce65c7f6265ad Mon Sep 17 00:00:00 2001 From: Arne van Iterson Date: Thu, 14 Sep 2023 14:24:12 +0200 Subject: [PATCH] Added testing suite --- src/gui/main.ui | 195 ++++++++++++++++++++++++++++++++++++++++++++++++ src/nuts.py | 0 src/suite.py | 114 ++++++++++++++++++++++++++++ 3 files changed, 309 insertions(+) create mode 100644 src/gui/main.ui delete mode 100644 src/nuts.py diff --git a/src/gui/main.ui b/src/gui/main.ui new file mode 100644 index 0000000..3dfcf14 --- /dev/null +++ b/src/gui/main.ui @@ -0,0 +1,195 @@ + + + + 800x600 + 800 + Tree Recogniser 7000 + 200 + + false + + + + 200 + n + 5 + Options + 200 + + n + false + x + top + + + n + 300 + + + + top + 0 + 1000 + int:canny_thr1 + + + 0 + 80 + 0 + + + + + + Canny Edge threshold 1 + + 0 + 1 + + + + + + 0 + 1000 + int:canny_thr2 + + + 1 + 80 + 0 + + + + + + Canny Edge threshold 2 + + 1 + 1 + 1 + 1 + + + + + + true + string:img_path + file + + + 0 + 2 + 200 + 4 + 1 + + + + + + Image path + + 0 + 2 + 5 + + + + + + 0 + 30 + int:blur_rate + + + 0 + 80 + 2 + + + + + + Blur kernel size + + 0 + 3 + + + + + + top + 10 + 1000 + int:img_size + + + 0 + 2 + 230 + 6 + 1 + + + + + + Size + + 0 + 2 + 7 + 1 + + + + + + string:sobel_select + x y both + + + 1 + 60 + 2 + + + + + + Sobel edge + + 1 + 3 + + + + + + + + 200 + n + Output + 200 + + true + both + top + + + + + true + both + top + + + + + + + diff --git a/src/nuts.py b/src/nuts.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/suite.py b/src/suite.py index e69de29..8cc9c4f 100644 --- a/src/suite.py +++ b/src/suite.py @@ -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()