直线检测
霍夫直线变换介绍
霍夫直线检测原理
通过直角坐标和极坐标的对应关系,在一条直线上的点所产生的sin曲线在极坐标区域会相交于同一个点(r, theta),将这个(r,theta)对应到直角坐标就可以得到一条直线。
code and API
- HoughLines(image,rho,theta,threshold ,lines ,srn ,stn ,min_theta ,max_theta):
def line_detection(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY);
edge = cv.Canny(gray, 50, 150, apertureSize = 3);
lines = cv.HoughLines(edge, 1, np.pi/180, 150);
#get pieces of (r,theta);
for line in lines:
rho, theta = line[0];
a = np.cos(theta);
b = np.sin(theta);
x0 = a * rho;
y0 = b * rho;
x1 = int(x0 + 1000 * (-b)); #必须乘1000
y1 = int(y0 + 1000 * (a) );
x2 = int(x0 - 1000 * (-b));
y2 = int(y0 - 1000 * (a) );
cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2);
cv.imshow("image_line", image);
- HoughLinesP( image, rho, theta, threshold, lines, minLineLength, maxLineGap)
def line_detect_possible_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY);
edge = cv.Canny(gray, 50, 150, apertureSize = 3);
lines = cv.HoughLinesP(edge, 1, np.pi/180, 200, minLineLength = 50, maxLineGap =10);
#minLineLength, maxLineGap
for line in lines:
print(type(line));
x1, y1, x2, y2 = line[0];#line这个东西本来就只有一行!!!!
cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2);
cv.imshow("line_detect_possible_demo", image);