This commit is contained in:
Tom Selier 2023-09-27 18:15:13 +02:00
commit 5aba28b120
3 changed files with 260 additions and 145 deletions

View File

@ -1,4 +1,5 @@
{
"path": "",
"out": "",
"size": 750
}

View File

@ -28,50 +28,49 @@
</containerlayout>
<child>
<object class="ttk.LabeledScale" id="canny_thr1_slider" named="True">
<property name="compound">top</property>
<property name="compound">bottom</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">60</property>
<property name="column">1</property>
<property name="padx">0</property>
<property name="row">0</property>
<property name="row">1</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="" />
<property name="text" translatable="yes">Canny thresh. 1</property>
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="ipadx">60</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>
<object class="ttk.LabeledScale" id="canny_thr2_slider" named="True">
<property name="compound">bottom</property>
<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="columnspan">2</property>
<property name="ipady">5</property>
<property name="column">2</property>
<property name="columnspan">1</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="canny_thr2_label" named="True">
<property name="text" translatable="yes">Canny thresh. 2</property>
<layout manager="grid">
<property name="column">2</property>
<property name="columnspan">1</property>
<property name="ipady">5</property>
<property name="row">0</property>
<property name="rowspan">1</property>
</layout>
</object>
@ -80,73 +79,65 @@
<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="title" translatable="yes">Dataset</property>
<property name="type">directory</property>
<bind sequence="&lt;&lt;PathChooserPathChanged&gt;&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">1 </property>
<property name="ipadx">30</property>
<property name="row">4</property>
<property name="row">7</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">Dataset path</property>
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">1</property>
<property name="row">5</property>
</layout>
</object>
</child>
<child>
<object class="ttk.LabeledScale" id="blur_rate_slider" named="True">
<object class="ttk.LabeledScale" id="contrast_slider" named="True">
<property name="compound">bottom</property>
<property name="from_">0</property>
<property name="to">30</property>
<property name="variable">int:blur_rate</property>
<property name="to">500</property>
<property name="variable">int:contrast</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">0</property>
<property name="ipadx">60</property>
<property name="padx">0</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.Label" id="contrast_label" named="True">
<property name="text" translatable="yes">Contrast adjust</property>
<layout manager="grid">
<property name="column">0</property>
<property name="row">2</property>
</layout>
</object>
</child>
<child>
<object class="ttk.LabeledScale" id="img_size_slider" named="True">
<property name="compound">top</property>
<property name="compound">bottom</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">1</property>
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="ipadx">60</property>
<property name="row">6</property>
<property name="row">3</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>
<property name="text" translatable="yes">Display size adjust</property>
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">1</property>
<property name="row">7</property>
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="row">2</property>
<property name="rowspan">1</property>
</layout>
</object>
@ -157,66 +148,58 @@
<property name="values">x y both</property>
<bind sequence="&lt;Leave&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="ipadx">40</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="columnspan">2</property>
<property name="ipady">5</property>
<property name="row">3</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="prev" named="True">
<property name="text" translatable="yes">Prev img</property>
<bind sequence="&lt;ButtonPress&gt;" handler="img_prev" add="" />
<layout manager="grid">
<property name="column">1</property>
<property name="ipadx">10</property>
<property name="row">4</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="next" named="True">
<property name="text" translatable="yes">Next img</property>
<bind sequence="&lt;ButtonPress&gt;" handler="img_next" add="" />
<layout manager="grid">
<property name="column">2</property>
<property name="ipadx">10</property>
<property name="row">4</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="label2">
<property name="text" translatable="yes">Switch image</property>
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="pady">5</property>
<property name="column">0</property>
<property name="columnspan">1</property>
<property name="ipadx">42</property>
<property name="row">5</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="sobel_select_label" named="True">
<property name="justify">center</property>
<property name="text" translatable="yes">Sobel edge</property>
<layout manager="grid">
<property name="column">0</property>
<property name="columnspan">1</property>
<property name="row">4</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="prev" named="True">
<property name="text" translatable="yes">&lt; Prev img</property>
<bind sequence="&lt;ButtonPress&gt;" handler="img_prev" add="" />
<layout manager="grid">
<property name="column">1</property>
<property name="ipadx">10</property>
<property name="row">5</property>
<property name="rowspan">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="next" named="True">
<property name="text" translatable="yes">Next img &gt;</property>
<bind sequence="&lt;ButtonPress&gt;" handler="img_next" add="" />
<layout manager="grid">
<property name="column">2</property>
<property name="ipadx">10</property>
<property name="row">5</property>
<property name="rowspan">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="button3">
<property name="text" translatable="yes">Export dataset</property>
<property name="text" translatable="yes">Export PNG</property>
<bind sequence="&lt;ButtonPress&gt;" handler="apply" add="" />
<layout manager="grid">
<property name="column">2</property>
<property name="columnspan">1</property>
<property name="ipadx">0</property>
<property name="row">6</property>
<property name="ipadx">10</property>
<property name="row">7</property>
<property name="rowspan">1</property>
</layout>
</object>
</child>
@ -224,27 +207,31 @@
<object class="ttk.Entry" id="entry1">
<property name="textvariable">int:export_id</property>
<property name="width">15</property>
<layout manager="grid">
<property name="column">1</property>
<property name="row">6</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="label1">
<property name="text" translatable="yes">Img to export</property>
<layout manager="grid">
<property name="column">1</property>
<property name="row">7</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="label1">
<property name="text" translatable="yes">Export Image by ID</property>
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="row">6</property>
</layout>
</object>
</child>
<child>
<object class="tk.Text" id="dataset" named="True">
<property name="height">10</property>
<property name="height">15</property>
<property name="pady">0</property>
<property name="selectborderwidth">4</property>
<property name="setgrid">false</property>
<property name="state">disabled</property>
<property name="text" translatable="yes">Image metadata should appear here</property>
<property name="takefocus">true</property>
<property name="text" translatable="yes">Image IDs should appear here</property>
<property name="width">25</property>
<property name="wrap">word</property>
<layout manager="grid">
@ -255,6 +242,70 @@
</layout>
</object>
</child>
<child>
<object class="ttk.LabeledScale" id="brightness_slider" named="True">
<property name="compound">bottom</property>
<property name="from_">-500</property>
<property name="to">500</property>
<property name="variable">int:brightness</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid">
<property name="column">0</property>
<property name="ipadx">60</property>
<property name="row">1</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="brightness_label" named="True">
<property name="text" translatable="yes">Brightness adjust</property>
<layout manager="grid">
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="dataset_path_label" named="True">
<property name="text" translatable="yes">Dataset path</property>
<layout manager="grid">
<property name="column">0</property>
<property name="row">6</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="label4">
<property name="text" translatable="yes">Image control</property>
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="row">4</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Button" id="export_all" named="True">
<property name="text" translatable="yes">Export ID for entire dataset</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="apply_all" add="" />
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="ipadx">30</property>
<property name="row">8</property>
</layout>
</object>
</child>
<child>
<object class="ttk.Label" id="label2">
<property name="font">TkIconFont</property>
<property name="text" translatable="yes">Powered by ARNweb.nl &amp; TomSelier.com</property>
<layout manager="grid">
<property name="column">0</property>
<property name="row">8</property>
</layout>
</object>
</child>
</object>
</child>
<child>

