SIFT特征具有缩放、旋转特征不变性,下载了大牛的matlab版SIFT特征提取代码,解释如下:
1.调用方法:
将文件加入matlab目录后,在主程序中有两种操作:
op1:寻找图像中的Sift特征:
- [image, descrips, locs] = sift('scene.pgm');
- showkeys(image, locs);
op2:对两幅图中的SIFT特征进行匹配:
- match('scene.pgm','book.pgm');
由于scene和book两图中有相同的一本书,但orientation和size都不同,可以发现所得结果中Sift特征检测结果非常好。
2.代码下载地址:
<span style="vertical-align: baseline; color: rgb(19, 61, 182);"><a target=_blank target="_blank" href="http://www.cs.ubc.ca/~lowe/keypoints/" style="color: rgb(19, 61, 182); text-decoration: none; vertical-align: baseline;"><span style="font-family: 'Microsoft YaHei'; font-size: 13px;">http://www.cs.ubc.ca/~lowe/keypoints/</span></a></span>
<span style="font-family: 'Microsoft YaHei'; font-size: 13px;">3.想用自己的图片进行调用:</span>
<span style="font-family: 'Microsoft YaHei'; font-size: 13px;"></span><div class="dp-highlighter bg_csharp" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; background-color: rgb(231, 229, 220); width: 936.71875px; overflow: auto; padding-top: 1px; margin: 18px 0px !important;"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; background-color: rgb(248, 248, 248); border-left-width: 3px; border-left-style: solid; border-left-color: rgb(108, 226, 108);"><strong>[csharp]</strong> <a target=_blank href="http://blog.csdn.net/abcjennifer/article/details/7365882#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_plain.gif); background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">view plain</a><a target=_blank href="http://blog.csdn.net/abcjennifer/article/details/7365882#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; background-image: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/default/ico_copy.gif); background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;">copy</a><a target=_blank href="https://code.csdn.net/snippets/135681" target="_blank" title="在CODE上查看代码片" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/135681/fork" target="_blank" title="派生到我的代码片" style="color: rgb(160, 160, 160); text-decoration: none; background-image: none; background-color: inherit; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; background-position: 0% 0%; background-repeat: no-repeat no-repeat;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 398px; top: 928px; width: 18px; height: 18px; z-index: 99;"></div></div></div><ol start="1" class="dp-c" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; background-color: rgb(255, 255, 255); color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 17.27272605895996px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">i1=imread(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'D:\Images\New\Cars\image_0001.jpg'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 17.27272605895996px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">i2=imread(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'D:\Images\New\Cars\image_0076.jpg'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 17.27272605895996px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">i11=rgb2gray(i1); </span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 17.27272605895996px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">i22=rgb2gray(i2); </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 17.27272605895996px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">imwrite(i11,<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'v1.jpg'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'quality'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,80); </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 17.27272605895996px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">imwrite(i22,<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'v2.jpg'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'quality'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,80); </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 17.27272605895996px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">match(<span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'v1.jpg'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'v2.jpg'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span></li></ol></div>
<span style="font-family: 'Microsoft YaHei'; font-size: 13px;">experiment results:</span>
<span style="font-family: 'Microsoft YaHei'; font-size: 13px;"><img src="http://hi.csdn.net/attachment/201203/18/0_1332039524thQs.gif" alt="" style="border: none; max-width: 100%;" /> </span>
<span style="font-family: 'Microsoft YaHei'; font-size: 13px;">scene</span>
book
compare result
EXP2:
C代码:
- // FeatureDetector.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "highgui.h"
- #include "cv.h"
- #include "vector"
- #include "opencv\cxcore.hpp"
- #include "iostream"
- #include "opencv.hpp"
- #include "nonfree.hpp"
- #include "showhelper.h"
- using namespace cv;
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- //Load Image
- Mat c_src1 = imread( "..\\Images\\3.jpg");
- Mat c_src2 = imread("..\\Images\\4.jpg");
- Mat src1 = imread( "..\\Images\\3.jpg", CV_LOAD_IMAGE_GRAYSCALE);
- Mat src2 = imread( "..\\Images\\4.jpg", CV_LOAD_IMAGE_GRAYSCALE);
- if( !src1.data || !src2.data )
- { std::cout<< " --(!) Error reading images " << std::endl; return -1; }
- //sift feature detect
- SiftFeatureDetector detector;
- std::vector<KeyPoint> kp1, kp2;
- detector.detect( src1, kp1 );
- detector.detect( src2, kp2 );
- SiftDescriptorExtractor extractor;
- Mat des1,des2;//descriptor
- extractor.compute(src1,kp1,des1);
- extractor.compute(src2,kp2,des2);
- Mat res1,res2;
- int drawmode = DrawMatchesFlags::DRAW_RICH_KEYPOINTS;
- drawKeypoints(c_src1,kp1,res1,Scalar::all(-1),drawmode);//在内存中画出特征点
- drawKeypoints(c_src2,kp2,res2,Scalar::all(-1),drawmode);
- cout<<"size of description of Img1: "<<kp1.size()<<endl;
- cout<<"size of description of Img2: "<<kp2.size()<<endl;
- BFMatcher matcher(NORM_L2);
- vector<DMatch> matches;
- matcher.match(des1,des2,matches);
- Mat img_match;
- drawMatches(src1,kp1,src2,kp2,matches,img_match);//,Scalar::all(-1),Scalar::all(-1),vector<char>(),drawmode);
- cout<<"number of matched points: "<<matches.size()<<endl;
- imshow("matches",img_match);
- cvWaitKey();
- cvDestroyAllWindows();
- return 0;
- }
Python代码:
http://blog.csdn.net/abcjennifer/article/details/7639681
关于sift的其他讲解:
http://blog.csdn.net/abcjennifer/article/details/7639681
http://blog.csdn.net/abcjennifer/article/details/7372880
http://blog.csdn.net/abcjennifer/article/details/7365882