當前位置:首頁 » 操作系統 » sift源碼分析

sift源碼分析

發布時間: 2022-02-28 07:35:47

⑴ 我也求一份opencv下提取圖片sift特徵的項目源碼,急用,謝謝您了

#include "stdafx.h"
#include <opencv2/opencv.hpp>
double
compareSURFDescriptors( const float* d1, const float* d2, double best, int length )
{
double total_cost = 0;
assert( length % 4 == 0 );
for( int i = 0; i < length; i += 4 )
{
double t0 = d1[i ] - d2[i ];
double t1 = d1[i+1] - d2[i+1];
double t2 = d1[i+2] - d2[i+2];
double t3 = d1[i+3] - d2[i+3];
total_cost += t0*t0 + t1*t1 + t2*t2 + t3*t3;
if( total_cost > best )
break;
}
return total_cost;
}

int
naiveNearestNeighbor( const float* vec, int laplacian,
const CvSeq* model_keypoints,
const CvSeq* model_descriptors )
{
int length = (int)(model_descriptors->elem_size/sizeof(float));
int i, neighbor = -1;
double d, dist1 = 1e6, dist2 = 1e6;
CvSeqReader reader, kreader;
cvStartReadSeq( model_keypoints, &kreader, 0 );
cvStartReadSeq( model_descriptors, &reader, 0 );

for( i = 0; i < model_descriptors->total; i++ )
{
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
const float* mvec = (const float*)reader.ptr;
CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
if( laplacian != kp->laplacian )
continue;
d = compareSURFDescriptors( vec, mvec, dist2, length );
if( d < dist1 )
{
dist2 = dist1;
dist1 = d;
neighbor = i;
}
else if ( d < dist2 )
dist2 = d;
}
if ( dist1 < 0.6*dist2 )
return neighbor;
return -1;
}

void
findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, vector<int>& ptpairs )
{
int i;
CvSeqReader reader, kreader;
cvStartReadSeq( objectKeypoints, &kreader );
cvStartReadSeq( objectDescriptors, &reader );
ptpairs.clear();

for( i = 0; i < objectDescriptors->total; i++ )
{
const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr;
const float* descriptor = (const float*)reader.ptr;
CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader );
CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors );
if( nearest_neighbor >= 0 )
{
ptpairs.push_back(i);
ptpairs.push_back(nearest_neighbor);
}
}
}

void
flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors,
const CvSeq*, const CvSeq* imageDescriptors, vector<int>& ptpairs )
{
int length = (int)(objectDescriptors->elem_size/sizeof(float));

cv::Mat m_object(objectDescriptors->total, length, CV_32F);
cv::Mat m_image(imageDescriptors->total, length, CV_32F);

// descriptors
CvSeqReader obj_reader;
float* obj_ptr = m_object.ptr<float>(0);
cvStartReadSeq( objectDescriptors, &obj_reader );
for(int i = 0; i < objectDescriptors->total; i++ )
{
const float* descriptor = (const float*)obj_reader.ptr;
CV_NEXT_SEQ_ELEM( obj_reader.seq->elem_size, obj_reader );
memcpy(obj_ptr, descriptor, length*sizeof(float));
obj_ptr += length;
}
CvSeqReader img_reader;
float* img_ptr = m_image.ptr<float>(0);
cvStartReadSeq( imageDescriptors, &img_reader );
for(int i = 0; i < imageDescriptors->total; i++ )
{
const float* descriptor = (const float*)img_reader.ptr;
CV_NEXT_SEQ_ELEM( img_reader.seq->elem_size, img_reader );
memcpy(img_ptr, descriptor, length*sizeof(float));
img_ptr += length;
}

// find nearest neighbors using FLANN
cv::Mat m_indices(objectDescriptors->total, 2, CV_32S);
cv::Mat m_dists(objectDescriptors->total, 2, CV_32F);
cv::flann::Index flann_index(m_image, cv::flann::KDTreeIndexParams(4)); // using 4 randomized kdtrees
flann_index.knnSearch(m_object, m_indices, m_dists, 2, cv::flann::SearchParams(64) ); // maximum number of leafs checked

int* indices_ptr = m_indices.ptr<int>(0);
float* dists_ptr = m_dists.ptr<float>(0);
for (int i=0;i<m_indices.rows;++i) {
if (dists_ptr[2*i]<0.6*dists_ptr[2*i+1]) {
ptpairs.push_back(i);
ptpairs.push_back(indices_ptr[2*i]);
}
}
}

