Opencv2.x-python 与opencv3.x-python特征点检测方法
参考网址:OpenCV-Python Feature2D 特征点检测(含SIFT/SURF/ORB/KAZE/FAST/BRISK/AKAZE)_阿木寺的博客-CSDN博客
对于OpenCV-Python,OpenCV2.x和OpenCV3.x的函数使用方式有很大不同。网上很多教程都还是基于OpenCV2.x,此版本已经逐渐被弃用。
本教程针对特征点检测,分析OpenCV2.x和OpenCV3.x的不同之后,并重点介绍OpenCV3.x-Python的特征点检测。
对于OpenCV2.x-Python,特征点检测及显示方法如下:
- # OpenCV2.x-Python
- function = cv2.Function_Name()
- keypoints = function.detect(img, None)
- img2 = cv2.drawKeyPoints(img, keypoints, color=(0,255,0))
其中Function_Name就是特征检测方法的函数名,如BRISK、FastFeatureDetector等。
比如,在OpenCV2.x-Python,想使用Fast来检测特征点,示例如下:
- # OpenCV2.x-Python
- fast = cv2.FastFeatureDetector()
- keypoints = fast.detect(img, None)
- img2 = cv2.drawKeypoints(img, keypoints, color=(255,0,0))
对于OpenCV3.x-Python,特征点检测及显示方法如下:
- # OpenCV3.x-Python
- # 注意有_create()后缀
- function = cv2.Function_Name_create()
- keypoints = function.detect(img, None)
- # 注意显示之前要先将img2初始化
- img2 = img.copy()
- img2 = cv2.drawKeyPoints(img, keypoints, color=(0,255,0))
其中Function_Name就是特征检测方法的函数名,如BRISK、FastFeatureDetector等。
[注意1]:对于OpenCV3.x-Python,还要在Function_Name后加上_create后缀。其实这一点在opencv_doc中具体的函数python使用方法中已经注明了。
[注意2]:对于OpenCV3.x-Python,若要显示检测的特征点,需要初始化img2,才能正常显示。这里可以先使用img2 = img.copy()完成拷贝初始化。
下面就重点介绍OpenCV3.x-Python中的各种特征点检测方法的使用示例。
测试图像为标准的lena.png
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # Summary: 使用OpenCV3.x-Python检测AKAZE特征点
- # Author: Amusi
- # Date: 2018-03-17
- # Reference: https://docs.opencv.org/master/d8/d30/classcv_1_1AKAZE.html
- import cv2
- import numpy
- def main():
- img = cv2.imread("lena.png")
- cv2.imshow('Input Image', img)
- cv2.waitKey(0)
- # 检测
- akaze = cv2.AKAZE_create()
- keypoints = akaze.detect(img, None)
- # 显示
- # 必须要先初始化img2
- img2 = img.copy()
- img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
- cv2.imshow('Detected AKAZE keypoints', img2)
- cv2.waitKey(0)
- if __name__ == '__main__':
- main()
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # Summary: 使用OpenCV3.x-Python检测BRISK特征点
- # Author: Amusi
- # Date: 2018-03-17
- # Reference: https://docs.opencv.org/master/de/dbf/classcv_1_1BRISK.html
- import cv2
- import numpy
- def main():
- img = cv2.imread("lena.png")
- cv2.imshow('Input Image', img)
- cv2.waitKey(0)
- brisk = cv2.BRISK_create()
- keypoints = brisk.detect(img, None)
- # 必须要先初始化img2
- img2 = img.copy()
- img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
- cv2.imshow('Detected BRISK keypoints', img2)
- cv2.waitKey(0)
- if __name__ == '__main__':
- main()
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # Summary: 使用OpenCV3.x-Python检测FAST特征点
- # Author: Amusi
- # Date: 2018-03-17
- # Reference: https://docs.opencv.org/master/df/d74/classcv_1_1FastFeatureDetector.html
- import cv2
- import numpy
- def main():
- img = cv2.imread("lena.png")
- cv2.imshow('Input Image', img)
- cv2.waitKey(0)
- # 2018-03-17 Amusi: OpenCV3.x FeatureDetector写法有变化
- # OpenCV2.x
- # fast = cv2.FastFeatureDetector()
- # keypoints = fast.detect(img, None)
- # OpenCV3.x
- # 注意有_create()后缀
- fast = cv2.FastFeatureDetector_create()
- keypoints = fast.detect(img, None)
- # 必须要先初始化img2
- img2 = img.copy()
- img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
- cv2.imshow('Detected FAST keypoints', img2)
- cv2.waitKey(0)
- if __name__ == '__main__':
- main()
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # Summary: 使用OpenCV3.x-Python检测KAZE特征点
- # Author: Amusi
- # Date: 2018-03-17
- # Reference: https://docs.opencv.org/master/d3/d61/classcv_1_1KAZE.html
- import cv2
- import numpy
- def main():
- img = cv2.imread("lena.png")
- cv2.imshow('Input Image', img)
- cv2.waitKey(0)
- # 检测
- kaze = cv2.KAZE_create()
- keypoints = kaze.detect(img, None)
- # 显示
- # 必须要先初始化img2
- img2 = img.copy()
- img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
- cv2.imshow('Detected KAZE keypoints', img2)
- cv2.waitKey(0)
- if __name__ == '__main__':
- main()
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # Summary: 使用OpenCV3.x-Python检测ORB特征点
- # Author: Amusi
- # Date: 2018-03-17
- # Reference: https://docs.opencv.org/master/db/d95/classcv_1_1ORB.html
- import cv2
- import numpy
- def main():
- img = cv2.imread("lena.png")
- cv2.imshow('Input Image', img)
- cv2.waitKey(0)
- # 检测
- orb = cv2.ORB_create()
- keypoints = orb.detect(img, None)
- # 显示
- # 必须要先初始化img2
- img2 = img.copy()
- img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
- cv2.imshow('Detected ORB keypoints', img2)
- cv2.waitKey(0)
- if __name__ == '__main__':
- main()
----------我是可爱的分割线----------
下面介绍属于nonfree的特征检测方法,如SIFT和SURF。
这些方法在opencv-contrib中,所以想要使用前,请卸载当前非contrib版本的opencv,即pip uninstall opencv-python后;再重新安装opencv-contrib-python,即pip install opencv-contrib-python
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # Summary: 使用OpenCV3.x-Python检测SIFT特征点
- # Author: Amusi
- # Date: 2018-03-17
- # Reference: https://docs.opencv.org/master/d5/d3c/classcv_1_1xfeatures2d_1_1SIFT.html
- import cv2
- import numpy
- def main():
- img = cv2.imread("lena.png")
- cv2.imshow('Input Image', img)
- cv2.waitKey(0)
- # 检测
- sift = cv2.xfeatures2d.SIFT_create()
- keypoints = sift.detect(img, None)
- # 显示
- # 必须要先初始化img2
- img2 = img.copy()
- img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
- cv2.imshow('Detected SIFT keypoints', img2)
- cv2.waitKey(0)
- if __name__ == '__main__':
- main()
- #!/usr/bin/env python
- # -*- coding=utf-8 -*-
- # Summary: 使用OpenCV3.x-Python检测SURF特征点
- # Author: Amusi
- # Date: 2018-03-17
- # Reference: https://docs.opencv.org/master/d5/df7/classcv_1_1xfeatures2d_1_1SURF.html
- import cv2
- import numpy
- def main():
- img = cv2.imread("lena.png")
- cv2.imshow('Input Image', img)
- cv2.waitKey(0)
- # 检测
- surf = cv2.xfeatures2d.SURF_create()
- keypoints = surf.detect(img, None)
- # 显示
- # 必须要先初始化img2
- img2 = img.copy()
- img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
- cv2.imshow('Detected SURF keypoints', img2)
- cv2.waitKey(0)
- if __name__ == '__main__':
- main()
注:OpenCV3.x-Python与OpenCV2.x-Python有很多函数的用法不同,虽然网上教程大多参次不齐,但可以直接去官网查看最新的用法(官网即正义)