From b7f3cf02603eadbe6ed76e189f9900f81a32e215 Mon Sep 17 00:00:00 2001 From: ludovic Date: Tue, 4 Feb 2020 19:49:02 +0100 Subject: [PATCH] Error correction for circle detection, autocrop modification, report modification --- AMi_Image_Analysis.py | 18 +++++++++--------- Check_Circle_detection.py | 2 +- autocrop.py | 37 ++++++++++++++++++++++++------------- pdf_writer.py | 2 ++ preferences.py | 10 +++++----- 5 files changed, 41 insertions(+), 28 deletions(-) diff --git a/AMi_Image_Analysis.py b/AMi_Image_Analysis.py index 3422110..439cd79 100755 --- a/AMi_Image_Analysis.py +++ b/AMi_Image_Analysis.py @@ -47,7 +47,7 @@ def Citation(): print(''' -Program written for in python3/PyQt5 by +Program written for python3/PyQt5 by Ludovic Pecqueur Laboratoire de Chimie des Processus Biologiques Collège de France. @@ -688,14 +688,14 @@ def buttonClicked(self): ClassificationColor[self.classifications[well]]["text"])) - def LoadWellImage(self,path): - - QtGui.QPixmapCache.clear() - label=QLabel(self) - pixmap=QPixmap(path) - #resize pixmap to size of the QscrollArea Temporary? - label.setPixmap(pixmap.scaled(860, 630, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)) - self.ImageViewer.setWidget(label) + # def LoadWellImage(self,path): + # ''' ''' + # QtGui.QPixmapCache.clear() + # label=QLabel(self) + # pixmap=QPixmap(path) + # #resize pixmap to size of the QscrollArea Temporary? + # label.setPixmap(pixmap.scaled(860, 630, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)) + # self.ImageViewer.setWidget(label) def open_image(self, path): '''based on https://vincent-vande-vyvre.developpez.com/tutoriels/pyqt/manipulation-images/''' diff --git a/Check_Circle_detection.py b/Check_Circle_detection.py index 8425d60..fb45855 100755 --- a/Check_Circle_detection.py +++ b/Check_Circle_detection.py @@ -36,7 +36,7 @@ def DetectCircle(_file): # Apply Hough transform on the blurred image. detected_circles = cv2.HoughCircles(gray_blurred, cv2.HOUGH_GRADIENT, 1, pref.minDistance , param1 = pref.param1, - param2 = pref.param1, minRadius =pref.minRadius, maxRadius = pref.maxRadius) + param2 = pref.param2, minRadius =pref.minRadius, maxRadius = pref.maxRadius) euclidians=[] diff --git a/autocrop.py b/autocrop.py index 7ae498e..9456c1a 100755 --- a/autocrop.py +++ b/autocrop.py @@ -12,7 +12,7 @@ python3 autocrop.py Function is now called within GUI. - Can still be used as standalone + Can still be used as standalone script """ import os @@ -31,19 +31,32 @@ def natural_sort_key(s): def crop_ROI(image, output_dir, well): - x, y, r = find_best_circle(image) - r=r+100 - cropped=image[y-r:y+r, x-r:x+r] - # print("CROPPED image size well %s "%well, cropped.shape) + x, y, r = find_best_circle(image) + if x==0 or y==0: #If No circle was detected + print("CROPPED image for well %s is empty, NOT SAVED"%well) + return False + r=r+20 + Ymax, Xmax =image.shape[0], image.shape[1] + if y-r<0:ymin=0 + else:ymin=y-r + if y+r>Ymax:ymax=Ymax + else:ymax=y+r + if x-r<0:xmin=0 + else:xmin=x-r + if x+r>Xmax:xmax=Xmax + else: xmax=x+r + cropped=image[ymin:ymax, xmin:xmax] + # cropped=image[y-r:y+r, x-r:x+r] + # print("CROPPED image size well %s "%well, cropped.shape, "r=", r) path=Path(output_dir).joinpath("cropped",well+".jpg") #Only save images with bytes otherwise print error message if cropped.shape[0] != 0 and cropped.shape[1] != 0: cv2.imwrite(str(path), cropped) - print("well %s saved to %s"%(well, path)) + # print("well %s saved to %s"%(well, path)) else: - # print("CROPPED image for well %s is empty"%well) + print("CROPPED image for well %s is empty, NOT SAVED"%well) return False @@ -58,9 +71,7 @@ def find_best_circle(image): cv2.HOUGH_GRADIENT, 1, pref.minDistance, param1 = pref.param1, param2 = pref.param2, minRadius = pref.minRadius, maxRadius = pref.maxRadius) - R = 0 - X = 0 - Y = 0 + R, X, Y = 0, 0, 0 euclidians=[] #Find detected circle with max radius closest to center of image, temporary bad solution @@ -75,8 +86,8 @@ def find_best_circle(image): R = int(i[1]) X = int(i[2]) Y = int(i[3]) - # print("Rmax, X, Y ", R, X, Y) - # print("DISTANCES to image center ", euclidians, "MIN distance to image center", min(euclidians)) + # if circles is not None: + # print("DISTANCES to image center for %s"%well, euclidians, "MIN distance to image center", min(euclidians)) del euclidians return X,Y,R @@ -106,7 +117,7 @@ def find_best_circle(image): output=crop_ROI(img, directory, well) if output is False: errors +=1 - error_list.append(well) + error_list.append(os.path.basename(_file)) del img, output log=Path(directory).joinpath("cropped","autocrop.log") diff --git a/pdf_writer.py b/pdf_writer.py index de144ba..e2fd280 100644 --- a/pdf_writer.py +++ b/pdf_writer.py @@ -11,6 +11,8 @@ 2019 ''' +__date__ = "04-02-2020" + class PDF(fpdf.FPDF): def header(self): diff --git a/preferences.py b/preferences.py index b84fdd0..411adec 100644 --- a/preferences.py +++ b/preferences.py @@ -16,7 +16,7 @@ #Comment/uncomment the two lines below according to your need keyboard_layout="qwerty" -#keyboard_layout="azerty" +# keyboard_layout="azerty" #autoMARCO acceptance probability criterium autoMARCO_threshold=0.60 @@ -58,8 +58,8 @@ class Shortcut(): class DetectCircle(): '''parameters for circle detection used in autocrop.py and Check_Circle_detection.py''' - param1=35 + param1=60 param2=25 - minDistance=120 - minRadius=100 - maxRadius=300 + minDistance=800 + minRadius=250 + maxRadius=400