SURF与SIFT算法相似,SURF特征检测具有尺度不变的特点。SIFT特征点多复杂度较高。SURF运算简单,效率高。
工具/原料
python3pycharm
opencv3contribwindows7环境
方法/步骤
1、surf是sift的加强版本,具有计算简单,速度更快的特点。应用比较广。本文实现surf特征点检测。先看结果,原图使用湖南祁阳的颜真卿大唐中兴碑真迹为例。
2、importcv2ascvimportcopyimage=cv.imread('c:\\lzy.jpg')cv.imshow("image",image)image1=copy.copy(image)image2=copy.copy(image)
3、gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)cv.imshow("gray",gray)转成灰度图
4、SURF特征计算使用xfeatures2d.SURF_create完成S诔罨租磊IFT特征点初始化surf=cv.xfeatures2d.SURF_cr髫潋啜缅eate()surf.detectAndCompute实现特征点和特征点描述分别输出。keypoints,features=surf.detectAndCompute(gray,None)print(keypoints)坐标点print(keypoints[0].pt[0],keypoints[0].pt[1])特征点描述。print(features)
5、image1=cv.蟠校盯昂drawKeypoints(gray,keypoints,image1)#绘制关键点d鸡堕樱陨rawKeyPoints()参数1:image:原始图;参数2:keypoints,特征点向量,元素为KeyPoint对象,包含特征点信息;参数3:outImage;参数4:color:特征点颜色;参数5:flags:设置特征点需要不要画 DEFAULT:只绘制特征点的坐标点,显示小圆点 DRAW_OVER_OUTIMG: NOT_DRAW_SINGLE_POINTS:单点特征点不绘制 DRAW_RICH_KEYPOINTS:带有方向的圆,显示坐标,大小和方向。keypoints:angle:角度,特征点方向,对特征点点邻域梯度计算,获得方向,缺省-1。class_id:对每个特征点进行区分,缺省-1octave:从金字塔哪层提取的得到的数据。pt:特征点坐标response:该点角点的程度。size:直径
6、forkeyinkeypoints:cv.circle(image2,(int(key.pt[0]),int(key.pt[1])),1,(255,255,0),-1)采用自己绘制方式画特征点半径=1
7、修改drawKeypoints增加flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS增加方向image1=cv.drawKeypoints(gray,keypoints,image1,flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
8、小结:importcv2ascvimportcopyimage=cv.imread(争犸禀淫'c:\\lzy.jpg')cv.imshow("i罪焐芡拂mage",image)image1=copy.copy(image)image2=copy.copy(image)gray=cv.cvtColor(image,cv.COLOR_BGR2GRAY)cv.imshow("gray",gray)surf=cv.xfeatures2d.SURF_create()keypoints,features=surf.detectAndCompute(gray,None)#keypoints=surf.detect(gray,None)print(keypoints)print(keypoints[0].pt[0],keypoints[0].pt[1])print(features)image1=cv.drawKeypoints(gray,keypoints,image1)image1=cv.drawKeypoints(gray,keypoints,image1,flags=cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)forkeyinkeypoints:cv.circle(image2,(int(key.pt[0]),int(key.pt[1])),1,(255,255,0),-1)cv.imshow("image1",image1)cv.imshow("image2",image2)cv.waitKey(0)cv.destroyAllWindows()