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

yipeiwu_com5年前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中关于使用模块的基础知识

 一个模块可以在逻辑上组织Python代码。将相关的代码到一个模块中,使代码更容易理解和使用。模块是可以绑定和借鉴任意命名属性的Python对象。 简单地说,一个模块是由Pyt...

python Django批量导入不重复数据

本文为大家分享了python Django批量导入不重复数据的实现代码,供大家参考,具体内容如下 程序如下: #coding:utf-8 import os os.enviro...

python3 unicode列表转换为中文的实例

python3 unicode列表转换为中文的实例

查了很多很多的资料无果,果然知乎牛逼,完美解决。 爬取网站时,最终得到list内容,编码为unicode,想让其转换为汉字并输出。 需要提取的为下图中unicode部分: 保存为列表...

超实用的 30 段 Python 案例

Python是目前最流行的语言之一,它在数据科学、机器学习、web开发、脚本编写、自动化方面被许多人广泛使用。 它的简单和易用性造就了它如此流行的原因。 如果你正在阅读本文,那么你或多或...

Python使用PyGreSQL操作PostgreSQL数据库教程

PostgreSQL是一款功能强大的开源关系型数据库,本文使用python实现了对开源数据库PostgreSQL的常用操作,其开发过程简介如下: 一、环境信息:   ...