/* a rough implementation for object location */
int
locatePlanarObject( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors,
const CvSeq* imageKeypoints, const CvSeq* imageDescriptors,
const CvPoint src_corners[4], CvPoint dst_corners[4] )
{
double h[9];
CvMat _h = cvMat(3, 3, CV_64F, h);
vector<int> ptpairs;
vector<CvPoint2D32f> pt1, pt2;
CvMat _pt1, _pt2;
int i, n;

#ifdef USE_FLANN
flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif

n = (int)(ptpairs.size()/2);
if( n < 4 )
return 0;

pt1.resize(n);
pt2.resize(n);
for( i = 0; i < n; i++ )
{
pt1[i] = ((CvSURFPoint*)cvGetSeqElem(objectKeypoints,ptpairs[i*2]))->pt;
pt2[i] = ((CvSURFPoint*)cvGetSeqElem(imageKeypoints,ptpairs[i*2+1]))->pt;
}

_pt1 = cvMat(1, n, CV_32FC2, &pt1[0] );
_pt2 = cvMat(1, n, CV_32FC2, &pt2[0] );
if( !cvFindHomography( &_pt1, &_pt2, &_h, CV_RANSAC, 5 ))
return 0;

for( i = 0; i < 4; i++ )
{
double x = src_corners[i].x, y = src_corners[i].y;
double Z = 1./(h[6]*x + h[7]*y + h[8]);
double X = (h[0]*x + h[1]*y + h[2])*Z;
double Y = (h[3]*x + h[4]*y + h[5])*Z;
dst_corners[i] = cvPoint(cvRound(X), cvRound(Y));
}

return 1;
}

int main(int argc, char** argv)
{
const char* object_filename = argc == 3 ? argv[1] : "box.png";
const char* scene_filename = argc == 3 ? argv[2] : "box_in_scene.png";
IplImage* object = cvLoadImage( object_filename, CV_LOAD_IMAGE_GRAYSCALE );
IplImage* image = cvLoadImage( scene_filename, CV_LOAD_IMAGE_GRAYSCALE );
if( !object || !image )
{
fprintf( stderr, "Can not load %s and/or %s\n",
object_filename, scene_filename );
exit(-1);
}

CvMemStorage* storage = cvCreateMemStorage(0);
cvNamedWindow("Object", 1);
cvNamedWindow("Object Correspond", 1);

static CvScalar colors[] =
{
{{0,0,255}},
{{0,128,255}},
{{0,255,255}},
{{0,255,0}},
{{255,128,0}},
{{255,255,0}},
{{255,0,0}},
{{255,0,255}},
{{255,255,255}}
};

IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3);
cvCvtColor( object, object_color, CV_GRAY2BGR );

CvSeq* objectKeypoints = 0, *objectDescriptors = 0;
CvSeq* imageKeypoints = 0, *imageDescriptors = 0;
int i;
CvSURFParams params = cvSURFParams(500, 1);

double tt = (double)cvGetTickCount();
cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params );
printf("Object Descriptors: %d\n", objectDescriptors->total);

cvExtractSURF( image, 0, &imageKeypoints, &imageDescriptors, storage, params );
printf("Image Descriptors: %d\n", imageDescriptors->total);
tt = (double)cvGetTickCount() - tt;

printf( "Extraction time = %gms\n", tt/(cvGetTickFrequency()*1000.));

CvPoint src_corners[4] = {{0,0}, {object->width,0}, {object->width, object->height}, {0, object->height}};
CvPoint dst_corners[4];
IplImage* correspond = cvCreateImage( cvSize(image->width, object->height+image->height), 8, 1 );
cvSetImageROI( correspond, cvRect( 0, 0, object->width, object->height ) );
cvCopy( object, correspond );
cvSetImageROI( correspond, cvRect( 0, object->height, correspond->width, correspond->height ) );
cvCopy( image, correspond );
cvResetImageROI( correspond );

#ifdef USE_FLANN
printf("Using approximate nearest neighbor search\n");
#endif

