Skip to content

Commit

Permalink
Merge pull request #8 from joaofauvel/master
Browse files Browse the repository at this point in the history
Fixes to get it working with pylsd and opencv >4
  • Loading branch information
andrewdcampbell authored Jan 13, 2021
2 parents 412fbd2 + efdcd70 commit d3683eb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
1 change: 0 additions & 1 deletion polygon_interacter.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
Binary file added requirements.txt
Binary file not shown.
16 changes: 7 additions & 9 deletions scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import itertools
import math
import cv2
from pylsd.lsd import lsd

import argparse
import os
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -191,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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d3683eb

Please sign in to comment.