def detect_vertical_lines(self, Mat_Binary): ''' Detect vertical lines present in the binary image ''' kernel_length = Mat_Binary.shape[1]//80 vertical_kernel = cv2.getStructuringElement( cv2.MORPH_RECT, (1, kernel_length)) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) Mat_Vertical = cv2.erode( Mat_Binary, vertical_kernel, iterations=3) KLogger.set_log('LINE_VERTICAL200', 'Detecting Vertical Lines') return Mat_Vertical
def detect_horizontal_lines(self, Mat_Binary): ''' Detect Horizontal lines present in the binary image ''' kernel_length = Mat_Binary.shape[1]//80 horizontal_kernel = cv2.getStructuringElement( cv2.MORPH_RECT, (kernel_length, 1)) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) Mat_Horizontal = cv2.erode( Mat_Binary, horizontal_kernel, iterations=3) KLogger.set_log('LINE_HORIZONTAL200', 'Detecting Horizontal Lines') return Mat_Horizontal
def detect_line_separation(self, Mat_Binary): ''' detect line separation by space ''' KLogger.set_log('DETECT_LINES200', 'Detecting Lines By Text Spacing') pts = cv2.findNonZero(Mat_Binary) rect = cv2.minAreaRect(pts) (cx, cy), (w, h), angle = rect # if w > h: # w, h = h, w # angle += 90 # M = cv2.getRotationMatrix2D((cx, cy), angle, 1.0) # rotated = cv2.warpAffine( # Mat_Binary, M, (Mat_Binary.shape[1], Mat_Binary.shape[0])) hist = cv2.reduce(Mat_Binary, 1, cv2.REDUCE_AVG).reshape(-1) th = 0 H, W = Mat_Binary.shape[:2] uppers = [y for y in range(H-1) if hist[y] <= th and hist[y+1] > th] lowers = [y for y in range(H-1) if hist[y] > th and hist[y+1] <= th] # result = cv2.cvtColor(Mat_Binary, cv2.COLOR_GRAY2BGR) result = np.zeros((H, W), dtype=np.uint8) # for y in uppers: # cv2.line(result, (0, y), (W, y), (0, 255, 0), 1) for y in lowers: cv2.line(result, (0, y), (W, y), (255, 0, 0), 1) # result = cv2.pyrUp(result) KLogger.set_log('RETURN_LINES200', 'Return Lines By Text Spacing') return result
def count_lines(self, Mat, vertical=True): ''' Return total number of lines ''' if vertical: KLogger.set_log('COUNT_VERTICAL_LINES200', 'Counting vertical lines') else: KLogger.set_log('COUNT_HORIZONTAL_LINES200', 'Counting horizontal lines') lines = cv2.HoughLines(Mat, 1, np.pi/180, 0) # vertical_lines, horizontal_lines = [], [] lines_counted = [] for line in lines: for rho, theta in line: a = np.cos(theta) # Stores the value of sin(theta) in b b = np.sin(theta) x0 = a*rho y0 = b*rho # x1 stores the rounded off value of (rcos(theta)-1000sin(theta)) x1 = int(x0 + 1000*(-b)) # y1 stores the rounded off value of (rsin(theta)+1000cos(theta)) y1 = int(y0 + 1000*(a)) # x2 stores the rounded off value of (rcos(theta)+1000sin(theta)) x2 = int(x0 - 1000*(-b)) # y2 stores the rounded off value of (rsin(theta)-1000cos(theta)) y2 = int(y0 - 1000*(a)) if not vertical and b == 1: # calculate the horizontal lines # cv2.line(Mat, (x1, y1), (x2, y2),(255, 255, 255), 1, cv2.CV_AA) lines_counted.append([x1, y1, x2, y2]) if vertical and theta == 0: # calculate the vertical lines only # cv2.line(Mat, (x1, y1), (x2, y2),(255, 255, 255), 1, cv2.CV_AA) lines_counted.append([x1, y1, x2, y2]) KLogger.set_log('RETURNING_VERT_HORZ_COUNTED_LINES200', 'Returing vertical and horizontal lines') return lines_counted
def get_mat_binary(self): ''' Method to get binary image ''' KLogger.set_log('BINARY204', 'Getting Binary Mat') return self.Mat_Thresh
def get_mat_rgb(self): ''' Method to get rgb image ''' KLogger.set_log('RGB204', 'Getting RGB Mat') return self.Mat_Rgb
def get_mat_gray(self): ''' Method to get gray image ''' KLogger.set_log('Gray204', 'Getting Gray Mat') return self.Mat_Gray
def set_mat_gray(self, mat_gray): ''' Method to set gray image ''' self.Mat_Gray = mat_gray KLogger.set_log('GRAY202', 'Setting GrayScale from method')
def set_mat_binary(self, mat_binary): ''' Method to set rgb image ''' self.Mat_Thresh = mat_binary KLogger.set_log('BINARY202', 'Setting Threshold/Binary from method')
def set_mat_rgb(self, mat_rgb): ''' Method to set rgb image ''' self.Mat_Rgb = mat_rgb KLogger.set_log('RGB202', 'Setting RGB from method')
def convert_to_zero_and_one(self): ''' Method to create binary image into zero and one ''' KLogger.set_log( 'ZERO2ONE404', 'Not implemented')
def image_thresholding(self, min_thresh, max_thresh, THRESH_CODE=cv2.THRESH_BINARY): self.Mat_Thresh = cv2.threshold( self.Mat_Gray, min_thresh, max_thresh, THRESH_CODE)[1] KLogger.set_log( 'THRESH200', f'Thresholding from {min_thresh} to {max_thresh}')
def convert_to_grayscale(self): ''' Convert an Image From RGB to GrayScale ''' self.Mat_Gray = cv2.cvtColor(self.Mat_Rgb, cv2.COLOR_BGR2GRAY) KLogger.set_log('GRAY200', 'Converting RGB to Grayscale')
def load_image(self, image_path): ''' Load image from the path into the Mat RGB ''' self.Image_Path = image_path self.Mat_Rgb = cv2.imread(self.Image_Path, cv2.IMREAD_COLOR) KLogger.set_log('RGB200', 'Reading RGB')