Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to get it working with pylsd and opencv >4 #8

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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