From 771c089639d8d1b9db9c210976a75026b166fce6 Mon Sep 17 00:00:00 2001 From: harshmittal2210 Date: Sat, 15 Jan 2022 18:27:08 +0530 Subject: [PATCH] Added Image Processing --- Python/AutoPaint/PaintUI.py | 18 ++++++++- Python/AutoPaint/ProcessImage.py | 65 ++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 Python/AutoPaint/ProcessImage.py diff --git a/Python/AutoPaint/PaintUI.py b/Python/AutoPaint/PaintUI.py index 7f14d19..273839b 100644 --- a/Python/AutoPaint/PaintUI.py +++ b/Python/AutoPaint/PaintUI.py @@ -5,6 +5,8 @@ import cv2,imutils +from ProcessImage import ProcessImage + class PaintUI(QtWidgets.QMainWindow): def __init__(self): super(PaintUI,self).__init__() #Call the inherited classes __init__ method @@ -16,9 +18,11 @@ def __init__(self): self.cannyThreshold1 = 0 self.cannyThreshold2 = 100 + self.processImage = ProcessImage() + # Signal and Slots self.loadButton.clicked.connect(self.loadImage) - self.saveButton.clicked.connect(self.label.clear) + self.saveButton.clicked.connect(self.saveImage) self.thresh1Slider.valueChanged['int'].connect(self.thresholdValue1) self.thresh2Slider.valueChanged['int'].connect(self.thresholdValue2) @@ -32,6 +36,7 @@ def loadImage(self): if not self.fileName: return self.img = cv2.imread(self.fileName) + self.processImage.loadImage(self.img) self.setPhoto(self.img) def setPhoto(self,image): @@ -49,10 +54,21 @@ def setPhoto(self,image): def thresholdValue1(self,value): self.cannyThreshold1 = value print(f"Thresh 1: {value}") + self.update() def thresholdValue2(self,value): self.cannyThreshold2 = value print(f"Thresh 2: {value}") + self.update() + + def update(self): + if not self.fileName: + print("Select Image First") + return + self.setPhoto(self.processImage.refreshImage(self.cannyThreshold1,self.cannyThreshold2)) + + def saveImage(self): + cv2.imwrite("test.jpg",self.tmp) diff --git a/Python/AutoPaint/ProcessImage.py b/Python/AutoPaint/ProcessImage.py new file mode 100644 index 0000000..078851d --- /dev/null +++ b/Python/AutoPaint/ProcessImage.py @@ -0,0 +1,65 @@ +import pyautogui +import time +import cv2 + +pyautogui.PAUSE = 0.01 + +class ProcessImage(): + def __init__(self): + self.canvas_start_ratio_x = 10.0/1920 + self.canvas_start_ratio_y = 180.0/1080 + + self.canvas_size_ratio_x = 1070.0/1920 + self.canvas_size_ratio_y = 670.0/1080 + + self.screenWidth, self.screenHeight = pyautogui.size() + + self.canvas_start_x = self.screenWidth*self.canvas_start_ratio_x + self.canvas_start_y = self.screenHeight*self.canvas_start_ratio_y + + self.canvas_end_x = self.canvas_start_x + self.canvas_size_ratio_x*self.screenWidth + self.canvas_end_y = self.canvas_start_y + self.canvas_size_ratio_y*self.screenHeight + + self.img = None + + def loadImage(self,img): + self.img = img.copy() + (self.h,self.w) = self.img.shape[:2] + + if self.h>self.w: + self.img = self.image_resize(self.img,height=int(self.canvas_size_ratio_y*self.screenHeight)) + else: + self.img = self.image_resize(self.img,width=int(self.canvas_size_ratio_x*self.screenWidth)) + + self.image_pre_process() + self.edge_detection() + + def refreshImage(self,thesh1,thresh2): + self.edges = cv2.Canny(image=self.img_blur, threshold1=thesh1, threshold2=thresh2) + return self.edges + + def image_resize(self,image, width = None, height = None, inter = cv2.INTER_AREA): + dim = None + (h, w) = image.shape[:2] + if width is None and height is None: + return None + + if width is None: + r = height / float(h) + dim = (int(w * r), height) + else: + r = width / float(w) + dim = (width, int(h * r)) + resized = cv2.resize(image, dim, interpolation = inter) + return resized + + def image_pre_process(self): + self.img_gray = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY) + self.img_blur = cv2.GaussianBlur(self.img_gray, (3,3), 0) + + def edge_detection(self): + # self.sobelx = cv2.Sobel(src=self.img_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize=5) # Sobel Edge + # self.sobely = cv2.Sobel(src=self.img_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize=5) # Sobel Edge + # self.sobelxy = cv2.Sobel(src=self.img_blur, ddepth=cv2.CV_64F, dx=1, dy=1, ksize=5) # Combined X + + self.edges = cv2.Canny(image=self.img_blur, threshold1=30, threshold2=50)