zl程序教程

您现在的位置是:首页 >  后端

当前栏目

OpenCV入门(二十六)快速学会OpenCV 25 角点检测

Opencv入门 快速 检测 25 学会 二十六
2023-09-14 09:12:45 时间

OpenCV入门(二十六)快速学会OpenCV 25 角点检测

作者:Xiou

1.角点检测

角点检测 (Corner Detection) 是图像的重要特征. 角点可以帮助我们实现图像对其, 图像拼接, 目标识别等等重要用途。

·Harris:该算法适用于角点检测。

Harris 角点检测 (Harris Corner Detection) 是最基础也是最重要的一种角点检测算法. 通过计算图像在 x, y 上平移的自相似性 (Self-Similarity) 来判断图像是否为角点。

在这里插入图片描述

OpenCV有一个名为cv2.cornerHarris的方便函数,用于检测图像中的角点。例如: 某图像的某个位置在 x / y 方向上做微小的滑动, 如果窗口内的灰度值都有较大变换, 那么这个位置就是角点。

cv2.cornerHarris(src, blockSize, ksize, k, dst=None, borderType=None)

参数:

scr: 输入图像;
blockSize: 焦点检测中指定区域的大小;
ksize: Sobel 求导中使用的窗口大小;
ksize: Sobel 孔径参数, 取值范围为 [0.04, 0.06];

2.角点检测代码实例

2.1 实例1:

import numpy as np
import cv2

# 读取图片
image = cv2.imread("p1.jpg")
# 转换成灰度图
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# harris角点检测
harris = cv2.cornerHarris(image_gray, 2, 3, 0.04)
# 阈值转换原图
image_corner = image.copy()
image_corner[harris > 0.01 * harris.max()] = [0, 0, 255]
# 整合
combine = np.hstack((image, image_corner))

# 图片展示
cv2.imshow("origional vs corner detection", combine)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存结果
cv2.imwrite("harris.jpg", combine)

输出结果:
在这里插入图片描述

2.2 实例2:

import numpy as np
import cv2

# 读取图片
image = cv2.imread("p1.jpg")
# 转换成灰度图
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# harris角点检测
harris = cv2.cornerHarris(image_gray, 2, 3, 0.04)
# 阈值转换原图
image_corner = image.copy()
image_corner[harris > 0.1 * harris.max()] = [0, 0, 255]
# 整合
combine = np.hstack((image, image_corner))
# 图片展示
cv2.imshow("origional vs corner detection", image_corner)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 保存结果
cv2.imwrite("harris.jpg", combine)

输出结果:

在这里插入图片描述