如何在openCV python中使用HoughLines变换精确检测线条?

时间:2016-03-24 20:19:43

标签: python numpy opencv3.0 hough-transform

我是python opencv 的新手,我在检测以下图片中的线条时遇到问题,图片中有黑色线条。地:

enter image description here

我使用了以下代码:

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 坐标的方法。

2 个答案:

答案 0 :(得分:8)

Sanj,

下面显示了一个检测到不是一条但是多条Hough线的修改代码。我已经改进了如何遍历线阵列的方式,以便您获得更多的线段。

您可以进一步调整参数,但是,我认为您的其他帖子中的轮廓方法很可能是解决任务的更好方法,如下所示: 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()

答案 1 :(得分:0)

我试图在图像中提取水平和垂直线。所以我们可以使用形态学操作。这对于这个问题是最好的。试试吧。

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;
相关问题