如何在openCV python中使用HoughLines变换准确检测线条?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36209310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect lines accurately using HoughLines transform in openCV python?
提问by Sanj
I am a newbie in both python
and opencv
and I am facing a problem in detecting lines in the following image, which has strips of black lines laid on the ground:
我是新手在两个python
和opencv
,我在检测以下图像,其具有放置在地面黑线带中的行面临的一个问题:
I used the following code:
我使用了以下代码:
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)
but it is unable to detect the lines accurately and only draws a green line on the first black strip from the bottom which does not even cover the entire line,
also,
please suggest a way of obtaining the y
cordinates of each line.
但它无法准确检测线条,只能在从底部开始的第一个黑色条上画一条绿色线条,甚至没有覆盖整条线条,
另外,
请提出一种获取y
每条线条坐标的方法。
回答by tfv
Sanj,
山治,
a modified code which detects not one but many Hough lines is shown below. I have improved the way how to loop through the lines array so that you get many more line segments.
下面显示了一种修改后的代码,它检测的不是一个而是许多霍夫线。我改进了如何循环遍历行数组的方式,以便您获得更多的线段。
You can further tune the parameters, however, I think that the contour approach in your other post will most likely be the better approach to solve your task, as shown there: How to detect horizontal lines in an image and obtain its y-coordinates using python and opencv?
您可以进一步调整参数,但是,我认为您另一篇文章中的轮廓方法很可能是解决您的任务的更好方法,如下所示: 如何检测图像中的水平线并使用以下方法获取其 y 坐标python和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()
回答by Vimukthi Gunasekara
I tried to extract horizontal and vertical lines in an image.So we can use morphological operations for this.It'll be the best thing for this problem.Try it.
我试图提取图像中的水平线和垂直线。所以我们可以使用形态学操作。这将是解决这个问题的最好方法。试试吧。
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;