华域联盟 Python OpenCV半小时掌握基本操作之角点检测

OpenCV半小时掌握基本操作之角点检测

目录

概述

OpenCV 是一个跨平台的计算机视觉库, 支持多语言, 功能强大. 今天小白就带大家一起携手走进 OpenCV 的世界.

角点检测

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

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

例如: 某图像的某个位置在 x / y 方向上做微小的滑动, 如果窗口内的灰度值都有较大变换, 那么这个位置就是角点.

角点检测代码

格式:

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

参数:

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

例1 :

import numpy as np
import cv2

# 读取图片
image = cv2.imread("house.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:

import numpy as np
import cv2

# 读取图片
image = cv2.imread("house2.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)

输出结果:

到此这篇关于OpenCV半小时掌握基本操作之角点检测的文章就介绍到这了,更多相关OpenCV角点检测内容请搜索华域联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持华域联盟!

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » OpenCV半小时掌握基本操作之角点检测

转载请保留出处和原文链接:https://www.cnhackhy.com/35033.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部