Opencv2.x-python 与opencv3.x-python特征点检测方法

Opencv2.x-python opencv3.x-python特征点检测方法

参考网址:OpenCV-Python Feature2D 特征点检测(含SIFT/SURF/ORB/KAZE/FAST/BRISK/AKAZE)_阿木寺的博客-CSDN博客

对于OpenCV-PythonOpenCV2.xOpenCV3.x的函数使用方式有很大不同。网上很多教程都还是基于OpenCV2.x,此版本已经逐渐被弃用。

本教程针对特征点检测,分析OpenCV2.xOpenCV3.x的不同之后,并重点介绍OpenCV3.x-Python的特征点检测。

Open2.x-Python 特征点检测方法

对于OpenCV2.x-Python,特征点检测及显示方法如下:

  1. # OpenCV2.x-Python
  2. function = cv2.Function_Name()
  3. keypoints = function.detect(img, None)
  4. img2 = cv2.drawKeyPoints(img, keypoints, color=(0,255,0))

其中Function_Name就是特征检测方法的函数名,如BRISKFastFeatureDetector等。

比如,在OpenCV2.x-Python,想使用Fast来检测特征点,示例如下:

  1. # OpenCV2.x-Python
  2. fast = cv2.FastFeatureDetector()
  3. keypoints = fast.detect(img, None)
  4. img2 = cv2.drawKeypoints(img, keypoints, color=(255,0,0))

Open3.x-Python 特征点检测方法

对于OpenCV3.x-Python,特征点检测及显示方法如下:

  1. # OpenCV3.x-Python
  2. # 注意有_create()后缀
  3. function = cv2.Function_Name_create()
  4. keypoints = function.detect(img, None)
  5. # 注意显示之前要先将img2初始化
  6. img2 = img.copy()
  7. img2 = cv2.drawKeyPoints(img, keypoints, color=(0,255,0))

其中Function_Name就是特征检测方法的函数名,如BRISKFastFeatureDetector等。

[注意1]:对于OpenCV3.x-Python,还要在Function_Name后加上_create后缀。其实这一点在opencv_doc中具体的函数python使用方法中已经注明了。

[注意2]:对于OpenCV3.x-Python,若要显示检测的特征点,需要初始化img2,才能正常显示。这里可以先使用img2 = img.copy()完成拷贝初始化。

下面就重点介绍OpenCV3.x-Python中的各种特征点检测方法的使用示例。

测试图像为标准的lena.png

AKAZE Feature Detection

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # Summary: 使用OpenCV3.x-Python检测AKAZE特征点
  4. # Author:  Amusi
  5. # Date:    2018-03-17
  6. # Reference: https://docs.opencv.org/master/d8/d30/classcv_1_1AKAZE.html
  7. import cv2
  8. import numpy
  9. def main():
  10.        img = cv2.imread("lena.png")
  11.        cv2.imshow('Input Image', img)
  12.        cv2.waitKey(0)
  13.       
  14.        # 检测
  15.        akaze = cv2.AKAZE_create()
  16.        keypoints = akaze.detect(img, None)
  17.       
  18.        # 显示
  19.        # 必须要先初始化img2
  20.        img2 = img.copy()
  21.        img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
  22.        cv2.imshow('Detected AKAZE keypoints', img2)
  23.        cv2.waitKey(0)
  24.     
  25. if __name__ == '__main__':
  26.        main()

BRISK Feature Detection

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # Summary: 使用OpenCV3.x-Python检测BRISK特征点
  4. # Author:  Amusi
  5. # Date:    2018-03-17
  6. # Reference: https://docs.opencv.org/master/de/dbf/classcv_1_1BRISK.html
  7. import cv2
  8. import numpy
  9. def main():
  10.        img = cv2.imread("lena.png")
  11.        cv2.imshow('Input Image', img)
  12.        cv2.waitKey(0)
  13.        brisk = cv2.BRISK_create()
  14.        keypoints = brisk.detect(img, None)
  15.       
  16.        # 必须要先初始化img2
  17.        img2 = img.copy()
  18.        img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
  19.        cv2.imshow('Detected BRISK keypoints', img2)
  20.        cv2.waitKey(0)
  21.     
  22. if __name__ == '__main__':
  23.        main()


 

Fast Feature Detection

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # Summary: 使用OpenCV3.x-Python检测FAST特征点
  4. # Author:  Amusi
  5. # Date:    2018-03-17
  6. # Reference: https://docs.opencv.org/master/df/d74/classcv_1_1FastFeatureDetector.html
  7. import cv2
  8. import numpy
  9. def main():
  10.        img = cv2.imread("lena.png")
  11.        cv2.imshow('Input Image', img)
  12.        cv2.waitKey(0)
  13.        # 2018-03-17 Amusi: OpenCV3.x FeatureDetector写法有变化
  14.        # OpenCV2.x
  15.        # fast = cv2.FastFeatureDetector()
  16.        # keypoints = fast.detect(img, None)
  17.       
  18.        # OpenCV3.x
  19.        # 注意有_create()后缀
  20.        fast = cv2.FastFeatureDetector_create()
  21.        keypoints = fast.detect(img, None)
  22.       
  23.        # 必须要先初始化img2
  24.        img2 = img.copy()
  25.        img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
  26.        cv2.imshow('Detected FAST keypoints', img2)
  27.        cv2.waitKey(0)
  28.     
  29. if __name__ == '__main__':
  30.        main()


 

