程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2024-11(3)

特征点检测算法

发布于2020-03-21 17:31     阅读(3269)     评论(0)     点赞(9)     收藏(0)


特征点检测算法

Harris角点检测

cv2.cornerHarris(gray,blockSize=4,ksize=5,k=0.04)
  1. import cv2
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. def cv_show(name,image):
  5. """图像显示函数
  6. name:字符串,窗口名称
  7. img:numpy.ndarray,图像
  8. """
  9. cv2.namedWindow(name,cv2.WINDOW_NORMAL)
  10. cv2.imshow(name,image)
  11. cv2.waitKey(0)
  12. cv2.destroyAllWindows()
  13. if __name__=="__main__":
  14. img = cv2.imread('.\data\Box.jpg')
  15. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  16. gray = np.float32(gray)
  17. # 角点检测
  18. dst = cv2.cornerHarris(gray,blockSize=4,ksize=5,k=0.04)
  19. # 对角点进行膨胀操作
  20. dst = cv2.dilate(dst,None)
  21. img[dst>0.01*dst.max()] = [0,0,255]
  22. # 显示图像
  23. cv_show('Harris Corners',img)
 

                           14657665-cec9e2e3c8cdebe8.jpg

 

SIFT特征检测

  1. import cv2
  2. import numpy as np
  3. def cv_show(name,image):
  4. """图像显示函数
  5. name:字符串,窗口名称
  6. img:numpy.ndarray,图像
  7. """
  8. cv2.namedWindow(name,cv2.WINDOW_NORMAL)
  9. cv2.imshow(name,image)
  10. cv2.waitKey(0)
  11. cv2.destroyAllWindows()
  12. if __name__=="__main__":
  13. img = cv2.imread('.\data\975-1.jpg')
  14. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  15. sift = cv2.xfeatures2d.SIFT_create()
  16. keypoints,descriptor = sift.detectAndCompute(gray,None)
  17. # 在图上绘制关键点
  18. img = cv2.drawKeypoints(image=img,outImage=img,keypoints=keypoints,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,color=(255,255,0))
  19. cv_show('sift_keypoints',img)
  20. cv2.imwrite('.\opencv_python\SIFT.jpg',img)
 
           14657665-58d1f8f6addb5b05.jpg
 

SURF特征检测

  1. import cv2
  2. import numpy as np
  3. def cv_show(name,image):
  4. """图像显示函数
  5. name:字符串,窗口名称
  6. img:numpy.ndarray,图像
  7. """
  8. cv2.namedWindow(name,cv2.WINDOW_NORMAL)
  9. cv2.imshow(name,image)
  10. cv2.waitKey(0)
  11. cv2.destroyAllWindows()
  12. if __name__=="__main__":
  13. img = cv2.imread('.\data\975-1.jpg')
  14. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  15. surf = cv2.xfeatures2d.SURF_create(8000)
  16. keypoints,descriptor = surf.detectAndCompute(gray,None)
  17. img = cv2.drawKeypoints(image=img,outImage=img,keypoints=keypoints,flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS,color=(255,255,0))
  18. cv_show('surf_keypoints',img)
  19. cv2.imwrite('.\opencv_python\SURF.jpg',img)
 
          14657665-0268a937d70cb23c.jpg
 

ORB特征检测

  1. def img_show(name,image):
  2. """matplotlib图像显示函数
  3. name:字符串,图像标题
  4. img:numpy.ndarray,图像
  5. """
  6. if len(image.shape) == 3:
  7. image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
  8. plt.imshow(image,'gray')
  9. plt.xticks([])
  10. plt.yticks([])
  11. plt.xlabel(name,fontproperties='FangSong',fontsize=12)
  12. if __name__=="__main__":
  13. img1 = cv2.imread('.\data\974-1.jpg')
  14. img2 = cv2.imread('.\data\975-1.jpg')
  15. gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
  16. gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
  17. orb = cv2.ORB_create()
  18. kp1,des1 = orb.detectAndCompute(gray1,None)
  19. kp2,des2 = orb.detectAndCompute(gray2,None)
  20. bf = cv2.BFMatcher(cv2.NORM_HAMMING,crossCheck=True)
  21. matches = bf.match(des1,des2)
  22. matches = sorted(matches,key=lambda x:x.distance)
  23. img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:80],img2,flags=2)
  24. plt.figure(figsize=(60,80),dpi=80)
  25. img_show('',img3)
 
14657665-ac29796f5d2d1b6d.png
 

原文链接:https://blog.csdn.net/qq_28368377/article/details/104985249



所属网站分类: 技术文章 > 博客

作者:sjhjf0293

链接:https://www.pythonheidong.com/blog/article/274511/1c5501df28d22f1847bf/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

9 0
收藏该文
已收藏

评论内容:(最多支持255个字符)