def run(self, name): kernel = np.ones((2, 2), np.uint8) self.img = self.convert_to_sqr(self.img) cv2.imwrite(self.dir + "/0-SquareImage.png", self.img) detected_lines = self.line_segmentation(self.img, '/1-DetectedLines') extended_lines = self.extend_lines(detected_lines) extended_lined_img = cv2.createLineSegmentDetector(1).drawSegments(self.img, extended_lines) cv2.imwrite(self.dir + "/2-ExtendedLines.png", extended_lined_img) lined_img = cv2.cvtColor(extended_lined_img, cv2.COLOR_BGR2GRAY) rotated_img = self.rotate_imge(lined_img, 90) cv2.imwrite(self.dir + "/3-rotated.png", rotated_img) detected_lines = self.line_segmentation(rotated_img, '/4-DetectedLines') extended2_lines = self.extend_lines(detected_lines) extended2_lined_img = cv2.createLineSegmentDetector(1).drawSegments(rotated_img, extended2_lines) cv2.imwrite(self.dir + "/5-ExtendedLines.png", extended2_lined_img) lined_img = cv2.cvtColor(extended2_lined_img, cv2.COLOR_BGR2GRAY) lined_img = self.convert_to_bw(lined_img, 200) lined_img = cv2.erode(lined_img, kernel, iterations=1) lined_img = cv2.morphologyEx(lined_img, cv2.MORPH_OPEN, kernel, iterations=5) # circles = self.circle_detection(lined_img) second_rotated_img = self.rotate_imge(lined_img, 270) cv2.imwrite(self.dir + "/6-MorphologyErodeOpen.png", second_rotated_img) components = self.connected_component(second_rotated_img) # original = self.transfer_to_original(components) # cv2.imshow('components' + name, components) # cv2.imwrite('output_%s.png' % name, components) cv2.imwrite(self.dir + '/6-output_%s.png' % name, components)
def detect_lines(source): current_frame = cv2.cvtColor(source, cv2.COLOR_BGR2GRAY) # detector = cv2.LineSegmentDetector() detector = cv2.createLineSegmentDetector() lines = detector.detect(current_frame)[0] return detector.drawSegments(source, lines)
def detectLine_LSD(src, minLength2=-1): """Summary LSD: Line Segment Detector Args: src (TYPE): Description Returns: TYPE: Description """ width, height = src.shape dst = np.zeros((width, height, 3), np.uint8) ls = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD) tup_results = ls.detect(cv2.medianBlur(src, 5)) lines, widths, _, _ = tup_results print widths if lines is not None: if minLength2 != -1: def length2(line): return (line[0][0] - line[0][2])**2 + (line[0][1] - line[0][3])**2 lines = np.array( filter(lambda line: length2(line) <= minLength2, lines)) print "# Lines: ", len(lines) ls.drawSegments(dst, lines) return dst
def render(img, lines, color=None, thick=None): drawn_img = np.copy(img) lines = np.array(lines) shape = lines.shape if len(shape) == 1: if shape[0] == 0: return drawn_img lines = lines.reshape([1, -1]) shape = lines.shape assert shape[1] == 2 or shape[1] == 4 if shape[1] == 2: for line in lines: rho, theta = line a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 3000 * (-b)) y1 = int(y0 + 3000 * (a)) x2 = int(x0 - 3000 * (-b)) y2 = int(y0 - 3000 * (a)) if color and thick: cv2.line(drawn_img, (x1, y1), (x2, y2), color, thick) else: cv2.line(drawn_img, (x1, y1), (x2, y2), (0, 255, 0), 4) if shape[1] == 4: lines = lines.astype('int') if color and thick: for x1, y1, x2, y2 in lines: cv2.line(drawn_img, (x1, y1), (x2, y2), color, thick) else: lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_NONE) drawn_img = lsd.drawSegments(drawn_img, lines) return drawn_img
def lsd(img): # img = cv2.GaussianBlur(img, (7,7), 0) # (5,5)表示的是卷积模板的大小,0表示的是沿x与y方向上的标准差 # . @param _refine The way found lines will be refined, see cv::LineSegmentDetectorModes # . @param _scale The scale of the image that will be used to find the lines. Range (0..1]. # . @param _sigma_scale Sigma for Gaussian filter. It is computed as sigma = _sigma_scale/_scale. # . @param _quant Bound to the quantization error on the gradient norm. # . @param _ang_th Gradient angle tolerance in degrees. # . @param _log_eps Detection threshold: -log10(NFA) \> log_eps. Used only when advance refinement # . is chosen. # . @param _density_th Minimal density of aligned region points in the enclosing rectangle. # . @param _n_bins Number of bins in pseudo-ordering of gradient modulus. lsd = cv2.createLineSegmentDetector(0) lines = lsd.detect(img) for dline in lines[0]: x0 = int(round(dline[0][0])) y0 = int(round(dline[0][1])) x1 = int(round(dline[0][2])) y1 = int(round(dline[0][3])) if distance(x0, y0, x1, y1) < 100: continue cv2.line(img, (x0, y0), (x1, y1), (255, 255, 255), 3) cv2.imwrite("debug/lines.jpg", img) # cv2.imshow('addWeighted', img) # cv2.waitKey(0) # cv2.destroyAllWindows() return img
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
def detect(img, time): # color grey gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #detection lsd_detector = cv2.createLineSegmentDetector() lines = (lsd_detector.detect(gray_img)[0]) if lines is None: pass else: lines = convert(lines) lines = longer_than(lines, 70) lines = list(lines) lines = [line for line in lines if abs(line[5]) > 0.2] lines = [line for line in lines if abs(line[4]) > 0.4] lines = np.array(lines) color = (0, 0, 255) color1 = (255, 0, 0) thickness = 1 thickness1 = 2 vanish_point = vanishing_point(lines, img) #print vanish_point if vanishing_point != None: cv2.circle(img, vanish_point, 5, color) draw_segments(img, lines, color, thickness1) draw_lines(img, lines, color1, thickness) return vanish_point
def calculate_major_angle(image_src): lines, width, prec, nfa = cv2.createLineSegmentDetector().detect(image_src) # line_len_threshold = max(image_src.shape[:2]) // 30 line_len_threshold = 8 filtered_lines = filter(lambda l: line_len(l) > line_len_threshold, map(lambda l: l[0], lines)) angles = [line_to_angle(line) for line in filtered_lines] hist = np.histogram(angles, bins=range(-90, 90)) sorted_angles = sorted(zip(hist[1], hist[0]), key=lambda x: -x[1]) sorted_angles = list(filter(lambda pair: pair[1] > 2, sorted_angles)) sorted_angles = sorted(sorted_angles[:10], key=lambda pair: pair[0]) min_angle = 5 negative_angles = list(filter(lambda x: x < -min_angle, [a[0] for a in sorted_angles])) positive_angles = list(filter(lambda x: x > min_angle, [a[0] for a in sorted_angles])) negative_candidate_angle = max(negative_angles or [None]) positive_candidate_angle = min(positive_angles or [None]) # print(negative_candidate_angle) # print(positive_candidate_angle) if negative_candidate_angle is None: major_angle = positive_candidate_angle elif positive_candidate_angle is None: major_angle = negative_candidate_angle elif abs(negative_candidate_angle + positive_candidate_angle) < min_angle: major_angle = (positive_candidate_angle - negative_candidate_angle) / 2 else: major_angle = min([positive_candidate_angle, -negative_candidate_angle]) return major_angle
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) edges = cv2.Canny((blur_gray * 255).astype(np.uint8), 10, 200, apertureSize=5) 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
def lsd_example(): image_filepath = '../../../data/machine_vision/build.png' # Read gray image. img = cv2.imread(image_filepath, cv2.IMREAD_GRAYSCALE) if img is None: print('Failed to load an image, {}.'.format(image_filepath)) return # Create default parametrization LSD. lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_ADV, 0.8) # Detect lines in the image. lines, width, prec, nfa = lsd.detect( img) # The shape of lines = (#lines, 1, 4). if lines is not None: print('#detected lines =', lines.shape[0]) # Draw detected lines in the image. drawn_img = lsd.drawSegments(img, lines) # Show image. cv2.imshow('LSD', drawn_img) cv2.waitKey(0) else: print('No line detected.')
def lineDetect(img, mode=0, threshold=-1, minLineLength=0, maxLineGap=0): img = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img lines = [] if mode == 0: # 霍夫直线检测 # 参数 rho:累加器距离分辨率, theta:累加器角度分辨率 threshold:投票阈值, minLineLength:直线的最小长度 maxLineGap:直线合并最大断开距离 datas = cv2.HoughLinesP(img, rho=1, theta=np.pi / 360., threshold=int(threshold), minLineLength=minLineLength, maxLineGap=maxLineGap) if datas is None: return lines for d in datas.reshape(-1, 4): line = GLine().initXY(*d) if line.getLen() <= 0: continue lines.append(line) elif mode == 1: # 直线检测器检测 # 直线检测器检测 cv2.createLineSegmentDetector # 参数1 refine(LSD_REFINE_STD,): 调优策略, cv2.LSD_REFINE_NONE:无调优策略, cv2.LSD_REFINE_STD:标准调优策略,将弧分解成多个小线段, cv2.LSD_REFINE_ADV:使用NFA指标评价检测直线,调整参数近一步调优检测直线 # 参数2 scale(0.8): 缩放图像比例 参数3 sigma_scale(0.6): 高斯模糊核sigma=sigma_scale/scale 参数4 quant(2.0):梯度量化误差 # 参数5 ang_th(22.5):直线段角度容差 参数6 log_eps(0): NFA检测阈值 参数7 density_th(0.7):直线段密度阈值 参数8 n_bins(1024):梯度归一化数量 lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD) datas, width, prec, nfa = lsd.detect(img) if datas is None and width is None and prec is None and nfa is None: print("检测失败,未能检测到直线");return lines datas = datas.reshape(-1, 4) width = width.reshape(-1).tolist() prec = prec.reshape(-1).tolist() for i in range(len(datas)): line = GLine().initXY(*datas[i]) if line.getLen() <= 0: continue line.width, line.prec = width[i], prec[i] lines.append(line) else: pass return lines
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
def get_weight_matrix(imA, imB, show=False): overlap = cv2.bitwise_and(imA, imB) gray = cv2.cvtColor(overlap, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY) if show: cv2.imshow("mask", mask) mask = cv2.dilate(mask, np.ones((2, 2), np.uint8), iterations=2) if show: cv2.imshow("mask_dilated", mask) mask_inv = cv2.bitwise_not(mask) imA_remain = cv2.bitwise_and(imA, imA, mask=mask_inv) grayA = cv2.cvtColor(imA_remain, cv2.COLOR_BGR2GRAY) ret, G = cv2.threshold(grayA, 0, 255, cv2.THRESH_BINARY) G = G.astype(np.float32) / 255.0 ind = np.where(mask == 255) lsd = cv2.createLineSegmentDetector(0) lines = lsd.detect(mask)[0] lines = sorted(lines, key=linelen, reverse=True)[1::-1] lx1, ly1, lx2, ly2 = lines[0][0] mx1, my1, mx2, my2 = lines[1][0] for y, x in zip(*ind): d1 = dline(x, y, lx1, ly1, lx2, ly2) d2 = dline(x, y, mx1, my1, mx2, my2) d1 *= d1 d2 *= d2 G[y, x] = d1 / (d1 + d2) return G
def lsd_lines(source_image, min_line_length=0.0375, max_line_length=1, min_precision=0): """LSD algorithm for line detection. Args: source_image: An OpenCV Image. min_line_length: Minimum line size. Specified as a percentage of the source image diagonal (0-1). max_line_length: Maximum line size. Specified as a percentage of the source image diagonal (0-1). min_precision: Minimum precision of detections. Returns: Array of line endpoints tuples (x1, y1, x2, y2). """ height, width = source_image.shape[:2] diagonal = math.sqrt(height**2 + width**2) min_line_length = min_line_length * diagonal max_line_length = max_line_length * diagonal detector = cv2.createLineSegmentDetector(cv2.LSD_REFINE_ADV) lines, rect_widths, precisions, false_alarms = detector.detect( source_image) line_lengths = [geom_tools.get_line_length(l[0]) for l in lines] return [ l[0] for (i, l) in enumerate(lines) if max_line_length > line_lengths[i] > min_line_length and precisions[i] > min_precision ]
def detectLine_LSD(src, minLength2=-1): """Summary LSD: Line Segment Detector Args: src (TYPE): Description Returns: TYPE: Description """ width, height = src.shape dst = np.zeros((width, height, 3), np.uint8) ls = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD) tup_results = ls.detect(cv2.medianBlur(src, 5)) lines, widths, _, _ = tup_results print widths if lines is not None: if minLength2 != -1: def length2(line): return (line[0][0] - line[0][2])**2 + (line[0][1] - line[0][3])**2 lines = np.array( filter( lambda line: length2(line) <= minLength2, lines ) ) print "# Lines: ", len(lines) ls.drawSegments(dst, lines) return dst
def detect_lines_floor(img): # color grey gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #detection lsd_detector = cv2.createLineSegmentDetector() lines = (lsd_detector.detect(gray_img)[0]) if lines is None: pass else: lines = convert(lines) lines = longer_than(lines, 70) lines = list(lines) lines = [line for line in lines if abs(line[5]) > 0.2] lines = [line for line in lines if abs(line[4]) > 0.4] lines = np.array(lines) color = (0, 0, 255) color1 = (255, 0, 0) thickness = 1 thickness1 = 2 vp = vanishing_point(lines, img) lines = list(lines) lines_left = [line for line in lines if line[4] > 0] lines_right = [line for line in lines if line[4] < 0] distances_right_to_vp = [ vp[0] * line[4] + vp[1] * line[5] + c for line in lines_right ] distances_left_to_vp = [ vp[0] * line[4] + vp[1] * line[5] + c for line in lines_left ] line_left = max(zip(distances_left_to_vp, lines_left))[1] line_right = max(zip(distances_right_to_vp, lines_right))[1] return [line_right, line_left]
def detect_lines(original, warped_edges): lsd = cv2.createLineSegmentDetector(0) lines = lsd.detect(warped_edges)[0] drawn = lsd.drawSegments(original, lines) cv2.imshow('lol', drawn) cv2.waitKey(0) for line in lines: print(line)
def doubleColumnDetector(img, yolov3_res): if yolov3_res.minx < 0: yolov3_res.minx = 0 if yolov3_res.miny < 0: yolov3_res.miny = 0 testImg = img[yolov3_res.miny:yolov3_res.maxy, yolov3_res.minx:yolov3_res.maxx, :] rows, cols = testImg.shape[:2] CENTER = (int(cols / 2), int(rows / 2)) if cols >= rows: RADIUS = int(cols / 15) else: RADIUS = int(rows / 15) grayImg = cv2.cvtColor(testImg, cv2.COLOR_RGB2GRAY) cannyImg = cv2.Canny(grayImg, 100, 130) detector = Detector() circles = cv2.HoughCircles(cannyImg, cv2.HOUGH_GRADIENT, 5, 10, param1=60, param2=5, minRadius=1, maxRadius=10) ###过滤出在预置圆内的圆 resCircles = detector.filteCircles(circles, CENTER, RADIUS * 2) meanCenter = [0, 0] if len(resCircles) != 0: resCircles = np.array(resCircles) centerCircles = resCircles[:, :2] meanCenter = np.mean(centerCircles, axis=0) # cv2.circle(testImg, (int(meanCenter[0]), int(meanCenter[1])), 5, (255, 255, 0), 2) else: print("no detect circles...") ### 直线检测 lsd = cv2.createLineSegmentDetector(0) lines = lsd.detect(cannyImg)[0] oriImgResLines = [] angleDiff = 90 if lines is None: print("no detector line.....") else: lines1 = lines[:, 0, :] # 提取为二维 lenLines = detector.filteLineWithLength(lines1, RADIUS) ###根据预置圆过滤 cirLines = detector.filteLineWithCircle(lenLines, CENTER, RADIUS * 5) # print("cirLines num: ", len(cirLines)) angleLines = detector.fiteWithAngle(cirLines, rows, cols) # print("angleLines num: ", len(angleLines)) # LineTools.drawLines(testImg, angleLines) testImgResLines, angleDiff = detector.resLines(angleLines, meanCenter, RADIUS * 5) # LineTools.drawLines(testImg, testImgResLines) oriImgResLines = originResLines(testImgResLines, yolov3_res) oriImgResStatus = " (ANGLE: " + str(angleDiff) + ")" return oriImgResStatus, oriImgResLines
def _LSDLine(self, edge): lsd = cv2.createLineSegmentDetector(_refine=cv2.LSD_REFINE_ADV) # Lines, width, precision, number of false alarms lines, width, prec, nfa = lsd.detect(edge) if lines is not None: lines = np.array(lines[:, 0]) else: lines = [] return lines
def __init__(self): self.pub = rospy.Publisher('/centroids', String, queue_size=1) self.pubForBag = rospy.Publisher('/imgForBag/front/compressed', CompressedImage, queue_size=1) self.subscriber = rospy.Subscriber("/drone/front/compressed",CompressedImage, self.callback) self.lsd = cv2.createLineSegmentDetector(0) self.gcx = 0 self.gcy = 0 self.length = 100 self.br = CvBridge()
def detect_lines(img: Any, debug: bool = True) -> Any: img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) lsd = cv2.createLineSegmentDetector(0) lines = lsd.detect(img)[0] if debug: drawn_img = lsd.drawSegments(img, lines) show_image(drawn_img, wait=True) return lines
def line_detector(self, img): lsd = cv2.createLineSegmentDetector(0) lines = lsd.detect(img)[ 0] #Position 0 of the returned tuple are the detected lines res = [] print('Detecting Lines') for l in lines[0]: x2, y2, x1, y1 = l res.append('Line({},{},{},{})'.format(x2, y2, x1, y1)) return res
def LSD(filename, output_directory, isPath=True): img = filename if (isPath): img = cv2.imread(filename) detector = cv2.createLineSegmentDetector(0) lines = detector.detect(img) drawLines(img, output_directory + "lsd_lines.jpg", lines[0]) return lines[0]
def __init__(self, K, image, model, instance_frame): self.K = K kernel = np.ones((3, 3), np.float32)/9.0 self.image = cv2.filter2D(image, -1, kernel) self.gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY) self.img_size = (640, 480) self.model = model self.instance_frame = instance_frame self.lsd = cv2.createLineSegmentDetector(0) self.gains = [10, 10, 10, 50, 50, 50]
def extract_lines_lsd(contour_mask): lsd = cv2.createLineSegmentDetector() contour_mask_copy = contour_mask.copy() contour_mask_copy[contour_mask_copy > 0] = 255 lines = lsd.detect(contour_mask_copy)[0] lens = [euc_dis(line[0, :2].tolist(), line[0, 2:].tolist()) for line in lines] lens = np.array(lens) # remove extreme short lines. lines = lines[lens > 5] return lines
def __init__(self, image, K, model, instance_frame, ariadne, debug=False): self.image = image self.K = K self.im_size = (640, 480) self.debug = debug self.output_image = ariadne.graph.generateBoundaryImage(image) self.ariadne = ariadne self.model = model self.instance_frame = instance_frame self.lsd = cv2.createLineSegmentDetector(0) self.gains = np.array([1.0, 1, 1, 5, 5, 5, 5]) * 0.1
def main(argv): default_file = 'sudoku.png' filename = argv[0] if len(argv) > 0 else default_file # Loads an image src = cv.imread(cv.samples.findFile(filename), cv.IMREAD_GRAYSCALE) # Check if image is loaded fine if src is None: print('Error opening image!') print('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n') return -1 dst = cv.Canny(src, 50, 200, None, 3) # Copy edges to the images that will display the results in BGR cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR) cdstP = np.copy(cdst) lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0) for l in lines: print(l) if lines is not None: for i in range(0, len(lines)): rho = lines[i][0][0] theta = lines[i][0][1] a = math.cos(theta) b = math.sin(theta) x0 = a * rho y0 = b * rho pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a))) pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a))) cv.line(cdst, pt1, pt2, (0, 0, 255), 3, cv.LINE_AA) linesP = cv.HoughLinesP(dst, 1, np.pi / 180, 50, None, 50, 10) if linesP is not None: for i in range(0, len(linesP)): l = linesP[i][0] cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 3, cv.LINE_AA) retval = cv.createLineSegmentDetector() _lines, width, prec, nfa = cv.LineSegmentDetector.detect(dst) cv.imshow("Source", src) cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst) cv.imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP) cv.waitKey() return 0
def layoutToDump(dumpMatr, lines): height, width = dumpMatr.shape blank = blankImage(height, width, (0, 0, 0)) LSD = cv2.createLineSegmentDetector(0) layout = LSD.drawSegments(blank, lines) for i in range(0, height): for j in range(0, width): if layout[i][j][2] != 0 or layout[i][j][1] != 0 or layout[i][j][ 0] != 0: dumpMatr[i, j] = dumpMatr[i, j] + 1 return dumpMatr
def __init__(self): #self.velocity_publisher = rospy.Publisher("/cmd_vel", Twist, queue_size=1) self.img_pub = rospy.Publisher('/image_center', Image, queue_size=1) #self.modesub = rospy.Subscriber('/mode_msg',mode_msg, self.modemsg) #self.twistpub = rospy.Publisher('/mode_twist', twist, queue_size=10) self.lanepub = rospy.Publisher('/lane_msg', msg_lane, queue_size=1) self.lanemsg = msg_lane() self.lsd = cv2.createLineSegmentDetector(0) #self.vel_msg = twist() self.cap = None self.frame = None self.hsv = None
def lsd(img): # 不改变原图 rt_img = np.zeros(img.shape, dtype=np.uint8) lsd = cv2.createLineSegmentDetector() # parameters can be set lines = lsd.detect(img) # lsd.drawSegments(rt_img, lines) for line in lines[0]: x0 = int(round(line[0][0])) y0 = int(round(line[0][1])) x1 = int(round(line[0][2])) y1 = int(round(line[0][3])) cv2.line(rt_img, (x0, y0), (x1, y1), 255, 1, cv2.LINE_8) return rt_img
def detect(img, time): """ detecter les lignes et le point de fuite puis img: image time=temps voir exemple 3""" gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #detection de lignes lsd_detector = cv2.createLineSegmentDetector() lines = (lsd_detector.detect(gray_img)[0]) if lines is None: pass else: #detecter le point de fuite lines = convert(lines) lines = longer_than(lines, 70) lines = list(lines) lines = [line for line in lines if abs(line[5]) > 0.2] lines = [line for line in lines if abs(line[4]) > 0.4] lines = np.array(lines) color = (0, 0, 255) color1 = (255, 0, 0) thickness = 1 thickness1 = 2 vanish_point = vision_lib.vanishing_point.vanishing_point(lines, img) vp = vanish_point #chercher les lignes au sol a gauche et a droite et notamment les plus proches du point de fuite if vanish_point is not None: cv2.circle(img, vanish_point, 5, color) lines_left = [line for line in lines if line[4] < 0] lines_right = [line for line in lines if line[4] > 0] distances_right_to_vp = [ vp[0] * line[4] + vp[1] * line[5] + line[6] for line in lines_right ] distances_left_to_vp = [ vp[0] * line[4] + vp[1] * line[5] + line[6] for line in lines_left ] line_left, line_right = [], [] if len(lines_left) > 0: line_left = max(zip(distances_left_to_vp, lines_left))[1] if len(lines_right) > 0: line_right = max(zip(distances_right_to_vp, lines_right))[1] else: return #dessiner les lignes et renvoyer les poitns de fuite draw_segments(img, lines, color, thickness1) draw_lines(img, lines, color1, thickness) return (vanish_point, [line_right, line_left])
def __find_lines(input): """Finds all line segments in an image. Args: input: A numpy.ndarray. Returns: A filtered list of Lines. """ detector = cv2.createLineSegmentDetector() if (len(input.shape) == 2 or input.shape[2] == 1): lines = detector.detect(input) else: tmp = cv2.cvtColor(input, cv2.COLOR_BGR2GRAY) lines = detector.detect(tmp) output = [] if len(lines) != 0: for i in range(1, len(lines[0])): tmp = GripPipeline.Line(lines[0][i, 0][0], lines[0][i, 0][1], lines[0][i, 0][2], lines[0][i, 0][3]) output.append(tmp) return output
def main(): # get all the arguments arg_parser = argparse.ArgumentParser(description="Generate line segment data file from ROS map server data") arg_parser.add_argument("map_yaml_path", help="path to the map server yaml file") arg_parser.add_argument("output_path", help="path of the directory to output the line segment map files (.yaml and .dat files)") arg_parser.add_argument("-f", "--filename", dest="filename", default="map_lines", help="name of the line segment map files (extensions .yaml and .dat will be added automatically)") arg_parser.add_argument("-i", "--output-image", dest="output_image", action='store_true', help="Output image with line segments drawn on top of the map image") arg_parser.add_argument("-d", "--definition", dest="definition", choices=["low", "med", "high"], default="low", help="how detailed you want the linesegments to represent the map") args = arg_parser.parse_args() map_yaml_path = os.path.abspath(args.map_yaml_path) output_path = os.path.abspath(args.output_path) filename = args.filename output_image = args.output_image definition = args.definition # extract data from map server format node = yaml.load(open(map_yaml_path, "r")) image_path = os.path.join(os.path.dirname(map_yaml_path), node["image"]) scale = node["resolution"] origin = node["origin"] occupied_thresh = node["occupied_thresh"] print("") print("From input map yaml file: ") print(" image: %s" % image_path) print(" scale: %f" % scale) print(" origin: [%f, %f, %f]" % (origin[0], origin[1], origin[2])) print("occupied_thresh: %f" % occupied_thresh) output_yaml_path = os.path.join(output_path, filename + ".yaml") output_lines_path = os.path.join(output_path, filename + ".dat") output_image_path = os.path.join(output_path, filename + ".png") print("") print("Output files are:") print(" map: %s" % output_yaml_path) print("lines: %s" % output_lines_path) if output_image: print("Image: %s" % output_image_path) print("") print("Processing...") # threshold the image raw_img = cv2.imread(image_path, 0) img = cv2.inRange(raw_img, occupied_thresh * 255, 255.0) # cv2.createLineSegmentDetector([_refine[, _scale[, _sigma_scale[, _quant[, _ang_th[, _log_eps[, _density_th[, _n_bins]]]]]]]]) # Read the paper from http://www.ipol.im/pub/art/2012/gjmr-lsd/ to get an understanding of the parameters # play around with the parameters to obtain the desired results # _refine: does not have much of a effect, using standard should be fine # _scale (default = 0.8) # _sigma_scale (default = 0.6) # _quant (default = 2) # _ang_th (default = 22.5) # _log_eps (default = 1) # _density_th (default = 0.7) # _n_bins (default = ?) if definition == "low": lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD, 1.05, 1, 1, 22.5, 1, 0.8) elif definition == "med": lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD, 1.5, 1, 1, 22.5, 1, 0.8) elif definition == "high": lsd = cv2.createLineSegmentDetector(cv2.LSD_REFINE_STD, 2, 1, 1, 22.5, 1, 0.8) lines = lsd.detect(img)[0] print("%d line segments generated\n" % len(lines)) # generate the line segment file output_lines_file = open(output_lines_path, "w") rows = np.size(img, 0) for line in lines: line = line[0] output_lines_file.write("%25.15f %25.15f %25.15f %25.15f\n" % (line[0], rows - line[1], line[2], rows - line[3])) output_lines_file.close() # generate the map yaml file output_yaml = {} output_yaml["type"] = "line_segments" output_yaml["data"] = os.path.basename(output_lines_path) output_yaml["scale"] = scale output_yaml["origin"] = origin yaml.dump(output_yaml, open(output_yaml_path, "w")) #Draw detected lines in the image if output_image: drawn_img = lsd.drawSegments(img, lines) cv2.imwrite(output_image_path, drawn_img)
# @File : LineSegmentDetector1.py # @Software: PyCharm """ LineSegmentDetector1.py: """ import cv2 import numpy as np # Read gray image img0 = cv2.imread("pokerQ.jpg") img = cv2.cvtColor(img0,cv2.COLOR_BGR2GRAY) cv2.imshow('pokerQ',img0) # Create default parametrization LSD lsd = cv2.createLineSegmentDetector(0) # Detect lines in the image dlines = lsd.detect(img)#TODO 返回什么? lines = lsd.detect(img)[0] # Position 0 of the returned tuple are the detected lines # Draw detected lines in the image # drawn_img = lsd.drawSegments(img, lines) # cv2.waitKey(0) for dline in dlines[0]: x0 = int(round(dline[0][0])) y0 = int(round(dline[0][1])) x1 = int(round(dline[0][2])) y1 = int(round(dline[0][3]))