python 实现Harris角点检测算法
作者:我坚信阳光灿烂 时间:2023-08-03 08:08:31
算法流程:
将图像转换为灰度图像
利用Sobel滤波器求出 海森矩阵 (Hessian matrix) :
将高斯滤波器分别作用于Ix²、Iy²、IxIy
计算每个像素的 R= det(H) - k(trace(H))²。det(H)表示矩阵H的行列式,trace表示矩阵H的迹。通常k的取值范围为[0.04,0.16]。
满足 R>=max(R) * th 的像素点即为角点。th常取0.1。
Harris算法实现:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# Harris corner detection
def Harris_corner(img):
## Grayscale
def BGR2GRAY(img):
gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]
gray = gray.astype(np.uint8)
return gray
## Sobel
def Sobel_filtering(gray):
# get shape
H, W = gray.shape
# sobel kernel
sobely = np.array(((1, 2, 1),
(0, 0, 0),
(-1, -2, -1)), dtype=np.float32)
sobelx = np.array(((1, 0, -1),
(2, 0, -2),
(1, 0, -1)), dtype=np.float32)
# padding
tmp = np.pad(gray, (1, 1), 'edge')
# prepare
Ix = np.zeros_like(gray, dtype=np.float32)
Iy = np.zeros_like(gray, dtype=np.float32)
# get differential
for y in range(H):
for x in range(W):
Ix[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobelx)
Iy[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobely)
Ix2 = Ix ** 2
Iy2 = Iy ** 2
Ixy = Ix * Iy
return Ix2, Iy2, Ixy
# gaussian filtering
def gaussian_filtering(I, K_size=3, sigma=3):
# get shape
H, W = I.shape
## gaussian
I_t = np.pad(I, (K_size // 2, K_size // 2), 'edge')
# gaussian kernel
K = np.zeros((K_size, K_size), dtype=np.float)
for x in range(K_size):
for y in range(K_size):
_x = x - K_size // 2
_y = y - K_size // 2
K[y, x] = np.exp( -(_x ** 2 + _y ** 2) / (2 * (sigma ** 2)))
K /= (sigma * np.sqrt(2 * np.pi))
K /= K.sum()
# filtering
for y in range(H):
for x in range(W):
I[y,x] = np.sum(I_t[y : y + K_size, x : x + K_size] * K)
return I
# corner detect
def corner_detect(gray, Ix2, Iy2, Ixy, k=0.04, th=0.1):
# prepare output image
out = np.array((gray, gray, gray))
out = np.transpose(out, (1,2,0))
# get R
R = (Ix2 * Iy2 - Ixy ** 2) - k * ((Ix2 + Iy2) ** 2)
# detect corner
out[R >= np.max(R) * th] = [255, 0, 0]
out = out.astype(np.uint8)
return out
# 1. grayscale
gray = BGR2GRAY(img)
# 2. get difference image
Ix2, Iy2, Ixy = Sobel_filtering(gray)
# 3. gaussian filtering
Ix2 = gaussian_filtering(Ix2, K_size=3, sigma=3)
Iy2 = gaussian_filtering(Iy2, K_size=3, sigma=3)
Ixy = gaussian_filtering(Ixy, K_size=3, sigma=3)
# 4. corner detect
out = corner_detect(gray, Ix2, Iy2, Ixy)
return out
# Read image
img = cv.imread("../qiqiao.jpg").astype(np.float32)
# Harris corner detection
out = Harris_corner(img)
cv.imwrite("out.jpg", out)
cv.imshow("result", out)
cv.waitKey(0)
cv.destroyAllWindows()
实验结果:
原图:
Harris角点检测算法检测结果:
来源:https://www.cnblogs.com/wojianxin/p/12574909.html
标签:python,Harris角点检测,算法
0
投稿
猜你喜欢
Python3多进程 multiprocessing 模块实例详解
2022-05-27 11:11:51
JS 中如何判断 null
2007-12-13 20:28:00
python 通过 pybind11 使用Eigen加速代码的步骤
2021-03-07 06:09:30
如何用mysql自带的定时器定时执行sql(每天0点执行与间隔分/时执行)
2024-01-16 01:58:31
python动态性强类型用法实例
2022-08-04 23:32:51
php设置允许大文件上传示例代码
2024-05-09 14:47:46
Python操作Redis数据库的超详细教程
2024-01-13 04:37:30
python从gbff文件中直接提取cds序列
2023-05-29 16:10:35
python采用django框架实现支付宝即时到帐接口
2023-07-16 00:15:33
linux CentOS6.5 yum安装mysql5.6
2024-01-28 16:45:25
python实现测试工具(一)——命令行发送get请求
2022-04-06 03:49:00
配置高可用性的MySQL服务器负载均衡群集
2009-01-04 12:43:00
快速掌握JavaScript正则表达式
2010-01-23 11:39:00
Python实现简单生成验证码功能【基于random模块】
2022-12-10 18:19:58
Python批量对word文档进行操作步骤
2022-07-24 03:37:36
Python利用xlrd 与 xlwt 模块操作 Excel
2022-07-19 20:57:13
在Visual Studio中使用GIT进行源码管理
2022-09-24 19:28:02
Python的Bottle框架中实现最基本的get和post的方法的教程
2022-07-13 13:37:24
找Python安装目录,设置环境路径以及在命令行运行python脚本实例
2022-06-01 23:28:56
python根据距离和时长计算配速示例
2022-10-13 14:30:36