python 实现Harris角点检测算法


Posted in Python onDecember 11, 2020

算法流程:

  1. 将图像转换为灰度图像
  2. 利用Sobel滤波器求出 海森矩阵 (Hessian matrix) :

python 实现Harris角点检测算法

  • 将高斯滤波器分别作用于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()

实验结果:

原图:

python 实现Harris角点检测算法

Harris角点检测算法检测结果:

python 实现Harris角点检测算法

以上就是python 实现Harris角点检测算法的详细内容,更多关于python Harris角点检测算法的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python字符串替换示例
Apr 24 Python
用Python代码来解图片迷宫的方法整理
Apr 02 Python
进一步了解Python中的XML 工具
Apr 13 Python
Python语言描述KNN算法与Kd树
Dec 13 Python
Tensorflow 同时载入多个模型的实例讲解
Jul 27 Python
对python列表里的字典元素去重方法详解
Jan 21 Python
Python函数的返回值、匿名函数lambda、filter函数、map函数、reduce函数用法实例分析
Dec 26 Python
PyCharm无法引用自身项目解决方式
Feb 12 Python
用Python 爬取猫眼电影数据分析《无名之辈》
Jul 24 Python
django中cookiecutter的使用教程
Dec 03 Python
python 通过 pybind11 使用Eigen加速代码的步骤
Dec 07 Python
python math模块的基本使用教程
Jan 16 Python
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
Dec 11 #Python
Python3 用matplotlib绘制sigmoid函数的案例
Dec 11 #Python
python 基于opencv 实现一个鼠标绘图小程序
Dec 11 #Python
Python 用__new__方法实现单例的操作
Dec 11 #Python
python实现图像高斯金字塔的示例代码
Dec 11 #Python
Pycharm plot独立窗口显示的操作
Dec 11 #Python
Python OpenCV中的numpy与图像类型转换操作
Dec 11 #Python
You might like
在“咖啡之国”感受咖啡文化
2021/03/03 咖啡文化
PHP 身份证号验证函数
2009/05/07 PHP
解析PHP中如何将数组变量写入文件
2013/06/06 PHP
php登陆页的密码处理方式分享
2013/10/14 PHP
完整删除ecshop中获取店铺信息的API
2014/12/24 PHP
PHP数字金额转换成中文大写显示
2019/01/05 PHP
TP3.2.3框架文件上传操作实例详解
2020/01/23 PHP
Jquery中使用setInterval和setTimeout的方法
2013/04/08 Javascript
js判断页面中是否有指定控件的简单实例
2014/03/04 Javascript
javascript的函数作用域
2014/11/12 Javascript
jquery调整表格行tr上下顺序实例讲解
2016/01/09 Javascript
js创建jsonArray传输至后台及后台全面解析
2016/04/11 Javascript
浅谈angular懒加载的一些坑
2016/08/20 Javascript
Web打印解决方案之证件套打的实现思路
2016/08/29 Javascript
微信js-sdk预览图片接口及从拍照或手机相册中选图接口用法示例
2016/10/13 Javascript
vue父子组件的数据传递示例
2017/03/07 Javascript
Vue实现自带的过滤器实例
2017/03/09 Javascript
AngularJS 单选框及多选框的双向动态绑定
2017/04/20 Javascript
微信小程序自定义单项选择器样式
2019/07/25 Javascript
用VsCode编辑TypeScript的实现方法
2020/05/07 Javascript
JavaScript, select标签元素左右移动功能实现
2020/05/14 Javascript
浅谈JSON5解决了JSON的两大痛点
2020/12/14 Javascript
将Python中的数据存储到系统本地的简单方法
2015/04/11 Python
Python实现动态图解析、合成与倒放
2018/01/18 Python
Python闭包函数定义与用法分析
2018/07/20 Python
Django values()和value_list()的使用
2020/03/31 Python
处理HTML5新标签的浏览器兼容版问题
2017/03/13 HTML / CSS
利用canvas实现图片压缩的示例代码
2018/07/17 HTML / CSS
HTML table 表格边框的实现思路
2019/10/12 HTML / CSS
HTML5跳转小程序wx-open-launch-weapp的示例代码
2020/07/16 HTML / CSS
《忆江南》教学反思
2014/04/07 职场文书
局机关干部群众路线个人对照检查材料思想汇报
2014/10/05 职场文书
2015大学生求职信范文
2015/03/20 职场文书
协议书格式模板
2016/03/24 职场文书
导游词之宿迁乾隆行宫
2019/10/15 职场文书
python双向链表实例详解
2022/05/25 Python