CV算法 (一):霍夫变换
霍夫变换可以实现 点到直线,直线到到点的转换,利用这一特性,我们可以通过霍夫变换检测直线,圆圈等
一、手写霍夫变换原理
更详细的解释,可参考资料:
https://www.bilibili.com/video/BV1bb411b7VQ?from=search&seid=10595774442048469089&spm_id_from=333.337.0.0
https://jishuin.proginn.com/p/763bfbd6c025
二、手撕代码
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
np.random.seed(777)
np.set_printoptions(precision=3, suppress=True)
np.set_printoptions(formatter={
'float': '{: 0.3f}'.format})
def fake_image():
image = np.zeros([100, 100, 1], dtype=np.uint8)
rs = []
thetas = []
for _ in range(3):
theta = np.random.uniform(0, np.pi/2)
r = np.random.randint(30, 70)
rs.append(r)
thetas.append(np.rad2deg(theta))
if theta > np.pi/4:
x = np.arange(100)
y = np.uint8((r - x*np.cos(theta)) / np.sin(theta)