Added contrast and brightness adjust

This commit is contained in:
Arne van Iterson 2023-09-25 22:08:01 +02:00
parent 83d78c9d3f
commit cae0aed939
2 changed files with 158 additions and 127 deletions

View File

@ -28,50 +28,49 @@
</containerlayout> </containerlayout>
<child> <child>
<object class="ttk.LabeledScale" id="canny_thr1_slider" named="True"> <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="from_">0</property>
<property name="to">1000</property> <property name="to">1000</property>
<property name="variable">int:canny_thr1</property> <property name="variable">int:canny_thr1</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">1</property>
<property name="ipadx">60</property>
<property name="padx">0</property> <property name="padx">0</property>
<property name="row">0</property> <property name="row">1</property>
</layout> </layout>
</object> </object>
</child> </child>
<child> <child>
<object class="ttk.Label" id="canny_thr1_label" named="True"> <object class="ttk.Label" id="canny_thr1_label" named="True">
<property name="text" translatable="yes">Canny Edge threshold 1</property> <property name="text" translatable="yes">Canny thresh. 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"> <layout manager="grid">
<property name="column">1</property> <property name="column">1</property>
<property name="columnspan">2</property>
<property name="ipadx">60</property>
<property name="row">0</property> <property name="row">0</property>
</layout> </layout>
</object> </object>
</child> </child>
<child> <child>
<object class="ttk.Label" id="canny_thr2_label" named="True"> <object class="ttk.LabeledScale" id="canny_thr2_slider" named="True">
<property name="text" translatable="yes">Canny Edge threshold 2</property> <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"> <layout manager="grid">
<property name="column">1</property> <property name="column">2</property>
<property name="columnspan">2</property> <property name="columnspan">1</property>
<property name="ipady">5</property>
<property name="row">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> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
@ -80,73 +79,65 @@
<object class="pygubu.builder.widgets.pathchooserinput" id="img_path_input" named="True"> <object class="pygubu.builder.widgets.pathchooserinput" id="img_path_input" named="True">
<property name="mustexist">true</property> <property name="mustexist">true</property>
<property name="textvariable">string:img_path</property> <property name="textvariable">string:img_path</property>
<property name="title" translatable="yes">Dataset</property>
<property name="type">directory</property> <property name="type">directory</property>
<bind sequence="&lt;&lt;PathChooserPathChanged&gt;&gt;" handler="update" add="" /> <bind sequence="&lt;&lt;PathChooserPathChanged&gt;&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="columnspan">1 </property> <property name="columnspan">1 </property>
<property name="ipadx">30</property> <property name="ipadx">30</property>
<property name="row">4</property> <property name="row">7</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
</child> </child>
<child> <child>
<object class="ttk.Label" id="img_path_label" named="True"> <object class="ttk.LabeledScale" id="contrast_slider" named="True">
<property name="text" translatable="yes">Dataset path</property> <property name="compound">bottom</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">
<property name="from_">0</property> <property name="from_">0</property>
<property name="to">30</property> <property name="to">500</property>
<property name="variable">int:blur_rate</property> <property name="variable">int:contrast</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">0</property>
<property name="ipadx">60</property> <property name="ipadx">60</property>
<property name="padx">0</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> <property name="row">3</property>
</layout> </layout>
</object> </object>
</child> </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> <child>
<object class="ttk.LabeledScale" id="img_size_slider" named="True"> <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="from_">10</property>
<property name="to">1000</property> <property name="to">1000</property>
<property name="variable">int:img_size</property> <property name="variable">int:img_size</property>
<bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" /> <bind sequence="&lt;ButtonRelease&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">0</property> <property name="column">1</property>
<property name="columnspan">1</property> <property name="columnspan">2</property>
<property name="ipadx">60</property> <property name="ipadx">60</property>
<property name="row">6</property> <property name="row">3</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
</child> </child>
<child> <child>
<object class="ttk.Label" id="img_size_label" named="True"> <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"> <layout manager="grid">
<property name="column">0</property> <property name="column">1</property>
<property name="columnspan">1</property> <property name="columnspan">2</property>
<property name="row">7</property> <property name="row">2</property>
<property name="rowspan">1</property> <property name="rowspan">1</property>
</layout> </layout>
</object> </object>
@ -157,66 +148,58 @@
<property name="values">x y both</property> <property name="values">x y both</property>
<bind sequence="&lt;Leave&gt;" handler="update" add="" /> <bind sequence="&lt;Leave&gt;" handler="update" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">1</property> <property name="column">0</property>
<property name="columnspan">2</property> <property name="columnspan">1</property>
<property name="ipadx">40</property> <property name="ipadx">42</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="row">5</property> <property name="row">5</property>
</layout> </layout>
</object> </object>
</child> </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> <child>
<object class="ttk.Button" id="button3"> <object class="ttk.Button" id="button3">
<property name="text" translatable="yes">Export dataset</property> <property name="text" translatable="yes">Useless button</property>
<bind sequence="&lt;ButtonPress&gt;" handler="apply" add="" /> <bind sequence="&lt;ButtonPress&gt;" handler="apply" add="" />
<layout manager="grid"> <layout manager="grid">
<property name="column">2</property> <property name="column">2</property>
<property name="columnspan">1</property> <property name="columnspan">1</property>
<property name="ipadx">0</property> <property name="ipadx">0</property>
<property name="row">6</property> <property name="row">7</property>
<property name="rowspan">1</property>
</layout> </layout>
</object> </object>
</child> </child>
@ -224,25 +207,27 @@
<object class="ttk.Entry" id="entry1"> <object class="ttk.Entry" id="entry1">
<property name="textvariable">int:export_id</property> <property name="textvariable">int:export_id</property>
<property name="width">15</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"> <layout manager="grid">
<property name="column">1</property> <property name="column">1</property>
<property name="row">7</property> <property name="row">7</property>
</layout> </layout>
</object> </object>
</child> </child>
<child>
<object class="ttk.Label" id="label1">
<property name="text" translatable="yes">Useless imput</property>
<layout manager="grid">
<property name="column">1</property>
<property name="columnspan">2</property>
<property name="row">6</property>
</layout>
</object>
</child>
<child> <child>
<object class="tk.Text" id="dataset" named="True"> <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="pady">0</property>
<property name="selectborderwidth">4</property>
<property name="state">disabled</property> <property name="state">disabled</property>
<property name="text" translatable="yes">Image metadata should appear here</property> <property name="text" translatable="yes">Image metadata should appear here</property>
<property name="width">25</property> <property name="width">25</property>
@ -255,6 +240,48 @@
</layout> </layout>
</object> </object>
</child> </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>
</object> </object>
</child> </child>
<child> <child>

