【问题标题】:How to detect lines accurately using HoughLines transform in openCV python?如何在openCV python中使用HoughLines变换准确检测线条?
【发布时间】:2016-03-24 20:19:42
【问题描述】:

我是 python 和 opencv 的新手,我在检测下图中的线条时遇到了问题,在地面上有黑色线条: p>

我使用了以下代码:

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength = img.shape[1]-1
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
    cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

但它无法准确检测线条,仅在底部的第一个黑条上画一条绿线,甚至没有覆盖整条线,
还有,
请提出一种获取每行y坐标的方法。

【问题讨论】:

  • 这些线条不是很直。我发现我自己的霍夫变换代码比使用固定解决方案给了我更多的洞察力(因为这个问题似乎是关于理解的)。查看一个简单的家庭旋转霍夫变换示例,例如这里:nabinsharma.wordpress.com/2012/12/26/…如果你自己可视化变换的输出,你会很快看到输入线的质量是否起作用(变换空间中的峰值更宽,最大值在哪里不太清楚)。

标签: python numpy opencv3.0 hough-transform


【解决方案1】:

桑杰,

下面显示了一个修改后的代码,它检测的不是一条而是多条霍夫线。我改进了如何循环遍历 lines 数组的方式,以便获得更多的线段。

您可以进一步调整参数,但是,我认为您其他帖子中的轮廓方法很可能是解决您的任务的更好方法,如下所示: How to detect horizontal lines in an image and obtain its y-coordinates using python and opencv?

import numpy as np
import cv2

img = cv2.imread('lines.jpg')

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength=img.shape[1]-300
lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500, threshold=10,lines=np.array([]), minLineLength=minLineLength,maxLineGap=100)

a,b,c = lines.shape
for i in range(a):
    cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)


cv2.imshow('edges', edges)
cv2.imshow('result', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

【讨论】:

    【解决方案2】:

    我尝试提取图像中的水平线和垂直线。因此我们可以对此使用形态学运算。这将是解决这个问题的最佳方法。试试吧。

    Mat img = imread(argv[1]);
    
    if(!src.data)
        cerr << "Problem loading image!!!" << endl;
    
    imshow("img .jpg", img);
    
    cvtColor(img, gray, CV_BGR2GRAY);
    imshow("gray", gray);
    
    
    Mat binary_image;
    adaptiveThreshold(gray, binary_image, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
    imshow("binary.jpg", binary_image);
    
    // Create the images that will use to extract the horizontal and vertical lines
    Mat horizontal = binary_image.clone();
    Mat vertical = binary_image.clone();
    
    int horizontalsize = horizontal.cols / 30;
    
    Mat horizontalStructure = getStructuringElement(MORPH_RECT, Size(horizontalsize,1));
    
    
    erode(horizontal, horizontal, horizontalStructure, Point(-1, -1));
    dilate(horizontal, horizontal, horizontalStructure, Point(-1, -1));
    imshow("horizontal", horizontal);
    
    int verticalsize = vertical.rows / 30;
    
    Mat verticalStructure = getStructuringElement(MORPH_RECT, Size( 1,verticalsize));
    
    erode(vertical, vertical, verticalStructure, Point(-1, -1));
    dilate(vertical, vertical, verticalStructure, Point(-1, -1));
    
    imshow("vertical", vertical);
    
    bitwise_not(vertical, vertical);
    imshow("vertical_bit", vertical);
    
    
    Mat edges;
    adaptiveThreshold(vertical, edges, 255, CV_ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, -2);
    imshow("edges", edges);
    
    Mat kernel = Mat::ones(2, 2, CV_8UC1);
    dilate(edges, edges, kernel);
    imshow("dilate", edges);
    
    Mat smooth;
    vertical.copyTo(smooth);
    
    blur(smooth, smooth, Size(2, 2));
    
    smooth.copyTo(vertical, edges);
    
    imshow("smooth", vertical);
    waitKey(0);
    return 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 1970-01-01
      • 2017-02-06
      • 2020-01-17
      相关资源
      最近更新 更多