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 Flask 装饰器顺序问题解决

上周 RealWorld CTF 2018 web 题 bookhub 有个未授权访问的漏洞,比较有意思,赛后看了一下公开的 WriteUp,大家也都没写清楚,所以就有了这篇博文。 前言...

使用DataFrame删除行和列的实例讲解

本文通过一个csv实例文件来展示如何删除Pandas.DataFrame的行和列 数据文件名为:example.csv 内容为: date s...

12个步骤教你理解Python装饰器

前言 或许你已经用过装饰器,它的使用方式非常简单但理解起来困难(其实真正理解的也很简单),想要理解装饰器,你需要懂点函数式编程的概念,python函数的定义以及函数调用的语法规则等,虽然...

Python实现基于PIL和tesseract的验证码识别功能示例

Python实现基于PIL和tesseract的验证码识别功能示例

本文实例讲述了Python实现基于PIL和tesseract的验证码识别功能。分享给大家供大家参考,具体如下: 之前搞这个搞了一段时间,后面遇到了点小麻烦,导致识别率太低了,最多也就百分...

利用QT写一个极简单的图形化Python闹钟程序

今天我们讲一下用Python写的GUI小程序。一个小闹钟(只是屏幕提示,没有声音哦) 让我们先介绍这个闹钟如何奇葩。 需要通过命令行启动。 没有标题栏。 没菜单。 甚至没有关闭按钮。 没...