本文整理汇总了Python中cv2.createLineSegmentDetector方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.createLineSegmentDetector方法的具体用法?Python cv2.createLineSegmentDetector怎么用?Python cv2.createLineSegmentDetector使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.createLineSegmentDetector方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lsdWrap
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createLineSegmentDetector [as 别名]
def lsdWrap(img, LSD=None, **kwargs):
'''
Opencv implementation of
Rafael Grompone von Gioi, Jérémie Jakubowicz, Jean-Michel Morel, and Gregory Randall,
LSD: a Line Segment Detector, Image Processing On Line, vol. 2012.
[Rafael12] http://www.ipol.im/pub/art/2012/gjmr-lsd/?utm_source=doi
@img
input image
@LSD
Constructing by cv2.createLineSegmentDetector
https://docs.opencv.org/3.0-beta/modules/imgproc/doc/feature_detection.html#linesegmentdetector
if LSD is given, kwargs will be ignored
@kwargs
is used to construct LSD
work only if @LSD is not given
'''
if LSD is None:
LSD = cv2.createLineSegmentDetector(**kwargs)
if len(img.shape) == 3:
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
lines, width, prec, nfa = LSD.detect(img)
if lines is None:
return np.zeros_like(img), np.array([])
edgeMap = LSD.drawSegments(np.zeros_like(img), lines)[..., -1]
lines = np.squeeze(lines, 1)
edgeList = np.concatenate([lines, width, prec, nfa], 1)
return edgeMap, edgeList
示例2: robust_edge_detection
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createLineSegmentDetector [as 别名]
def robust_edge_detection(img):
# Find edges
kernel_size = 5
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur_gray = cv2.GaussianBlur(gray, (kernel_size, kernel_size), 0)
# io.imagesc(blur_gray)
edges = cv2.Canny((blur_gray * 255).astype(np.uint8), 10, 200, apertureSize=5)
# io.imagesc(edges)
lsd = cv2.createLineSegmentDetector(0)
lines = lsd.detect(edges)[0] # Position 0 of the returned tuple are the detected lines
long_lines = []
for j in range(lines.shape[0]):
x1, y1, x2, y2 = lines[j, 0, :]
if np.linalg.norm(np.array([x1, y1]) - np.array([x2, y2])) > 50:
long_lines.append(lines[j, :, :])
lines = np.array(long_lines)
edges = 1 * np.ones_like(img)
drawn_img = lsd.drawSegments(edges, lines)
edges = (drawn_img[:, :, 2] > 1).astype(np.float32)
kernel = np.ones((7, 7), np.uint8)
edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
kernel = np.ones((3, 3), np.uint8)
edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
return edges
示例3: __detect_lines
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import createLineSegmentDetector [as 别名]
def __detect_lines(self, img):
"""
Detects lines using OpenCV LSD Detector
"""
# Convert to grayscale if required
if len(img.shape) == 3:
img_copy = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
else:
img_copy = img
# Create LSD detector with default parameters
lsd = cv2.createLineSegmentDetector(0)
# Detect lines in the image
# Returns a NumPy array of type N x 1 x 4 of float32
# such that the 4 numbers in the last dimension are (x1, y1, x2, y2)
# These denote the start and end positions of a line
lines = lsd.detect(img_copy)[0]
# Remove singleton dimension
lines = lines[:, 0]
# Filter out the lines whose length is lower than the threshold
dx = lines[:, 2] - lines[:, 0]
dy = lines[:, 3] - lines[:, 1]
lengths = np.sqrt(dx * dx + dy * dy)
mask = lengths >= self._length_thresh
lines = lines[mask]
# Store the lines internally
self.__lines = lines
# Return the lines
return lines