OpenCV-Python 图像的关键点检测

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
# 设置兼容中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
D:\Anaconda\AZWZ\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.NOIJJG62EMASZI6NYURL6JBKM4EVBGM7.gfortran-win_amd64.dll
D:\Anaconda\AZWZ\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll
  warnings.warn("loaded more than 1 DLL from .libs:\n%s" %

1.Sift关键点检测

OpenCV-Python 图像的关键点检测_第1张图片

OpenCV-Python 图像的关键点检测_第2张图片

tv = cv.imread('img/tv.jpg')
# 转化为灰度图
gray = cv.cvtColor(tv,cv.COLOR_BGR2GRAY)
# 实例化shit对象
sift = cv.SIFT_create()
# 关键点检测 kp关键点信息包括方向,尺度,位置信息,des是关键点的描述符
kp,des = sift.detectAndCompute(gray,None)
# 在图像上绘制关键点的检测结果
cv.drawKeypoints(tv,kp,tv,flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       ...,

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]],

       [[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)
# 图像显示
plt.figure(dpi=500)
plt.imshow(tv[:,:,::-1])

OpenCV-Python 图像的关键点检测_第3张图片

2.Fast算法

OpenCV-Python 图像的关键点检测_第4张图片

OpenCV-Python 图像的关键点检测_第5张图片

OpenCV-Python 图像的关键点检测_第6张图片

注:也可以传入彩色图像!

OpenCV-Python 图像的关键点检测_第7张图片

tv = cv.imread('img/tv.jpg')
# 创建一个fast对象 传入阈值 注意:可以传入彩色图像
fast = cv.FastFeatureDetector_create(threshold=30)
# 检测图像上的关键点
kp = fast.detect(tv,None)
# 在图像上绘制关键点
img2 = cv.drawKeypoints(tv,kp,None,color=(0,0,255))
# 输出默认参数
print("Threshold:{}".format(fast.getThreshold()))
print("NonmaxSuppression:{}".format(fast.getNonmaxSuppression()))
print("neighborhood:{}".format(fast.getType()))
print("Total Keypoints with nonmaxSupperssion:{}".format(len(kp)))
Threshold:30
NonmaxSuppression:True
neighborhood:2
Total Keypoints with nonmaxSupperssion:3539
# 关闭非极大值抑制
fast.setNonmaxSuppression(0)
kp = fast.detect(tv,None)
print("Total Keypoints with nonmaxSupperssion:{}".format(len(kp)))
Total Keypoints with nonmaxSupperssion:8959
# 绘制为进行非极大值抑制的结果
img3 = cv.drawKeypoints(tv,kp,None,color=(0,0,255))
# 绘制图形
plt.figure(figsize=(20,20))
plt.subplot(1,2,1)
m1 = plt.imshow(img2[:,:,::-1])
plt.title("加入非极大值抑制")
plt.subplot(1,2,2)
m2 = plt.imshow(img3[:,:,::-1])
plt.title("未加入非极大值抑制")
plt.show()

OpenCV-Python 图像的关键点检测_第8张图片

3.ORB算法

OpenCV-Python 图像的关键点检测_第9张图片

OpenCV-Python 图像的关键点检测_第10张图片

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sqUDK1wR-1637753728952)(attachment:image.png)]

tv = cv.imread('img/tv.jpg')
# 实例化ORB对象 nfeatures:检测多少个角点
orb = cv.ORB_create(nfeatures=5000)
# 检测关键点,并计算特征描述符
kp,des = orb.detectAndCompute(tv,None)
print("des.shape:",des.shape)
des.shape: (4395, 32)
# 将关键点绘制在图像上
res = cv.drawKeypoints(tv,kp,None,color=(0,0,255),flags = 0)
# 绘制图形
plt.figure(dpi=400)
plt.imshow(res[:,:,::-1])

总结

OpenCV-Python 图像的关键点检测_第11张图片

你可能感兴趣的:(机器视觉,python,opencv,计算机视觉)