KAZE Feature Detection

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # Summary: 使用OpenCV3.x-Python检测KAZE特征点
  4. # Author:  Amusi
  5. # Date:    2018-03-17
  6. # Reference: https://docs.opencv.org/master/d3/d61/classcv_1_1KAZE.html
  7. import cv2
  8. import numpy
  9. def main():
  10.        img = cv2.imread("lena.png")
  11.        cv2.imshow('Input Image', img)
  12.        cv2.waitKey(0)
  13.       
  14.        # 检测
  15.        kaze = cv2.KAZE_create()
  16.        keypoints = kaze.detect(img, None)
  17.       
  18.        # 显示
  19.        # 必须要先初始化img2
  20.        img2 = img.copy()
  21.        img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
  22.        cv2.imshow('Detected KAZE keypoints', img2)
  23.        cv2.waitKey(0)
  24.     
  25. if __name__ == '__main__':
  26.        main()

ORB Feature Detection

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # Summary: 使用OpenCV3.x-Python检测ORB特征点
  4. # Author:  Amusi
  5. # Date:    2018-03-17
  6. # Reference: https://docs.opencv.org/master/db/d95/classcv_1_1ORB.html
  7. import cv2
  8. import numpy
  9. def main():
  10.        img = cv2.imread("lena.png")
  11.        cv2.imshow('Input Image', img)
  12.        cv2.waitKey(0)
  13.       
  14.        # 检测
  15.        orb = cv2.ORB_create()
  16.        keypoints = orb.detect(img, None)
  17.       
  18.        # 显示
  19.        # 必须要先初始化img2
  20.        img2 = img.copy()
  21.        img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
  22.        cv2.imshow('Detected ORB keypoints', img2)
  23.        cv2.waitKey(0)
  24.     
  25. if __name__ == '__main__':
  26.        main()


 

----------我是可爱的分割线----------

下面介绍属于nonfree的特征检测方法,如SIFTSURF

这些方法在opencv-contrib中,所以想要使用前,请卸载当前非contrib版本的opencv,即pip uninstall opencv-python后;再重新安装opencv-contrib-python,即pip install opencv-contrib-python

SIFT Feature Detection

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # Summary: 使用OpenCV3.x-Python检测SIFT特征点
  4. # Author:  Amusi
  5. # Date:    2018-03-17
  6. # Reference: https://docs.opencv.org/master/d5/d3c/classcv_1_1xfeatures2d_1_1SIFT.html
  7. import cv2
  8. import numpy
  9. def main():
  10.        img = cv2.imread("lena.png")
  11.        cv2.imshow('Input Image', img)
  12.        cv2.waitKey(0)
  13.       
  14.        # 检测
  15.        sift = cv2.xfeatures2d.SIFT_create()
  16.        keypoints = sift.detect(img, None)
  17.       
  18.        # 显示
  19.        # 必须要先初始化img2
  20.        img2 = img.copy()
  21.        img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
  22.        cv2.imshow('Detected SIFT keypoints', img2)
  23.        cv2.waitKey(0)
  24.     
  25. if __name__ == '__main__':
  26.        main()


 

SURF Feature Detection

  1. #!/usr/bin/env python
  2. # -*- coding=utf-8 -*-
  3. # Summary: 使用OpenCV3.x-Python检测SURF特征点
  4. # Author:  Amusi
  5. # Date:    2018-03-17
  6. # Reference: https://docs.opencv.org/master/d5/df7/classcv_1_1xfeatures2d_1_1SURF.html
  7. import cv2
  8. import numpy
  9. def main():
  10.        img = cv2.imread("lena.png")
  11.        cv2.imshow('Input Image', img)
  12.        cv2.waitKey(0)
  13.       
  14.        # 检测
  15.        surf = cv2.xfeatures2d.SURF_create()
  16.        keypoints = surf.detect(img, None)
  17.       
  18.        # 显示
  19.        # 必须要先初始化img2
  20.        img2 = img.copy()
  21.        img2 = cv2.drawKeypoints(img, keypoints, img2, color=(0,255,0))
  22.        cv2.imshow('Detected SURF keypoints', img2)
  23.        cv2.waitKey(0)
  24.     
  25. if __name__ == '__main__':
  26.        main()

注:OpenCV3.x-PythonOpenCV2.x-Python有很多函数的用法不同,虽然网上教程大多参次不齐,但可以直接去官网查看最新的用法(官网即正义)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一颗温暖的心_lucky

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值