Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在OpenCV中检测线条?_Python_Opencv_Image Processing - Fatal编程技术网

Python 如何在OpenCV中检测线条?

Python 如何在OpenCV中检测线条?,python,opencv,image-processing,Python,Opencv,Image Processing,我正在尝试检测停车场中的线路,如下所示 我希望得到的是清晰的线和(x,y)在交叉线中的位置。然而,结果不是很有希望 我想这主要是因为两个原因: 有些线路断裂或缺失。甚至人眼也能看得很清楚 识别他们。即使HoughLine可以帮助连接一些缺失的 线,因为HoughLine有时会连接不必要的线 总的来说,我更愿意手工操作 有一些重复的台词 本工程的总管道如下所示: 1.选择一些特定的颜色(白色或黄色) 2.重复扩张和侵蚀,直到图像无法更改() 3.应用canny过滤线,并使用HoughL

我正在尝试检测停车场中的线路,如下所示

我希望得到的是清晰的线和(x,y)在交叉线中的位置。然而,结果不是很有希望

我想这主要是因为两个原因:

  • 有些线路断裂或缺失。甚至人眼也能看得很清楚 识别他们。即使HoughLine可以帮助连接一些缺失的 线,因为HoughLine有时会连接不必要的线 总的来说,我更愿意手工操作

  • 有一些重复的台词

  • 本工程的总管道如下所示:

    1.选择一些特定的颜色(白色或黄色)

    2.重复扩张和侵蚀,直到图像无法更改()

    3.应用canny过滤线,并使用HoughLinesP获取线


    我想知道为什么在选择某种颜色的第一步之后,线条就断了,而且有噪音。我认为在这一步中,我们应该做一些事情,使断线成为一条完整的、噪音更小的线。然后试着用一些东西来做Canny和Hough线。有什么想法吗?

    如果调整maxLineGap或内核的大小,会发生什么。或者,您可以找到线之间的距离。你必须通过线对,比如ax1,ay1到ax2,ay2 c.f.bx1,by1到bx2,by2,你可以找到与a成直角的梯度(-1相对于线的梯度)与b线相交的点。基础学校几何和联立方程,类似:

    x = (ay1 - by1) / ((by2 - by1) / (bx2 - bx1) + (ax2 - ax1) / (ay2 - ay1))
    # then
    y = by1 + x * (by2 - by1) / (bx2 - bx1)
    
    并将x,y与ax1,ay1进行比较


    PS您可能需要添加检查ax1、ay1和bx1之间的距离,by1,因为您的一些行看起来是其他行的延续,这些可能会被最近点技术消除。

    我不确定您到底在问什么,因为您的帖子中没有问题

    检测线段的一种好的、健壮的技术是LSD(线段检测器),它自openCV 3起就在openCV中可用

    这里有一些简单的C++基础代码,很容易转换为python:

    int main(int argc, char* argv[])
    {
        cv::Mat input = cv::imread("C:/StackOverflow/Input/parking.png");
        cv::Mat gray;
        cv::cvtColor(input, gray, CV_BGR2GRAY);
    
    
        cv::Ptr<cv::LineSegmentDetector> det;
        det = cv::createLineSegmentDetector();
    
    
    
        cv::Mat lines;
        det->detect(gray, lines);
    
        det->drawSegments(input, lines);
    
        cv::imshow("input", input);
        cv::waitKey(0);
        return 0;
    }
    
    intmain(intargc,char*argv[])
    {
    cv::Mat input=cv::imread(“C:/StackOverflow/input/parking.png”);
    cv::席灰色;
    cv::CVT颜色(输入,灰色,cv_bgr2灰色);
    cv::Ptr-det;
    det=cv::createLineSegmentDetector();
    cv::垫线;
    检测->检测(灰色、线条);
    数据->绘图段(输入,行);
    cv::imshow(“输入”,输入);
    cv::waitKey(0);
    返回0;
    }
    
    给出这个结果:


    哪个看起来比你的图像更适合进一步处理(没有线条重复等)。

    这是我的管道,也许它可以给你一些帮助

    首先,获取灰度图像并处理高斯模糊。

    img = cv2.imread('src.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    kernel_size = 5
    blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
    
    low_threshold = 50
    high_threshold = 150
    edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
    
    # Draw the lines on the  image
    lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
    
    其次,使用Canny进行边缘检测。

    img = cv2.imread('src.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    kernel_size = 5
    blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
    
    low_threshold = 50
    high_threshold = 150
    edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
    
    # Draw the lines on the  image
    lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
    
    然后,使用HoughLinesP获取线条。您可以调整参数以获得更好的性能

    rho = 1  # distance resolution in pixels of the Hough grid
    theta = np.pi / 180  # angular resolution in radians of the Hough grid
    threshold = 15  # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 50  # minimum number of pixels making up a line
    max_line_gap = 20  # maximum gap in pixels between connectable line segments
    line_image = np.copy(img) * 0  # creating a blank to draw lines on
    
    # Run Hough on edge detected image
    # Output "lines" is an array containing endpoints of detected line segments
    lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                        min_line_length, max_line_gap)
    
    for line in lines:
        for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5)
    
    最后,在图像上画线。

    img = cv2.imread('src.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    kernel_size = 5
    blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
    
    low_threshold = 50
    high_threshold = 150
    edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
    
    # Draw the lines on the  image
    lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
    
    这是我最后的表演

    最终图像:


    对于你问题的第一部分,这里有一些很好的答案,但对于第二部分(寻找直线交点),我看不到太多

    我建议你看看算法

    有一些python实现的算法和

    编辑:使用VeraPoseidon的Houghlines实现和此处链接的第二个库,我成功地获得了以下交叉点检测结果。这要归功于维拉和图书馆作者的出色工作。绿色正方形表示检测到的交点。有一些错误,但这对我来说似乎是一个非常好的起点。似乎您实际想要检测交叉口的大多数位置都检测到多个交叉口,因此您可能可以在图像上运行一个大小合适的窗口,查找多个交叉口,并将真实交叉口视为激活该窗口的位置

    下面是我用来生成该结果的代码:

    import cv2
    import numpy as np
    import isect_segments_bentley_ottmann.poly_point_isect as bot
    
    
    img = cv2.imread('parking.png')
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    
    kernel_size = 5
    blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
    
    low_threshold = 50
    high_threshold = 150
    edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
    
    rho = 1  # distance resolution in pixels of the Hough grid
    theta = np.pi / 180  # angular resolution in radians of the Hough grid
    threshold = 15  # minimum number of votes (intersections in Hough grid cell)
    min_line_length = 50  # minimum number of pixels making up a line
    max_line_gap = 20  # maximum gap in pixels between connectable line segments
    line_image = np.copy(img) * 0  # creating a blank to draw lines on
    
    # Run Hough on edge detected image
    # Output "lines" is an array containing endpoints of detected line segments
    lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]),
                        min_line_length, max_line_gap)
    print(lines)
    points = []
    for line in lines:
        for x1, y1, x2, y2 in line:
            points.append(((x1 + 0.0, y1 + 0.0), (x2 + 0.0, y2 + 0.0)))
            cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5)
    
    lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0)
    print(lines_edges.shape)
    #cv2.imwrite('line_parking.png', lines_edges)
    
    print points
    intersections = bot.isect_segments(points)
    print intersections
    
    for inter in intersections:
        a, b = inter
        for i in range(3):
            for j in range(3):
                lines_edges[int(b) + i, int(a) + j] = [0, 255, 0]
    
    cv2.imwrite('line_parking.png', lines_edges)
    
    您可以使用类似于此的代码块的策略来删除小区中的多个十字路口:

    for idx, inter in enumerate(intersections):
        a, b = inter
        match = 0
        for other_inter in intersections[idx:]:
            c, d = other_inter
            if abs(c-a) < 15 and abs(d-b) < 15:
                match = 1
                intersections[idx] = ((c+a)/2, (d+b)/2)
                intersections.remove(other_inter)
    
        if match == 0:
            intersections.remove(inter)
    
    对于idx,内部枚举(交叉点):
    a、 b=国际
    匹配=0
    对于交叉口内的其他交叉口[idx:]:
    c、 d=其他
    如果abs(c-a)<15且abs(d-b)<15:
    匹配=1
    交叉口[idx]=((c+a)/2,(d+b)/2)
    交叉点。移除(其他交叉点)
    如果匹配==0:
    交叉点。移除(内部)
    
    输出图像:

    不过,您必须使用窗口功能。

    我是初学者。 我得到了一些可能对这个问题有帮助的东西

    一种检测图像中线条的简单方法

    下面是在GoogleColab中执行的代码

    import cv2
    import numpy as np
    from google.colab.patches import cv2_imshow
    !wget  https://i.stack.imgur.com/sDQLM.png
    #read image 
    image = cv2.imread( "/content/sDQLM.png")
    
    #convert to gray
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    #performing binary thresholding
    kernel_size = 3
    ret,thresh = cv2.threshold(gray,200,255,cv2.THRESH_BINARY)  
    
    #finding contours 
    cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    
    #drawing Contours
    radius =2
    color = (30,255,50)
    cv2.drawContours(image, cnts, -1,color , radius)
    # cv2.imshow(image) commented as colab don't support cv2.imshow()
    cv2_imshow(image)
    # cv2.waitKey()
    
    

    检查这张纸:你不需要检测边缘,你可以直接在二值图像上使用
    HoughLinesP
    ,哟,检查我的更新答案。我认为这在某种程度上符合你想要的交叉口检测。交叉口?可能您可以按角度和距离对线段进行分组(此外,可能还有houghLines,但随后再次对分组线段进行操作),如果您知道停车位标记的总体外观,则可以执行透视校正,这将使比较和分组线段更容易。总的来说,应该有一些“格式塔理论”的方法,但afaik的研究还不算太远……然而,Python中不推荐使用LSDDetector,OpenCVT尚未测试,但EDLines可能会很好,太:如何在节点JS中执行相同的过程如何导入isect_segments_bentley_ottmann?您只需下载此处链接的库并将其添加到项目中即可。从那里引用它。