霍夫变换
霍夫直线变换
霍夫直线变换用来做直线检测
绘制经过某点的所有直线的示例代码如下
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
def draw_line():
# 绘制一张黑图
img = np.zeros((500, 500, 1), np.uint8)
# 绘制一个点
cv.line(img, (10, 10), (10, 10), (255), 1)
cv.imshow("line", img)
return img
def hough_lines(img):
rho = 1;
theta = np.pi / 180
threshold = 0
lines = cv.HoughLines(img, rho, theta, threshold)
dst_img = img.copy()
for line in lines[:, 0]:
rho, theta = line
a = np.cos(theta)
b = np.sin(theta)
x = a * rho
y = b * rho
x1 = int(np.round(x + 1000 * (-b)))
y1 = int(np.round(y + 1000 * a))
x2 = int(np.round(x - 1000 * (-b)))
y2 = int(np.round(y - 1000 * a))
cv.line(dst_img, (x1, y1), (x2