00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <vector>
00026 #include <locale.h>
00027 #include <SiftGPU.h>
00028 #include <QtOpenGL>
00029
00030 #include <qvsiftgpu.h>
00031
00032 static bool firstTime = TRUE;
00033 static SiftGPU *sift = NULL;
00034
00035 bool resetSiftGPU()
00036 {
00037 delete sift;
00038
00039 firstTime = TRUE;
00040 sift = NULL;
00041 return true;
00042 }
00043
00044 const QList<QVSiftFeature> getSiftGPUFeatures(const QVImage<uChar> &image,
00045 nvidia_method method,
00046 bool sd, double f, double w, double dw,
00047 int fo, double t, double e, int d, int v)
00048 {
00049
00050
00051 if (sift == NULL)
00052 sift = new SiftGPU();
00053
00054
00055 setlocale(LC_NUMERIC,"C");
00056
00057
00058 char * t_argv[] = { (char*) "", (char*) "-f", (char*) "4.0", (char*) "-w", (char*) "2.0",
00059 (char*) "-dw", (char*) "3.0", (char*) "-fo", (char*) "0",
00060 (char*) "-t", (char*) "0.00666", (char*) "-e", (char*) "10.0",
00061 (char*) "-d", (char*) "3",
00062 (char*) "-v", (char*) "0",
00063 (char*) "-sign", (char*) "-sd" };
00064
00065 int t_argc = sizeof(t_argv)/sizeof(char*);
00066
00067
00068 switch(method)
00069 {
00070 case CUDA_METHOD:
00071 t_argv[0] = (char*) "-cuda";
00072 break;
00073 case GLSL_METHOD:
00074 t_argv[0] = (char*) "-glsl";
00075 break;
00076 case CG_METHOD:
00077 t_argv[0] = (char*) "-cg";
00078 break;
00079 }
00080
00081
00082 if(not sd)
00083 t_argc--;
00084
00085
00086 QString aux_f = QString::number(f,'f',6);
00087 QString aux_w = QString::number(w,'f',6);
00088 QString aux_dw = QString::number(dw,'f',6);
00089 QString aux_fo = QString::number(fo);
00090 QString aux_t = QString::number(t,'f',6);
00091 QString aux_e = QString::number(e,'f',6);
00092 QString aux_d = QString::number(d);
00093 QString aux_v = QString::number(v);
00094
00095 t_argv[2] = new char[50]; strcpy(t_argv[2],aux_f.toLatin1().data());
00096 t_argv[4] = new char[50]; strcpy(t_argv[4],aux_w.toLatin1().data());
00097 t_argv[6] = new char[50]; strcpy(t_argv[6],aux_dw.toLatin1().data());
00098 t_argv[8] = new char[50]; strcpy(t_argv[8],aux_fo.toLatin1().data());
00099 t_argv[10] = new char[50]; strcpy(t_argv[10],aux_t.toLatin1().data());
00100 t_argv[12] = new char[50]; strcpy(t_argv[12],aux_e.toLatin1().data());
00101 t_argv[14] = new char[50]; strcpy(t_argv[14],aux_d.toLatin1().data());
00102 t_argv[16] = new char[50]; strcpy(t_argv[16],aux_v.toLatin1().data());
00103
00104 sift->ParseParam(t_argc, t_argv);
00105
00106 delete [] t_argv[2];
00107 delete [] t_argv[4];
00108 delete [] t_argv[6];
00109 delete [] t_argv[8];
00110 delete [] t_argv[10];
00111 delete [] t_argv[12];
00112 delete [] t_argv[14];
00113 delete [] t_argv[16];
00114
00115
00116 if(firstTime)
00117 {
00118 if(sift->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED)
00119 std::cerr << "Falla inicialización ContextGL\n";
00120 firstTime = FALSE;
00121 }
00122
00123
00124 int width = image.getStep(), height = image.getRows();
00125 const unsigned char *data = image.getReadData();
00126 sift->RunSIFT(width,height,data,GL_LUMINANCE,GL_UNSIGNED_BYTE);
00127
00128
00129 int num = sift->GetFeatureNum();
00130 std::vector<float> descriptors(128*num);
00131 std::vector<SiftGPU::SiftKeypoint> keys(num);
00132 sift->GetFeatureVector(&keys[0],&descriptors[0]);
00133 QList<QVSiftFeature> feature_list;
00134 for(int i=0;i<num;i++)
00135 {
00136 QVKeypoint keypoint;
00137 QVVector descriptor(128);
00138
00139 keypoint.x() = keys[i].x;
00140 keypoint.y() = keys[i].y;
00141 keypoint.scale() = keys[i].s;
00142 keypoint.orientation() = keys[i].o;
00143
00144 for(int j=0;j<128;j++)
00145 descriptor[j] = descriptors[128*i+j];
00146
00147 feature_list.append( QVSiftFeature(keypoint, descriptor) );
00148 }
00149
00150 return feature_list;
00151 }
00152
00153
00154 QList< QPair<int, int> > matchSiftGPUFeatures(const QList<QVSiftFeature> img1features, const QList<QVSiftFeature> img2features, const float distmax, const float ratiomax, const bool mutual_best_match)
00155 {
00156 if (sift == NULL)
00157 sift = new SiftGPU();
00158
00159 if(firstTime)
00160 {
00161 if(sift->CreateContextGL() != SiftGPU::SIFTGPU_FULL_SUPPORTED)
00162 std::cerr << "Falla inicialización ContextGL\n";
00163 firstTime = FALSE;
00164 }
00165
00166 const int size1 = img1features.size(),
00167 size2 = img2features.size(),
00168 minSize = MIN(size1, size2);
00169
00170 float des1[128*size1], des2[128*size2];
00171
00172 for(int i=0;i<size1;i++)
00173 for(int j=0;j<128;j++)
00174 des1[i*128+j] = img1features[i].descriptor[j];
00175
00176 for(int i=0;i<size2;i++)
00177 for(int j=0;j<128;j++)
00178 des2[i*128+j] = img2features[i].descriptor[j];
00179
00180 SiftMatchGPU matcher(minSize);
00181 if(matcher.VerifyContextGL() == 0)
00182 {
00183 std::cout << "Error in matcher. VerifyContextGL\n";
00184 return QList<QPair<int, int> >();
00185 }
00186
00187 matcher.SetDescriptors(0, size1, des1);
00188 matcher.SetDescriptors(1, size2, des2);
00189
00190 int match_buf[minSize][2];
00191 int num_matchings = matcher.GetSiftMatch(minSize, match_buf, distmax, ratiomax, mutual_best_match);
00192
00193 QList< QPair<int, int> > matchings;
00194 for(int i = 0; i < num_matchings; i++)
00195 matchings << QPair<int, int>(match_buf[i][0], match_buf[i][1]);
00196
00197 return matchings;
00198 }
00199