1.角点是图片的重要区分特点,利用好角点容易区分图像的特征
2.Harris算法和shi-Tomas算法:都是利用角点检测来判断角点,边界以及平坦区域
3.Harris算法是通过R的值来判断角点,边界以及平坦区域
R = 矩阵行列式M - M的轨迹的平方
当R是一个数值大的正数时,它是角点
当R是一个数值大的负数时,它是边界
当R是小数时,它是平坦区域
API:cv.cornerHarris(src,blackSize,ksize,k)
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像并转换为灰色图 img = cv.imread(‘image1.jpg‘) gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) #输入图像必须为floate32 gray = np.float32(gray) dst = cv.cornerHarris(gray,2,3,0.04) #2为blockSize为领域大小,也就是窗口 #3为sobel求导时使用核大小 #角点赋值红色 img[dst>0.001*dst.max()] = [0,0,255] plt.imshow(img[:,:,::-1]) plt.show()
4.shi-tomas角点检测比Harris更好找到角点,若矩阵两个特征值中较小的一个大于阈值,则认为它是角点
2.判断角点方法:
1)如果两个都大于阈值就是角点
2)如果一个大于阈值就是边界
3)如果都小于阈值就是平坦区域
1)API:coeners = cv.goodFeaturesToTrace(img,maxcorners,qualityLevel,minDistance)
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #读取图像并转换为灰色图 img = cv.imread(‘image1.jpg‘) gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) corners = cv.goodFeaturesToTrack(gray,1000,0.01,10) for i in corners: x,y = i.ravel() cv.circle(img,(x,y),2,(0,0,255),-1) plt.imshow(img[:,:,::-1]) plt.show()
5.SIFT算法和SURF算法:不仅具有Harris算法的旋转不变性还具有尺度不变性
1.实例化SIFT
sift = cv.xfeatures2d.SIFT_create()
2.利用sift.detectAndCompute()检测关键点并计算
kp,des = sift,detectAndCompute(gray,None)
3.将关键点检测结果绘制在图像上
cv.drawKeypoints(image,keypoints,outpoints,color,flags)
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #1.读取图像并转换为灰色图 img = cv.imread(‘image1.jpg‘) gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY) #2.关键点检测 #2.1.实例化sift对象 #sift = cv.xfeatures2d.SIFT_create() #改进为 sift = cv.SIFT_create() #2.2.关键点检测,kp包括方向,尺度,位置信息,des关键点描述 kp,des = sift.detectAndCompute(gray,None) #2.3.绘制关键点检测结果 cv.drawKeypoints(img,kp,img,cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) plt.imshow(img[:,:,::-1]) plt.show()
6.Fast算法和ORB算法:
FAST算法的角点检测就是一个像素周围有一定数量的像素与该点的像素值不同,则认为其为角点
1).实例化fast对象
fast = cv.FastfeatureDetector_create(threshold,nonmaxSuppression)
2).利用fast.detect检测关键点
kp = fast.detect(grayImg,None)
3).检测结果绘制在图像上
cv.drawKeypoints(img,keypoints,outpoints,color,flags)
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #1.读取图像 img = cv.imread(‘image1.jpg‘) #2.实例化fast对象 fast = cv.FastFeatureDetector_create(threshold=30) #3.利用fast.detect检测关键点 kp = fast.detect(img,None) #4.将关键点绘制在图像上 img = cv.drawKeypoints(img,kp,None,color=(0,0,255),flags=0) plt.imshow(img[:,:,::-1]) plt.show()
ORB算法是基于FAST算法和Brief算法的,构造金字塔,为fast算法构造方向
1)实例化ORB
orb = cv.orb_create(nfeatures)
2)利用orb.detectAndCompute()检测关键地点并计算
kp,des = orb.detectAndCompute(gray,None)
3)将关键点检测结果绘制图像上
cv.drawKeypoints(img,keypoints,outpoints,color,flags)
import numpy as np import cv2 as cv import matplotlib.pyplot as plt #1.读取图像 img = cv.imread(‘image1.jpg‘) #2.实例化orb对象 orb = cv.ORB_create(nfeatures=5000)#特征点最大数量 #3.利用orb.detectAndCompute检测关键点并计算 kp,des = orb.detectAndCompute(img,None) #4.将关键点绘制在图像上 img = cv.drawKeypoints(img,kp,None,color=(0,0,255),flags=0) plt.imshow(img[:,:,::-1]) plt.show()