View File

@ -47,11 +47,13 @@ class MainApp:
self.canny_thr1 = None self.canny_thr1 = None
self.canny_thr2 = None self.canny_thr2 = None
self.img_path = None self.img_path = None
self.blur_rate = None self.contrast = None
self.img_size = None self.img_size = None
self.sobel_select = None self.sobel_select = None
self.export_id = 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) builder.connect_callbacks(self)
# Load values from config after UI has been initialised # Load values from config after UI has been initialised
@ -189,7 +191,8 @@ class MainApp:
# print(f"Result at thres {th1}, {th2}; \tIndex {y_ind}, {x_ind} \t= {w_res}") # print(f"Result at thres {th1}, {th2}; \tIndex {y_ind}, {x_ind} \t= {w_res}")
# print(results[y_ind]) # 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].imshow(results)
self.axs[column, row].xaxis.set_major_formatter(lambda x, pos: str(x*canny_step)) 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)) self.axs[column, row].yaxis.set_major_formatter(lambda x, pos: str(x*canny_step))
@ -215,9 +218,10 @@ class MainApp:
# Get all user vars # Get all user vars
ct1 = self.canny_thr1.get() ct1 = self.canny_thr1.get()
ct2 = self.canny_thr2.get() ct2 = self.canny_thr2.get()
br = self.blur_rate.get()
sxy = self.sobel_select.get() sxy = self.sobel_select.get()
size = self.img_size.get() size = self.img_size.get()
contrast = self.contrast.get()
bright = self.brightness.get()
# Import and resize image # Import and resize image
img = cv2.imread(images[self.img_current]) img = cv2.imread(images[self.img_current])
@ -228,15 +232,15 @@ class MainApp:
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
self.add_output(img_gray, "Grayscale") 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}")
# Blurred edition # Blurred edition
if (br == 0): img_blur = cv2.GaussianBlur(img_gray, (3, 3), 0)
img_blur = img_gray self.add_output(img_blur, "Blurred (3)")
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")
# Sobel edge # Sobel edge
if sxy in ['x', 'y', 'both']: if sxy in ['x', 'y', 'both']:
@ -253,7 +257,7 @@ class MainApp:
img_sobel = cv2.Sobel(src=img_blur, ddepth=cv2.CV_8U, dx=dx, dy=dy, ksize=5) img_sobel = cv2.Sobel(src=img_blur, ddepth=cv2.CV_8U, dx=dx, dy=dy, ksize=5)
else: else:
img_sobel = img_gray img_sobel = img_gray
self.add_output(img_sobel, "Sobel Edge") self.add_output(img_sobel, F"Sobel Edge\n nz={cv2.countNonZero(img_sobel)}")
# Canny edge # Canny edge
img_canny = cv2.Canny(image=img_blur,threshold1=ct1,threshold2=ct2) img_canny = cv2.Canny(image=img_blur,threshold1=ct1,threshold2=ct2)