特征点检测算法

作者: 陨星落云 | 来源:发表于2020-03-19 20:44 被阅读0次

    特征点检测算法

    Harris角点检测

    cv2.cornerHarris(gray,blockSize=4,ksize=5,k=0.04)
    
    import cv2
    import matplotlib.pyplot as plt
    import numpy as np
    
    def cv_show(name,image):
        """图像显示函数
        name:字符串,窗口名称
        img:numpy.ndarray,图像
        """
        cv2.namedWindow(name,cv2.WINDOW_NORMAL)
        cv2.imshow(name,image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    if __name__=="__main__":
        img = cv2.imread('.\data\Box.jpg')
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        gray = np.float32(gray)
        # 角点检测
        dst = cv2.cornerHarris(gray,blockSize=4,ksize=5,k=0.04)
        # 对角点进行膨胀操作
        dst = cv2.dilate(dst,None)
        img[dst>0.01*dst.max()] = [0,0,255]
        # 显示图像
        cv_show('Harris Corners',img)
    
    harris_crners.jpg

    SIFT特征检测

    import cv2
    import numpy as np
    
    def cv_show(name,image):
        """图像显示函数
        name:字符串,窗口名称
        img:numpy.ndarray,图像
        """
        cv2.namedWindow(name,cv2.WINDOW_NORMAL)
        cv2.imshow(name,image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    
    if __name__=="__main__":
        img = cv2.imread('.\data\975-1.jpg')
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        
        sift = cv2.xfeatures2d.SIFT_create()
        keypoints,descriptor = sift.detectAndCompute(gray,None)
        # 在图上绘制关键点
        img = cv2.drawKeypoints(image=img,outImage=img,keypoints=keypoints,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,color=(255,255,0))
        cv_show('sift_keypoints',img)
        cv2.imwrite('.\opencv_python\SIFT.jpg',img)
    
    SIFT.jpg

    SURF特征检测

    import cv2
    import numpy as np
    
    def cv_show(name,image):
        """图像显示函数
        name:字符串,窗口名称
        img:numpy.ndarray,图像
        """
        cv2.namedWindow(name,cv2.WINDOW_NORMAL)
        cv2.imshow(name,image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
    if __name__=="__main__":
        img = cv2.imread('.\data\975-1.jpg')
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        
        surf = cv2.xfeatures2d.SURF_create(8000)
        keypoints,descriptor = surf.detectAndCompute(gray,None)
        
        img = cv2.drawKeypoints(image=img,outImage=img,keypoints=keypoints,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,color=(255,255,0))
        cv_show('surf_keypoints',img)
        cv2.imwrite('.\opencv_python\SURF.jpg',img)
    
    SURF.jpg

    ORB特征检测

    def img_show(name,image):
        """matplotlib图像显示函数
        name:字符串,图像标题
        img:numpy.ndarray,图像
        """
        if len(image.shape) == 3:
            image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
        plt.imshow(image,'gray')
        plt.xticks([])
        plt.yticks([])
        plt.xlabel(name,fontproperties='FangSong',fontsize=12)
     
    if __name__=="__main__":
        img1 = cv2.imread('.\data\974-1.jpg')
        img2 = cv2.imread('.\data\975-1.jpg')
        gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
        gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
        orb = cv2.ORB_create()
        kp1,des1 = orb.detectAndCompute(gray1,None)
        kp2,des2 = orb.detectAndCompute(gray2,None)
        bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
        matches = bf.match(des1,des2)
        matches = sorted(matches,key=lambda x:x.distance)
        img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:80],img2,flags=2)
        plt.figure(figsize=(60,80),dpi=80)
        img_show('',img3)
        
    
    orb.png

    相关文章

      网友评论

        本文标题:特征点检测算法

        本文链接:https://www.haomeiwen.com/subject/zilryhtx.html