如何在 openCV python 中使用 HoughLines 变换准确检测线条?
How to detect lines accurately using HoughLines transform in openCV python?
我在 python
和 opencv
方面都是新手,我在检测下图中的线条时遇到了问题,其中有条带铺在地上的黑线:
我使用了以下代码:
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
坐标的方法。
桑吉,
下面显示了一个修改后的代码,它检测的不是一条霍夫线,而是多条霍夫线。我改进了如何循环遍历线数组的方式,以便您获得更多的线段。
您可以进一步调整参数,但是,我认为您的另一个 post 中的等高线方法很可能是解决您的任务的更好方法,如下所示:
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()
我试着在一个image.So中提取水平和垂直线我们可以使用形态学操作this.It这将是最好的problem.Try它。
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;
我在 python
和 opencv
方面都是新手,我在检测下图中的线条时遇到了问题,其中有条带铺在地上的黑线:
我使用了以下代码:
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
坐标的方法。
桑吉,
下面显示了一个修改后的代码,它检测的不是一条霍夫线,而是多条霍夫线。我改进了如何循环遍历线数组的方式,以便您获得更多的线段。
您可以进一步调整参数,但是,我认为您的另一个 post 中的等高线方法很可能是解决您的任务的更好方法,如下所示:
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()
我试着在一个image.So中提取水平和垂直线我们可以使用形态学操作this.It这将是最好的problem.Try它。
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;