Added histograms

This commit is contained in:
Tom Selier 2023-09-22 15:23:22 +02:00
parent 36d107587f
commit d65bc9848a

View File

@ -7,6 +7,7 @@ from PIL import ImageTk, Image
import numpy as np
import cv2
import time
import matplotlib.pyplot as plt
PROJECT_PATH = pathlib.Path(__file__).parent
PROJECT_UI = "./src/gui/main.ui"
@ -16,6 +17,7 @@ class MainApp:
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)
@ -29,6 +31,10 @@ class MainApp:
self.img_current = 0
self.img_max = 0
# Plots
self.axs = self.createPlot(2, 2)
# UI
self.blur_rate = None
self.img_size = None
self.sobel_select = None
@ -69,6 +75,24 @@ class MainApp:
else:
print("Nothing to export!")
def createPlot(self, columns, rows):
fig, axs = plt.subplots(columns, rows)
return axs
def drawHist(self, image, labels, column, row):
self.axs[column, row].clear()
for i,lab in enumerate(labels):
hist = cv2.calcHist(
[image],
[i],
None,
[256],
[0, 256],
)
self.axs[column, row].plot(hist, label=lab)
self.axs[column, row].grid()
self.axs[column, row].legend()
def update(self, event=None):
path = self.img_path.get()
print(path)
@ -142,6 +166,24 @@ class MainApp:
img_canny = cv2.Canny(image=img_blur,threshold1=ct1,threshold2=ct2)
output.append(img_canny)
# BGR
output.append(img[:, :, 0]) # B
output.append(img[:, :, 1]) # G
output.append(img[:, :, 2]) # R
if img is not None:
self.drawHist(img, ('B', 'G', 'R'), 0, 0)
# HSV
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
output.append(img_hsv)
output.append(img_hsv[:, :, 0]) # H
output.append(img_hsv[:, :, 1]) # S
output.append(img_hsv[:, :, 2]) # V
if img_hsv is not None:
self.drawHist(img_hsv, ('H', 'S', 'V'), 0, 1)
plt.show(block=False)
# Draw all output images
for idx, data in enumerate(output):
# Create ui image