if( locatePlanarObject( objectKeypoints, objectDescriptors, imageKeypoints,
imageDescriptors, src_corners, dst_corners ))
{
for( i = 0; i < 4; i++ )
{
CvPoint r1 = dst_corners[i%4];
CvPoint r2 = dst_corners[(i+1)%4];
cvLine( correspond, cvPoint(r1.x, r1.y+object->height ),
cvPoint(r2.x, r2.y+object->height ), colors[8] );
}
}
vector<int> ptpairs;
#ifdef USE_FLANN
flannFindPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#else
findPairs( objectKeypoints, objectDescriptors, imageKeypoints, imageDescriptors, ptpairs );
#endif
for( i = 0; i < (int)ptpairs.size(); i += 2 )
{
CvSURFPoint* r1 = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, ptpairs[i] );
CvSURFPoint* r2 = (CvSURFPoint*)cvGetSeqElem( imageKeypoints, ptpairs[i+1] );
cvLine( correspond, cvPointFrom32f(r1->pt),
cvPoint(cvRound(r2->pt.x), cvRound(r2->pt.y+object->height)), colors[8] );
}

cvShowImage( "Object Correspond", correspond );
for( i = 0; i < objectKeypoints->total; i++ )
{
CvSURFPoint* r = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, i );
CvPoint center;
int radius;
center.x = cvRound(r->pt.x);
center.y = cvRound(r->pt.y);
radius = cvRound(r->size*1.2/9.*2);
cvCircle( object_color, center, radius, colors[0], 1, 8, 0 );
}
cvShowImage( "Object", object_color );

cvWaitKey(0);

cvDestroyWindow("Object");
cvDestroyWindow("Object Correspond");

return 0;
}

⑵ 緊急求助:在運行SIFT源碼時,由於是matlab和vc混編的,設置了mex後還是顯示找不到c文件。

嘗試將'imsmooth.c' 也mex,把相關文件拷到vc。編譯出來的好像是.mexw32.混編會遇到諸多問題,若還未解決,多貼出些相關信息。我現在也在做混編,不過是基於COM的。

⑶ 求基於sift特徵提取的圖像匹配代碼,最好是利用C++和opencv編寫

哈哈,我有一個基於opencv實現的sift,我把代碼貼出來,你自己看看吧~~~

void sift_detector_and_descriptors(IplImage* i_left,IplImage* i_right)
{
Mat mat_image_left=Mat(i_left,false);
Mat mat_image_right=Mat(i_right,false);
cv::SiftFeatureDetector *pDetector=new cv::SiftFeatureDetector;
pDetector->detect(mat_image_left,left_key_point);
pDetector->detect(mat_image_right,right_key_point);
Mat left_image_descriptors,right_image_descriptors;
cv::SiftDescriptorExtractor *descriptor_extractor=new cv::SiftDescriptorExtractor;
descriptor_extractor->compute(mat_image_left,left_key_point,left_image_descriptors);
descriptor_extractor->compute(mat_image_right,right_key_point,right_image_descriptors);
Mat result_l,result_r;
drawKeypoints(mat_image_left,left_key_point,result_l,Scalar::all(-1),0);
drawKeypoints(mat_image_right,right_key_point,result_r,Scalar::all(-1),0);
//imshow("result_of_left_detector_sift",result_l);
//imshow("result_of_right_detector_sift",result_r);
Mat result_of_sift_match;
BruteForceMatcher<L2<float>> matcher;
matcher.match(left_image_descriptors,right_image_descriptors,result_of_point_match);

drawMatches(mat_image_left,left_key_point,mat_image_right,right_key_point,result_of_sift_match,result_of_sift_match);
imshow("matches_of_sift",result_of_sift_match);
imwrite("matches_of_sift.jpg",result_of_sift_match);
}
void main()
{
IplImage *n_left_image=cvLoadImage("D:\\lena.jpg");
IplImage *n_right_image=cvLoadImage("D:\\lena_r.jpg");

sift_detector_and_descriptors(n_left_image,n_right_image);

cvWaitKey(0);
}

這就是核心代碼了,至於opencv所要用到的庫,你自己弄一下吧,每個人的opencv版本不一樣,這個都市不同的,希望能夠幫到你~

⑷ 計算機視覺結合sift演算法的多尺度編寫改進harris演算法,也就是多尺度harris代碼,求vs2005能運行的。

怎麼不opencv啊。。。
到新浪共享下載源碼吧!