View File

@ -10,6 +10,9 @@ import time
import matplotlib.pyplot as plt
import json
from helpers.statistics import imgStats
import datetime
import os
import copy
## UI config load
PROJECT_PATH = pathlib.Path(__file__).parent
@ -38,6 +41,7 @@ class MainApp:
# Keep track of images in dataset
self.img_current = 0
self.img_old = -1
self.img_max = 0
# Plots
@ -47,11 +51,15 @@ class MainApp:
self.canny_thr1 = None
self.canny_thr2 = None
self.img_path = None
self.blur_rate = None
self.contrast = None
self.img_size = None
self.img_size_old = 0 ## Check if the rendering size has changed, if it has the analysis has to be run
self.sobel_select = None
self.export_id = None
builder.import_variables(self, ['canny_thr1','canny_thr2','img_path','blur_rate','img_size', 'sobel_select', 'export_id'])
self.brightness = None
builder.import_variables(self,['canny_thr1','canny_thr2','img_path','contrast','img_size','sobel_select','export_id','brightness'])
builder.connect_callbacks(self)
# Load values from config after UI has been initialised
@ -91,18 +99,54 @@ class MainApp:
self.img_current = self.img_current + 1
self.update(self)
def apply(self, event=None):
def apply(self, event=None, path=None):
'''
Export current dataset
'''
# Get export settings
img_arr = self.tk_imgs
img_id = self.export_id.get()
if path == None:
path = config_json["out"]
else:
print(F"Using path: {path}")
if (img_id >= 0 and img_id < len(img_arr)):
print("export")
# Create file
now = datetime.datetime.now()
new_file_name = F"{self.img_current}-{self.output[1][img_id]}-{now.strftime('%Y-%m-%dT%H.%M.%S')}.png"
# Put data
file_path = pathlib.Path(path, new_file_name)
# print(file_path)
imgpil = ImageTk.getimage(self.tk_imgs[img_id])
imgpil.save(file_path, "PNG" )
imgpil.close()
print(f"Exported Image ID {img_id} to {os.path.join(path, new_file_name)}")
else:
print("Nothing to export!")
def apply_all(self, event=None):
img_id = self.export_id.get()
img_current = copy.deepcopy(self.img_current)
now = datetime.datetime.now()
path = pathlib.Path(config_json["out"], F"{self.output[1][img_id]}-all-{now.strftime('%Y-%m-%dT%H.%M.%S')}/")
os.mkdir(path)
while True:
self.img_next()
self.update(part_update=True) # Enforce partial update since we don't need the histograms etc.
self.apply(path=path)
if (self.img_current == img_current):
break
## Ensure display is always correct with image
self.update()
def add_output(self, data, name: str):
'''
Add CV2 image to canvas output
@ -144,7 +188,6 @@ class MainApp:
self.meta.insert(END, F"{idx}: {self.output[1][idx]}\n")
# Clear output
self.output = [[] for x in range(2)]
self.meta.config(state=DISABLED)
# Draw canvas
@ -190,6 +233,7 @@ class MainApp:
# print(f"Result at thres {th1}, {th2}; \tIndex {y_ind}, {x_ind} \t= {w_res}")
# print(results[y_ind])
self.axs[column, row].title.set_text(F"Mean: {np.matrix(results).mean()}")
self.axs[column, row].imshow(results)
self.axs[column, row].xaxis.set_major_formatter(lambda x, pos: str(x*canny_step))
self.axs[column, row].yaxis.set_major_formatter(lambda x, pos: str(x*canny_step))
@ -201,9 +245,20 @@ class MainApp:
%(labels[0], mean[0], labels[1], mean[1], labels[2], mean[2],
labels[0], std[0], labels[1], std[1], labels[2], std[2]))
def update(self, event=None):
def update(self, event=None, part_update=False):
path = self.img_path.get()
## Check if hist and canny hm have to be rerendered
if not part_update: ## If partial update has not been forced, check if full update is required
if (self.img_current != self.img_old or self.img_size != self.img_size_old):
part_update = False
self.img_old = self.img_current
self.img_size_old = self.img_size
else:
part_update = True
else:
print("Partial update forced!")
if path != None and path != "":
# Get all images at current path
images = []
@ -215,9 +270,13 @@ class MainApp:
# 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()
contrast = self.contrast.get()
bright = self.brightness.get()
# Clear output
self.output = [[] for x in range(2)]
# Import and resize image
img = cv2.imread(images[self.img_current])
@ -228,15 +287,16 @@ class MainApp:
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
self.add_output(img_gray, "Grayscale")
# Contrast / brightness boost
contrast_val = contrast / 100
bright_val = bright / 100
img_contrast = np.clip(contrast_val * (img_gray + bright_val), 0, 255).astype(np.uint8)
# self.add_output(img_contrast, F"Contrast / Brightness\n c+{contrast_val} b+{bright_val}")
self.add_output(img_contrast, F"BCG")
# Blurred edition
if (br == 0):
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)
self.add_output(img_blur, "Blurred")
img_blur = cv2.GaussianBlur(img_gray, (3, 3), 0)
self.add_output(img_blur, "Blurred_k3")
# Sobel edge
if sxy in ['x', 'y', 'both']:
@ -253,17 +313,18 @@ class MainApp:
img_sobel = cv2.Sobel(src=img_blur, ddepth=cv2.CV_8U, dx=dx, dy=dy, ksize=5)
else:
img_sobel = img_gray
self.add_output(img_sobel, "Sobel Edge")
self.add_output(img_sobel, "Sobel_edge")
# self.add_output(img_sobel, F"Sobel Edge\n nz={cv2.countNonZero(img_sobel)}")
# Canny edge
img_canny = cv2.Canny(image=img_blur,threshold1=ct1,threshold2=ct2)
self.add_output(img_canny, "Canny Edge")
self.add_output(img_canny, "Canny_edge")
self.writeStats(img, ('B', 'G', 'R'), 0, 0)
# BGR
self.add_output(img[:, :, 0], "BGR B")
self.add_output(img[:, :, 1], "BGR G")
self.add_output(img[:, :, 2], "BGR R")
self.add_output(img[:, :, 0], "BGR_B")
self.add_output(img[:, :, 1], "BGR_G")
self.add_output(img[:, :, 2], "BGR_R")
if img is not None:
self.drawHist(img, ('B', 'G', 'R'), 0, 0)
@ -271,15 +332,17 @@ class MainApp:
# HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
self.add_output(img_hsv, "HSV")
self.add_output(img_hsv[:, :, 0], "HSV H") # H
self.add_output(img_hsv[:, :, 1], "HSV S") # S
self.add_output(img_hsv[:, :, 2], "HSV V") # V
self.add_output(img_hsv[:, :, 0], "HSV_H") # H
self.add_output(img_hsv[:, :, 1], "HSV_S") # S
self.add_output(img_hsv[:, :, 2], "HSV_V") # V
if not part_update:
if img_hsv is not None:
self.drawHist(img_hsv, ('H', 'S', 'V'), 0, 1)
self.writeStats(img_hsv, ('H', 'S', 'V'), 0, 1)
# Canny Heatmap
if not part_update:
self.drawCannyHM(img, 1, 1)
# Show all data