OpenCV哈里斯(Harris)角点检测的实现

yipeiwu_com6年前Python基础

环境

pip install opencv-python==3.4.2.16
 
pip install opencv-contrib-python==3.4.2.16

理论

克里斯·哈里斯Chris Harris)和迈克·史蒂芬斯(Mike Stephens)在1988年的论文《组合式拐角和边缘检测器》中做了一次尝试找到这些拐角的尝试,所以现在将其称为哈里斯拐角检测器。

函数:cv2.cornerHarris()cv2.cornerSubPix()

示例代码

import cv2
import numpy as np
 
filename = 'molecule.png'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
 
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
 
#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)
 
# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]
 
cv2.imshow('dst',img)
if cv2.waitKey(0) & 0xff == 27:
  cv2.destroyAllWindows()

原图

输出图

SubPixel精度的角落

import cv2
import numpy as np
 
filename = 'molecule.png'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
 
# find Harris corners
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
dst = cv2.dilate(dst,None)
ret, dst = cv2.threshold(dst,0.01*dst.max(),255,0)
dst = np.uint8(dst)
 
# find centroids
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
 
# define the criteria to stop and refine the corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
 
# Now draw them
res = np.hstack((centroids,corners))
res = np.int0(res)
img[res[:,1],res[:,0]]=[0,0,255]
img[res[:,3],res[:,2]] = [0,255,0]
 
cv2.imwrite('subpixel5.png',img)

输出图

参考

https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_features_harris/py_features_harris.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python的Django框架下管理站点的基本方法

对于某一类网站, 管理界面 是基础设施中非常重要的一部分。 这是以网页和有限的可信任管理者为基础的界面,它可以让你添加,编辑和删除网站内容。 一些常见的例子: 你可以用这个界面发布博客,...

python3.6生成器yield用法实例分析

本文实例讲述了python3.6生成器yield用法。分享给大家供大家参考,具体如下: 今天看源码的时候看到了一个比较有意思的函数:yield 功能与return类似,都是返回定义的函数...

使用Python制作自动推送微信消息提醒的备忘录功能

使用Python制作自动推送微信消息提醒的备忘录功能

日常工作生活中,事情一多,就会忘记一些该做未做的事情。即使有时候把事情记录在了小本本上或者手机、电脑端备忘录上,也总会有查看不及时,导致错过的尴尬。如果有一款小工具,可以及时提醒,而不用...

Python中按键来获取指定的值

Python中按键来获取值,相对来说要容易些,毕竟只需要dict[key]就可以找到,但里面同样有个问题,如果其中的键不存在的话,会抛出异常,如果不用try...except...等异常...

Python程序语言快速上手教程

Python程序语言快速上手教程

本来打算从网上找一篇入门教程,但因为Python很少是程序员的第一次接触程序所学的语言,所以网上现有的教程多不是很基础,还是决定自己写下这些。 如果没有程序基础的话,可能会觉得本文涵盖的...