当前位置:首页 » 操作系统 » 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特征点

具体的详细用法你可以研究一下代码

这份代码是我目前在网上找到的最简洁的代码

希望对你能有所帮助


热点内容
安卓我的世界114版本有什么 发布:2024-12-23 14:42:17 浏览:707
vbox源码 发布:2024-12-23 14:41:32 浏览:274
诗经是怎么存储 发布:2024-12-23 14:41:29 浏览:656
屏蔽视频广告脚本 发布:2024-12-23 14:41:24 浏览:416
php解析pdf 发布:2024-12-23 14:40:01 浏览:815
多看阅读上传 发布:2024-12-23 14:34:05 浏览:176
编程高性能 发布:2024-12-23 14:33:31 浏览:114
电脑加装固态后需要怎么配置 发布:2024-12-23 14:20:21 浏览:508
如何在服务器上进行序列比对 发布:2024-12-23 14:15:25 浏览:284
ga6选哪个配置车 发布:2024-12-23 14:13:36 浏览:274