⑸ sift演算法有什麼最新的進展

隨著多媒體技術、計算機技術迅速發展,Internet上呈現大量的圖像信息。圖像中包含了很多的物體特性,其中顏色是非常重要的特徵之一,顏色包含了圖像中更多有價值的識別信息。SIFT演算法提取圖像局部特徵,成功應用於物體識別、圖像檢索等領域。該演算法由DAVID G.L.於1999年提出[1],並於2004年進行了發展和完善[2],MIKOLAJCZYK[3]對多種描述子進行實驗分析,結果證實了SIFT描述子具有最強的魯棒性。然而這些描述子僅利用圖像的灰度信息,忽略了圖像的彩色信息。為了提高光照不變性,獲得更高的識別率,研究者提出了基於顏色不變特性的SIFT彩色描述子。目前彩色描述子主要分為基於顏色直方圖、基於顏色矩、基於SIFT三類。本文對彩色SIFT描述子進行了深入的研究,闡述了彩色SIFT描述子,給出了每種彩色描述子的性能評價。

⑹ sift的matlab代碼應該怎麼看呢怎麼運行呢

主要就是一個match函數
I_sence=imread('e:\SIFT\road_sign\danger_w.jpg');
I_object=imread('e:\SIFT\road_sign\sign5.jpg');
I_sence=rgb2gray(I_sence);
I_object=rgb2gray(I_object);
imwrite(I_sence,'e:\SIFT\road_sign\danger_w_gray.gif');
imwrite(I_object,'e:\SIFT\road_sign\sign5_gray.gif');

match('e:\SIFT\road_sign\sign5_gray.gif','e:\SIFT\road_sign\danger_w_gray.gif');
我自己寫的例子
就一句話管用—— match, 他都封裝好了 估計看不出來怎麼弄的吧?

⑺ 求opencv實現sift演算法的程序

到底是使用還是實現,實現的話直接看opencv的源碼就好了。
使用的話方法不一,傳送門:
http://www.cnblogs.com/tornadomeet/archive/2012/03/08/2384843.html

⑻ opencv里,用sift和surf進行跟蹤的這段源碼怎麼理解

沒看過這個源碼,猜的
1 這里應該是只用了距離的部分,沒有用旋轉的部分。只為了求中心點位置,所以最後畫出來的框應該沒有角度傾斜的。
2 不知道
3
OpenCV中的SIFT SURF都很慢,做不到實時的。SIFT的特徵點提取太慢了,而且描述默認128個float導致匹配也比較慢,除非修改演算法部分。如果是跟蹤的話,用OpenCV的KLT光流,或者模版匹配都能快很多(20ms以內)。

findHomography這個最後一個參數,可以修改為RANSAC或者PROSAC的實現版本。比LMEDS快好多倍。

⑼ 如何用matlab運行sift演算法

具體程序要具體分析的,請把程序發過來,調試一次就知道了

⑽ 各位大神,求一份尺度不變特徵變換(SIFT演算法)MATLAB實現的代碼,最好有注釋,小弟剛剛起步,謝謝了!

附件中是sift的matlab實現代碼,在matlab中直接點擊運行do_demo_3.m即可實現圖像匹配

do_demo_1.m可以顯示sift特徵點

具體的詳細用法你可以研究一下代碼

這份代碼是我目前在網上找到的最簡潔的代碼

希望對你能有所幫助


熱點內容
qq空間上傳視頻要什麼格式的 發布:2024-12-23 11:05:56 瀏覽:593
百度雲伺服器怎樣 發布:2024-12-23 11:02:21 瀏覽:644
pythonlinux推薦 發布:2024-12-23 10:58:54 瀏覽:56
pythonurllib2沒有了 發布:2024-12-23 10:57:38 瀏覽:606
常考演算法 發布:2024-12-23 10:53:04 瀏覽:303
循跡小車演算法 發布:2024-12-22 22:28:41 瀏覽:82
scss一次編譯一直生成隨機數 發布:2024-12-22 22:04:24 瀏覽:956
嫁接睫毛加密 發布:2024-12-22 21:50:12 瀏覽:975
linuxbin文件的安裝 發布:2024-12-22 21:46:07 瀏覽:798
vlcforandroid下載 發布:2024-12-22 21:45:26 瀏覽:664