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关键点检测
tv = cv.imread('img/tv.jpg')
gray = cv.cvtColor(tv,cv.COLOR_BGR2GRAY)
sift = cv.SIFT_create()
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])
2.Fast算法
注:也可以传入彩色图像!
tv = cv.imread('img/tv.jpg')
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()
3.ORB算法
tv = cv.imread('img/tv.jpg')
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])
总结