From 9922f02d57e1c7778505e3f3eeedbcbaf550e403 Mon Sep 17 00:00:00 2001 From: joaofauvel Date: Wed, 1 Jul 2020 11:14:34 -0300 Subject: [PATCH 1/4] Multiple fixes to work with pylsd and opencv >4 --- scan.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/scan.py b/scan.py index 1a0daea..dfb75d2 100644 --- a/scan.py +++ b/scan.py @@ -17,6 +17,7 @@ import itertools import math import cv2 +from pylsd.lsd import lsd import argparse import os @@ -92,8 +93,7 @@ def get_corners(self, img): This is a utility function used by get_contours. The input image is expected to be rescaled and Canny filtered prior to be passed in. """ - lsd = cv2.createLineSegmentDetector() - lines = lsd.detect(img)[0] + lines = lsd(img) # massages the output from LSD # LSD operates on edges. One "line" has 2 edges, and so we need to combine the edges back into lines @@ -112,7 +112,7 @@ def get_corners(self, img): horizontal_lines_canvas = np.zeros(img.shape, dtype=np.uint8) vertical_lines_canvas = np.zeros(img.shape, dtype=np.uint8) for line in lines: - x1, y1, x2, y2 = line + x1, y1, x2, y2, _ = line if abs(x2 - x1) > abs(y2 - y1): (x1, y1), (x2, y2) = sorted(((x1, y1), (x2, y2)), key=lambda pt: pt[0]) cv2.line(horizontal_lines_canvas, (max(x1 - 5, 0), y1), (min(x2 + 5, img.shape[1] - 1), y2), 255, 2) @@ -123,8 +123,7 @@ def get_corners(self, img): lines = [] # find the horizontal lines (connected-components -> bounding boxes -> final lines) - contours = cv2.findContours(horizontal_lines_canvas, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) - contours = contours[1] + (contours, hierarchy) = cv2.findContours(horizontal_lines_canvas, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) contours = sorted(contours, key=lambda c: cv2.arcLength(c, True), reverse=True)[:2] horizontal_lines_canvas = np.zeros(img.shape, dtype=np.uint8) for contour in contours: @@ -139,8 +138,7 @@ def get_corners(self, img): corners.append((max_x, right_y)) # find the vertical lines (connected-components -> bounding boxes -> final lines) - contours = cv2.findContours(vertical_lines_canvas, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) - contours = contours[1] + (contours, hierarchy) = cv2.findContours(vertical_lines_canvas, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) contours = sorted(contours, key=lambda c: cv2.arcLength(c, True), reverse=True)[:2] vertical_lines_canvas = np.zeros(img.shape, dtype=np.uint8) for contour in contours: @@ -227,7 +225,7 @@ def get_contour(self, rescaled_image): # also attempt to find contours directly from the edged image, which occasionally # produces better results - (_, cnts, hierarchy) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + (cnts, hierarchy) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] # loop over the contours From 2eecb37e0d7f52e8d5516cd7c8c732008a895f3b Mon Sep 17 00:00:00 2001 From: joaofauvel Date: Wed, 1 Jul 2020 11:16:05 -0300 Subject: [PATCH 2/4] Removed deprecated mlab method matplotlib.mlab.dist_point_to_segment() --- polygon_interacter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/polygon_interacter.py b/polygon_interacter.py index af5a638..66b3e1e 100644 --- a/polygon_interacter.py +++ b/polygon_interacter.py @@ -1,7 +1,6 @@ import numpy as np from matplotlib.lines import Line2D from matplotlib.artist import Artist -from matplotlib.mlab import dist_point_to_segment class PolygonInteractor(object): From b49b9bf6cc97f4e8113b7c6cc272b6f84928b6f4 Mon Sep 17 00:00:00 2001 From: joaofauvel Date: Wed, 1 Jul 2020 11:27:17 -0300 Subject: [PATCH 3/4] requirements --- requirements.txt | Bin 0 -> 196 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..6c24c6952d5d26673b6fe46a4fbfa2b2aef386ae GIT binary patch literal 196 zcmY+7TMEKZ3`Ebj;8K)6!5TZ_fp2YrE?w@%XjNSGufCz(R<}o+r9NYGkj8t5x)sMW@85@{1wQrGFQ+n_NPyH-e4|3+E%g)(M SoH_4&<` Date: Wed, 1 Jul 2020 12:32:17 -0300 Subject: [PATCH 4/4] Change dilate to closing --- scan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scan.py b/scan.py index dfb75d2..4aa835e 100644 --- a/scan.py +++ b/scan.py @@ -189,7 +189,7 @@ def get_contour(self, rescaled_image): # dilate helps to remove potential holes between edge segments kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(MORPH,MORPH)) - dilated = cv2.dilate(gray, kernel) + dilated = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel) # find edges and mark them in the output map using the Canny algorithm edged = cv2.Canny(dilated, 0, CANNY)