SiftMatch.cpp
Go to the documentation of this file.
00001 
00002 //      File:           SiftMatch.cpp
00003 //      Author:         Changchang Wu
00004 //      Description :   implementation of SiftMatchGPU and SiftMatchGL
00005 //
00006 //
00007 //      Copyright (c) 2007 University of North Carolina at Chapel Hill
00008 //      All Rights Reserved
00009 //
00010 //      Permission to use, copy, modify and distribute this software and its
00011 //      documentation for educational, research and non-profit purposes, without
00012 //      fee, and without a written agreement is hereby granted, provided that the
00013 //      above copyright notice and the following paragraph appear in all copies.
00014 //      
00015 //      The University of North Carolina at Chapel Hill make no representations
00016 //      about the suitability of this software for any purpose. It is provided
00017 //      'as is' without express or implied warranty. 
00018 //
00019 //      Please send BUG REPORTS to ccwu@cs.unc.edu
00020 //
00022 
00023 #include "GL/glew.h"
00024 #include <iostream>
00025 #include <iomanip>
00026 #include <vector>
00027 #include <sstream>
00028 #include <algorithm>
00029 using namespace std;
00030 #include <string.h>
00031 #include "GlobalUtil.h"
00032 
00033 #include "ProgramGLSL.h"
00034 #include "GLTexImage.h"
00035 #include "SiftGPU.h"
00036 #include "SiftMatch.h"
00037 #include "FrameBufferObject.h"
00038 
00039 #if defined(CUDA_SIFTGPU_ENABLED)
00040 #include "CuTexImage.h"
00041 #include "SiftMatchCU.h"
00042 #endif
00043 
00044 
00045 SiftMatchGL::SiftMatchGL(int max_sift, int use_glsl): SiftMatchGPU()
00046 {
00047         s_multiply = s_col_max = s_row_max = s_guided_mult = NULL;
00048         _num_sift[0] = _num_sift[1] = 0;
00049         _id_sift[0] = _id_sift[1] = 0;
00050         _have_loc[0] = _have_loc[1] = 0;
00051         _max_sift = max_sift <=0 ? 4096 : ((max_sift + 31)/ 32 * 32) ; 
00052         _pixel_per_sift = 32; //must be 32
00053         _sift_num_stripe = 1; 
00054         _sift_per_stripe = 1;
00055         _sift_per_row = _sift_per_stripe * _sift_num_stripe;
00056         _initialized = 0;
00057 }
00058 
00059 SiftMatchGL::~SiftMatchGL()
00060 {
00061         if(s_multiply) delete s_multiply;
00062         if(s_guided_mult) delete s_guided_mult;
00063         if(s_col_max) delete s_col_max;
00064         if(s_row_max) delete s_row_max;
00065 }
00066 
00067 void SiftMatchGL::SetMaxSift(int max_sift)
00068 {
00069         
00070         max_sift = ((max_sift + 31)/32)*32;
00071         if(max_sift > GlobalUtil::_texMaxDimGL) max_sift = GlobalUtil::_texMaxDimGL;
00072         if(max_sift > _max_sift)
00073         {
00074                 _max_sift = max_sift;
00075                 AllocateSiftMatch();
00076                 _have_loc[0] = _have_loc[1] = 0;
00077                 _id_sift[0] = _id_sift[1] = -1;
00078                 _num_sift[0] = _num_sift[1] = 1;
00079         }else
00080         {
00081                 _max_sift = max_sift;
00082         }
00083 
00084 }
00085 
00086 void SiftMatchGL::AllocateSiftMatch()
00087 {
00088         //parameters, number of sift is limited by the texture size
00089         if(_max_sift > GlobalUtil::_texMaxDimGL) _max_sift = GlobalUtil::_texMaxDimGL;  
00091         int h = _max_sift / _sift_per_row; 
00092         int n = (GlobalUtil::_texMaxDimGL + h - 1) / GlobalUtil::_texMaxDimGL; 
00093         if ( n > 1) {_sift_num_stripe *= n; _sift_per_row *= n; }
00094 
00095         //initialize
00096 
00097         _texDes[0].InitTexture(_sift_per_row * _pixel_per_sift, _max_sift / _sift_per_row, 0,GL_RGBA8);
00098         _texDes[1].InitTexture(_sift_per_row * _pixel_per_sift, _max_sift / _sift_per_row, 0, GL_RGBA8);
00099         _texLoc[0].InitTexture(_sift_per_row , _max_sift / _sift_per_row, 0);
00100         _texLoc[1].InitTexture(_sift_per_row , _max_sift / _sift_per_row, 0);
00101 
00102         if(GlobalUtil::_SupportNVFloat || GlobalUtil::_SupportTextureRG)
00103         {
00104                 //use single-component texture to save memory
00105 #ifndef GL_R32F
00106 #define GL_R32F 0x822E
00107 #endif
00108                 GLuint format = GlobalUtil::_SupportNVFloat ? GL_FLOAT_R_NV : GL_R32F;
00109                 _texDot.InitTexture(_max_sift, _max_sift, 0, format);
00110                 _texMatch[0].InitTexture(16, _max_sift / 16, 0, format);
00111                 _texMatch[1].InitTexture(16, _max_sift / 16, 0, format);
00112         }else
00113         {
00114                 _texDot.InitTexture(_max_sift, _max_sift, 0);
00115                 _texMatch[0].InitTexture(16, _max_sift / 16, 0);
00116                 _texMatch[1].InitTexture(16, _max_sift / 16, 0);
00117         }
00118 
00119 }
00120 void SiftMatchGL::InitSiftMatch()
00121 {
00122         if(_initialized) return;
00123         GlobalUtil::InitGLParam(0);
00124         if(GlobalUtil::_GoodOpenGL == 0) return;
00125         AllocateSiftMatch();
00126         LoadSiftMatchShadersGLSL();
00127         _initialized = 1; 
00128 }
00129 
00130 
00131 void SiftMatchGL::SetDescriptors(int index, int num, const unsigned char* descriptors, int id)
00132 {       
00133         if(_initialized == 0) return;
00134         if (index > 1) index = 1;
00135         if (index < 0) index = 0;
00136         _have_loc[index] = 0;
00137 
00138         //the same feature is already set
00139         if(id !=-1 && id == _id_sift[index]) return ;
00140         _id_sift[index] = id;
00141 
00142         if(num > _max_sift) num = _max_sift;
00143 
00144         sift_buffer.resize(num * 128 /4);
00145         memcpy(&sift_buffer[0], descriptors, 128 * num);
00146         _num_sift[index] = num; 
00147         int w = _sift_per_row * _pixel_per_sift;
00148         int h = (num + _sift_per_row  - 1)/ _sift_per_row; 
00149         sift_buffer.resize(w * h * 4, 0);
00150         _texDes[index].SetImageSize(w , h);
00151         _texDes[index].BindTex(); 
00152         if(_sift_num_stripe == 1)
00153         {
00154                 glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_RGBA,  GL_UNSIGNED_BYTE, &sift_buffer[0]);
00155         }else
00156         {
00157                 for(int i = 0; i < _sift_num_stripe; ++i)
00158                 {
00159                         int ws = _sift_per_stripe * _pixel_per_sift;
00160                         int x = i * ws;
00161                         int pos = i * ws * h * 4; 
00162                         glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[pos]);
00163                 }
00164         }
00165         _texDes[index].UnbindTex();
00166 
00167 }
00168 
00169 void SiftMatchGL::SetFeautreLocation(int index, const float* locations, int gap)
00170 {
00171         if(_num_sift[index] <=0) return;
00172         int w = _sift_per_row ;
00173         int h = (_num_sift[index] + _sift_per_row  - 1)/ _sift_per_row; 
00174         sift_buffer.resize(_num_sift[index] * 2);
00175         if(gap == 0)
00176         {
00177                 memcpy(&sift_buffer[0], locations, _num_sift[index] * 2 * sizeof(float));
00178         }else
00179         {
00180                 for(int i = 0; i < _num_sift[index]; ++i)
00181                 {
00182                         sift_buffer[i*2] = *locations++;
00183                         sift_buffer[i*2+1]= *locations ++;
00184                         locations += gap;
00185                 }
00186         }
00187         sift_buffer.resize(w * h * 2, 0);
00188         _texLoc[index].SetImageSize(w , h);
00189         _texLoc[index].BindTex(); 
00190         if(_sift_num_stripe == 1)
00191         {
00192                 glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_LUMINANCE_ALPHA , GL_FLOAT , &sift_buffer[0]);
00193         }else
00194         {
00195                 for(int i = 0; i < _sift_num_stripe; ++i)
00196                 {
00197                         int ws = _sift_per_stripe;
00198                         int x = i * ws;
00199                         int pos = i * ws * h * 2; 
00200                         glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_LUMINANCE_ALPHA , GL_FLOAT, &sift_buffer[pos]);
00201                 }
00202         }
00203         _texLoc[index].UnbindTex();
00204         _have_loc[index] = 1;
00205 }
00206 
00207 void SiftMatchGL::SetDescriptors(int index, int num, const float* descriptors, int id)
00208 {       
00209         if(_initialized == 0) return;
00210         if (index > 1) index = 1;
00211         if (index < 0) index = 0;
00212         _have_loc[index] = 0;
00213 
00214         //the same feature is already set
00215         if(id !=-1 && id == _id_sift[index]) return ;
00216         _id_sift[index] = id; 
00217 
00218         if(num > _max_sift) num = _max_sift;
00219 
00220         sift_buffer.resize(num * 128 /4);
00221         unsigned char * pub = (unsigned char*) &sift_buffer[0];
00222         for(int i = 0; i < 128 * num; ++i)
00223         {
00224                 pub[i] = int(512 * descriptors[i] + 0.5);
00225         }
00226         _num_sift[index] = num; 
00227         int w = _sift_per_row * _pixel_per_sift;
00228         int h = (num + _sift_per_row  - 1)/ _sift_per_row; 
00229         sift_buffer.resize(w * h * 4, 0);
00230         _texDes[index].SetImageSize(w, h);
00231         _texDes[index].BindTex();
00232         if(_sift_num_stripe == 1)
00233         {
00234                 glTexSubImage2D(GlobalUtil::_texTarget, 0, 0, 0, w, h, GL_RGBA,  GL_UNSIGNED_BYTE, &sift_buffer[0]);
00235         }else
00236         {
00237                 for(int i = 0; i < _sift_num_stripe; ++i)
00238                 {
00239                         int ws = _sift_per_stripe * _pixel_per_sift;
00240                         int x = i * ws;
00241                         int pos = i * ws * h * 4; 
00242                         glTexSubImage2D(GlobalUtil::_texTarget, 0, x, 0, ws, h, GL_RGBA, GL_UNSIGNED_BYTE, &sift_buffer[pos]);
00243                 }
00244         }
00245         _texDes[index].UnbindTex();
00246 }
00247 
00248 
00249 void SiftMatchGL::LoadSiftMatchShadersGLSL()
00250 {
00251         ProgramGLSL * program;
00252         ostringstream out;
00253         if(GlobalUtil::_IsNvidia)
00254         out <<  "#pragma optionNV(ifcvt none)\n"
00255                         "#pragma optionNV(unroll all)\n";
00256 
00257     out <<  "#define SIFT_PER_STRIPE " << _sift_per_stripe << ".0\n" 
00258                         "#define PIXEL_PER_SIFT " << _pixel_per_sift << "\n"
00259                         "uniform sampler2DRect tex1, tex2; uniform vec2 size;\n"
00260                         "void main()            \n"
00261                     "{\n"
00262                 <<      "   vec4 val = vec4(0.0, 0.0, 0.0, 0.0), data1, buf;\n"
00263                         "   vec2 index = gl_FragCoord.yx; \n"
00264                         "   vec2 stripe_size = size.xy * SIFT_PER_STRIPE;\n"
00265                         "       vec2 temp_div1 = index / stripe_size;\n"
00266                         "   vec2 stripe_index = floor(temp_div1);\n"
00267                         "   index = floor(stripe_size * (temp_div1 - stripe_index));\n"
00268                         "       vec2 temp_div2 = index * vec2(1.0 / float(SIFT_PER_STRIPE));\n"
00269                         "       vec2 temp_floor2 = floor(temp_div2);\n"
00270                         "   vec2 index_v = temp_floor2 + vec2(0.5);\n "
00271                         "   vec2 index_h = vec2(SIFT_PER_STRIPE)* (temp_div2 - temp_floor2);\n"
00272                         "   vec2 tx = (index_h + stripe_index * vec2(SIFT_PER_STRIPE))* vec2(PIXEL_PER_SIFT) + 0.5;\n"
00273                         "   vec2 tpos1, tpos2; \n"
00274                         "       vec4 tpos = vec4(tx, index_v);\n"
00276                         "   for(int i = 0; i < PIXEL_PER_SIFT; ++i){\n"
00277                         "               buf = texture2DRect(tex2, tpos.yw);\n"
00278                         "               data1 = texture2DRect(tex1, tpos.xz);\n"
00279                         "               val += (data1 * buf);\n"
00280                         "               tpos.xy = tpos.xy + vec2(1.0, 1.0);\n"
00281                         "       }\n"
00282                         "       const float factor = 0.248050689697265625; \n"
00283                         "       gl_FragColor =vec4(dot(val, vec4(factor)), index,  0);\n"
00284                         "}"
00285                 <<      '\0';
00286 
00287         s_multiply = program= new ProgramGLSL(out.str().c_str()); 
00288 
00289         _param_multiply_tex1 = glGetUniformLocation(*program, "tex1");
00290         _param_multiply_tex2 = glGetUniformLocation(*program, "tex2");
00291         _param_multiply_size = glGetUniformLocation(*program, "size");
00292 
00293         out.seekp(ios::beg);
00294     if(GlobalUtil::_IsNvidia)
00295     out <<  "#pragma optionNV(ifcvt none)\n"
00296                         "#pragma optionNV(unroll all)\n";
00297 
00298     out <<  "#define SIFT_PER_STRIPE " << _sift_per_stripe << ".0\n" 
00299                         "#define PIXEL_PER_SIFT " << _pixel_per_sift << "\n"
00300                         "uniform sampler2DRect tex1, tex2;\n"
00301                         "uniform sampler2DRect texL1;\n"
00302                         "uniform sampler2DRect texL2; \n"
00303                         "uniform mat3 H; \n"
00304                         "uniform mat3 F; \n"
00305                         "uniform vec4   size; \n"
00306                         "void main()            \n"
00307                     "{\n"
00308                 <<      "   vec4 val = vec4(0.0, 0.0, 0.0, 0.0), data1, buf;\n"
00309                         "   vec2 index = gl_FragCoord.yx; \n"
00310                         "   vec2 stripe_size = size.xy * SIFT_PER_STRIPE;\n"
00311                         "       vec2 temp_div1 = index / stripe_size;\n"
00312                         "   vec2 stripe_index = floor(temp_div1);\n"
00313                         "   index = floor(stripe_size * (temp_div1 - stripe_index));\n"
00314                         "       vec2 temp_div2 = index  * vec2(1.0/ float(SIFT_PER_STRIPE));\n"
00315                         "       vec2 temp_floor2 = floor(temp_div2);\n"
00316                         "   vec2 index_v = temp_floor2 + vec2(0.5);\n "
00317                         "   vec2 index_h = vec2(SIFT_PER_STRIPE)* (temp_div2 - temp_floor2);\n"
00318                         
00319                         //read feature location data
00320                         "   vec4 tlpos = vec4((index_h + stripe_index * vec2(SIFT_PER_STRIPE)) + 0.5, index_v);\n"
00321                         "   vec3 loc1 = vec3(texture2DRect(texL1, tlpos.xz).xw, 1.0);\n"
00322                         "   vec3 loc2 = vec3(texture2DRect(texL2, tlpos.yw).xw, 1.0);\n"
00323                         
00324                         //check the guiding homography
00325                         "   vec3 hxloc1 = H* loc1;\n"
00326                         "   vec2 diff = abs(loc2.xy- (hxloc1.xy/hxloc1.z));\n"
00327                         "   float disth = max(diff.x, diff.y);\n"
00328                         "   if(disth > size.z ) {gl_FragColor = vec4(0.0, index, 0.0); return;}\n"
00329 
00330                         //check the guiding fundamental 
00331                         "   vec3 fx1 = (F * loc1), ftx2 = (loc2 * F);\n"
00332                         "   float x2tfx1 = dot(loc2, fx1);\n"
00333                         "   vec4 temp = vec4(fx1.xy, ftx2.xy); \n"
00334                         "   float sampson_error = (x2tfx1 * x2tfx1) / dot(temp, temp);\n"
00335                         "   if(sampson_error > size.w) {gl_FragColor = vec4(0.0, index, 0.0); return;}\n"
00336 
00337                         //compare feature descriptor
00338                         "   vec2 tx = (index_h + stripe_index * SIFT_PER_STRIPE)* vec2(PIXEL_PER_SIFT) + 0.5;\n"
00339                         "   vec2 tpos1, tpos2; \n"
00340                         "       vec4 tpos = vec4(tx, index_v);\n"
00341                         "   for(int i = 0; i < PIXEL_PER_SIFT; ++i){\n"
00342                         "               buf = texture2DRect(tex2, tpos.yw);\n"
00343                         "               data1 = texture2DRect(tex1, tpos.xz);\n"
00344                         "               val += data1 * buf;\n"
00345                         "               tpos.xy = tpos.xy + vec2(1.0, 1.0);\n"
00346                         "       }\n"
00347                         "       const float factor = 0.248050689697265625; \n"
00348                         "       gl_FragColor =vec4(dot(val, vec4(factor)), index,  0.0);\n"
00349                         "}"
00350                 <<      '\0';
00351 
00352         s_guided_mult = program= new ProgramGLSL(out.str().c_str());
00353 
00354         _param_guided_mult_tex1 = glGetUniformLocation(*program, "tex1");
00355         _param_guided_mult_tex2= glGetUniformLocation(*program, "tex2");
00356         _param_guided_mult_texl1 = glGetUniformLocation(*program, "texL1");
00357         _param_guided_mult_texl2 = glGetUniformLocation(*program, "texL2");
00358         _param_guided_mult_h = glGetUniformLocation(*program, "H");
00359         _param_guided_mult_f = glGetUniformLocation(*program, "F");
00360         _param_guided_mult_param = glGetUniformLocation(*program, "size");
00361 
00362         //row max
00363         out.seekp(ios::beg);
00364         out <<  "#define BLOCK_WIDTH 16.0\n"
00365                         "uniform sampler2DRect tex;     uniform vec3 param;\n"
00366                         "void main ()\n"
00367                         "{\n"
00368                         "       float index = gl_FragCoord.x + floor(gl_FragCoord.y) * BLOCK_WIDTH; \n"
00369                         "       vec2 bestv = vec2(-1.0); float imax = -1.0;\n"
00370                         "       for(float i = 0.0; i < param.x; i ++){\n "
00371                         "               float v = texture2DRect(tex, vec2(i + 0.5, index)).r; \n"
00372                         "               imax = v > bestv.r ? i : imax; \n "
00373                         "               bestv  = v > bestv.r? vec2(v, bestv.r) : max(bestv, vec2(v));\n "
00374                         "       }\n"
00375                         "       bestv = acos(min(bestv, 1.0));\n"
00376                         "       if(bestv.x >= param.y || bestv.x >= param.z * bestv.y) imax = -1.0;\n"
00377                         "       gl_FragColor = vec4(imax, bestv, index);\n"
00378                         "}"
00379                 <<  '\0';
00380         s_row_max = program= new ProgramGLSL(out.str().c_str()); 
00381         _param_rowmax_param = glGetUniformLocation(*program, "param");
00382 
00383         out.seekp(ios::beg);
00384         out <<  "#define BLOCK_WIDTH 16.0\n"
00385                         "uniform sampler2DRect tex; uniform vec3 param;\n"
00386                         "void main ()\n"
00387                         "{\n"
00388                         "       float index = gl_FragCoord.x + floor(gl_FragCoord.y) * BLOCK_WIDTH; \n"
00389                         "       vec2 bestv = vec2(-1.0); float imax = -1.0;\n"
00390                         "       for(float i = 0.0; i < param.x; i ++){\n "
00391                         "               float v = texture2DRect(tex, vec2(index, i + 0.5)).r; \n"
00392                         "               imax = (v > bestv.r)? i : imax; \n "
00393                         "               bestv  = v > bestv.r? vec2(v, bestv.r) : max(bestv, vec2(v));\n "
00394                         "       }\n"
00395                         "       bestv = acos(min(bestv, 1.0));\n"
00396                         "       if(bestv.x >= param.y || bestv.x >= param.z * bestv.y) imax = -1.0;\n"
00397                         "       gl_FragColor = vec4(imax, bestv, index);\n"
00398                         "}"
00399                 <<  '\0';
00400         s_col_max = program =new ProgramGLSL(out.str().c_str()); 
00401         _param_colmax_param = glGetUniformLocation(*program, "param");
00402 
00403 
00404 }
00405 
00406 int  SiftMatchGL::GetGuidedSiftMatch(int max_match, int match_buffer[][2], float H[3][3], float F[3][3],
00407                                                                          float distmax, float ratiomax, float hdistmax, float fdistmax, int mbm)
00408 {
00409 
00410         int dw = _num_sift[1];
00411         int dh = _num_sift[0]; 
00412         if(_initialized ==0) return 0;
00413         if(dw <= 0 || dh <=0) return 0;
00414         if(_have_loc[0] == 0 || _have_loc[1] == 0) return 0;
00415 
00416         FrameBufferObject fbo;
00417         glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
00418         _texDot.SetImageSize(dw, dh);
00419 
00420 
00421         //data
00422         _texDot.AttachToFBO(0);
00423         _texDot.FitTexViewPort();
00424         glActiveTexture(GL_TEXTURE0);
00425         _texDes[0].BindTex();
00426         glActiveTexture(GL_TEXTURE1);
00427         _texDes[1].BindTex();
00428         glActiveTexture(GL_TEXTURE2);
00429         _texLoc[0].BindTex();
00430         glActiveTexture(GL_TEXTURE3);
00431         _texLoc[1].BindTex();
00432 
00433         //multiply the descriptor matrices
00434         s_guided_mult->UseProgram();
00435 
00436 
00437         //set parameters glsl
00438         float dot_param[4] = {(float)_texDes[0].GetDrawHeight(), (float) _texDes[1].GetDrawHeight(), hdistmax, fdistmax};
00439         glUniform1i(_param_guided_mult_tex1, 0);
00440         glUniform1i(_param_guided_mult_tex2, 1);
00441         glUniform1i(_param_guided_mult_texl1, 2);
00442         glUniform1i(_param_guided_mult_texl2, 3);
00443         glUniformMatrix3fv(_param_guided_mult_h, 1, GL_TRUE, H[0]);
00444         glUniformMatrix3fv(_param_guided_mult_f, 1, GL_TRUE, F[0]);
00445         glUniform4fv(_param_guided_mult_param, 1, dot_param);
00446 
00447         _texDot.DrawQuad();
00448 
00449         GLTexImage::UnbindMultiTex(4);
00450 
00451         return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
00452 }
00453 
00454 int SiftMatchGL::GetBestMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm)
00455 {
00456 
00457         glActiveTexture(GL_TEXTURE0);
00458         _texDot.BindTex();
00459 
00460         //readback buffer
00461         sift_buffer.resize(_num_sift[0] + _num_sift[1] + 16);
00462         float * buffer1 = &sift_buffer[0], * buffer2 = &sift_buffer[_num_sift[0]];
00463 
00464         //row max
00465         _texMatch[0].AttachToFBO(0);
00466         _texMatch[0].SetImageSize(16, ( _num_sift[0] + 15) / 16);
00467         _texMatch[0].FitTexViewPort();
00468 
00470         s_row_max->UseProgram();
00471         glUniform3f(_param_rowmax_param, (float)_num_sift[1], distmax, ratiomax);
00472 
00473         _texMatch[0].DrawQuad();
00474         glReadPixels(0, 0, 16, (_num_sift[0] + 15)/16, GL_RED, GL_FLOAT, buffer1);
00475 
00476         //col max
00477         if(mbm)
00478         {
00479                 _texMatch[1].AttachToFBO(0);
00480                 _texMatch[1].SetImageSize(16, (_num_sift[1] + 15) / 16);
00481                 _texMatch[1].FitTexViewPort();
00482                 //set parameter glsl
00483                 s_col_max->UseProgram();
00484                 glUniform3f(_param_rowmax_param, (float)_num_sift[0], distmax, ratiomax);
00485                 _texMatch[1].DrawQuad();
00486                 glReadPixels(0, 0, 16, (_num_sift[1] + 15) / 16, GL_RED, GL_FLOAT, buffer2);
00487         }
00488 
00489 
00490         //unload
00491         glUseProgram(0);
00492 
00493         GLTexImage::UnbindMultiTex(2);
00494         GlobalUtil::CleanupOpenGL();
00495 
00496         //write back the matches
00497         int nmatch = 0, j ;
00498         for(int i = 0; i < _num_sift[0] && nmatch < max_match; ++i)
00499         {
00500                 j = int(buffer1[i]);
00501                 if( j>= 0 && (!mbm ||int(buffer2[j]) == i))
00502                 {
00503                         match_buffer[nmatch][0] = i;
00504                         match_buffer[nmatch][1] = j;
00505                         nmatch++;
00506                 }
00507         }
00508         return nmatch;
00509 }
00510 
00511 int  SiftMatchGL::GetSiftMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm)
00512 {
00513         int dw = _num_sift[1];
00514         int dh =  _num_sift[0]; 
00515         if(_initialized ==0) return 0;
00516         if(dw <= 0 || dh <=0) return 0;
00517 
00518         FrameBufferObject fbo;
00519         glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
00520         _texDot.SetImageSize(dw, dh);
00521 
00522         //data
00523         _texDot.AttachToFBO(0);
00524         _texDot.FitTexViewPort();
00525         glActiveTexture(GL_TEXTURE0);
00526         _texDes[0].BindTex();
00527         glActiveTexture(GL_TEXTURE1);
00528         _texDes[1].BindTex();
00529 
00531         //multiply the descriptor matrices
00532         s_multiply->UseProgram();
00533         //set parameters
00534         float heights[2] = {(float)_texDes[0].GetDrawHeight(), (float)_texDes[1].GetDrawHeight()};
00535 
00536         glUniform1i(_param_multiply_tex1, 0);
00537         glUniform1i(_param_multiply_tex2 , 1);
00538         glUniform2fv(_param_multiply_size, 1, heights);
00539 
00540         _texDot.DrawQuad();
00541 
00542         glActiveTexture(GL_TEXTURE1);
00543         glBindTexture(GlobalUtil::_texTarget, 0);
00544 
00545         return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
00546 }
00547 
00548 
00549 int SiftMatchGPU::_CreateContextGL()
00550 {
00551         //Create an OpenGL Context?
00552     if (__language >= SIFTMATCH_CUDA) {}
00553         else if(!GlobalUtil::CreateWindowEZ())
00554         {
00555 #if CUDA_SIFTGPU_ENABLED
00556                 __language = SIFTMATCH_CUDA;
00557 #else
00558                 return 0;
00559 #endif
00560         }
00561         return VerifyContextGL();
00562 }
00563 
00564 
00565 int SiftMatchGPU::_VerifyContextGL()
00566 {
00567         if(__matcher) return GlobalUtil::_GoodOpenGL;
00568         
00569 #ifdef CUDA_SIFTGPU_ENABLED
00570 
00571     if(__language >= SIFTMATCH_CUDA) {}
00572     else if(__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA){}
00573     else  GlobalUtil::InitGLParam(0); 
00574     if(GlobalUtil::_GoodOpenGL == 0) __language = SIFTMATCH_CUDA;
00575 
00576     if(((__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA) || __language >= SIFTMATCH_CUDA) 
00577         && SiftMatchCU::CheckCudaDevice (GlobalUtil::_DeviceIndex))
00578     {
00579                 __language = SIFTMATCH_CUDA;
00580                 __matcher = new SiftMatchCU(__max_sift);
00581         }else
00582 #else
00583     if((__language == SIFTMATCH_SAME_AS_SIFTGPU && GlobalUtil::_UseCUDA) || __language >= SIFTMATCH_CUDA) 
00584     {
00585             std::cerr   << "---------------------------------------------------------------------------\n"
00586                                     << "CUDA not supported in this binary! To enable it, please use SiftGPU_CUDA_Enable\n" 
00587                                     << "Project for VS2005+ or set siftgpu_enable_cuda to 1 in makefile\n"
00588                                     << "----------------------------------------------------------------------------\n";
00589     }
00590 #endif
00591         {
00592                 __language = SIFTMATCH_GLSL;
00593                 __matcher = new SiftMatchGL(__max_sift, 1);
00594         }
00595 
00596         if(GlobalUtil::_verbose)
00597         std::cout   << "[SiftMatchGPU]: " << (__language == SIFTMATCH_CUDA? "CUDA" : "GLSL") <<"\n\n";
00598 
00599         __matcher->InitSiftMatch();
00600         return GlobalUtil::_GoodOpenGL;
00601 }
00602 
00603 void* SiftMatchGPU::operator new (size_t  size){
00604   void * p = malloc(size);
00605   if (p == 0)  
00606   {
00607           const std::bad_alloc ba;
00608           throw ba; 
00609   }
00610   return p; 
00611 }
00612 
00613 
00614 SiftMatchGPU::SiftMatchGPU(int max_sift)
00615 {
00616         __max_sift = max(max_sift, 1024);
00617         __language = 0;
00618         __matcher = NULL;
00619 }
00620 
00621 void SiftMatchGPU::SetLanguage(int language)
00622 {
00623         if(__matcher) return;
00625 #ifdef CUDA_SIFTGPU_ENABLED
00626         if(language >= SIFTMATCH_CUDA) GlobalUtil::_DeviceIndex = language - SIFTMATCH_CUDA; 
00627 #endif
00628     __language = language > SIFTMATCH_CUDA ? SIFTMATCH_CUDA : language;
00629 }
00630 
00631 void SiftMatchGPU::SetDeviceParam(int argc, char**argv)
00632 {
00633     if(__matcher) return;
00634     GlobalUtil::SetDeviceParam(argc, argv);
00635 }
00636 
00637 void SiftMatchGPU::SetMaxSift(int max_sift)
00638 {
00639         if(__matcher)   __matcher->SetMaxSift(max(128, max_sift));
00640         else __max_sift = max(128, max_sift);
00641 }
00642 
00643 SiftMatchGPU::~SiftMatchGPU()
00644 {
00645         if(__matcher) delete __matcher;
00646 }
00647 
00648 void SiftMatchGPU::SetDescriptors(int index, int num, const unsigned char* descriptors, int id)
00649 {
00650         __matcher->SetDescriptors(index, num,  descriptors, id);
00651 }
00652 
00653 void SiftMatchGPU::SetDescriptors(int index, int num, const float* descriptors, int id)
00654 {
00655         __matcher->SetDescriptors(index, num, descriptors, id);
00656 }
00657 
00658 void SiftMatchGPU::SetFeautreLocation(int index, const float* locations, int gap)
00659 {
00660         __matcher->SetFeautreLocation(index, locations, gap);
00661 
00662 }
00663 int  SiftMatchGPU::GetGuidedSiftMatch(int max_match, int match_buffer[][2], float H[3][3], float F[3][3], 
00664                                 float distmax, float ratiomax, float hdistmax, float fdistmax, int mutual_best_match)
00665 {
00666         if(H == NULL && F == NULL)
00667         {
00668                 return __matcher->GetSiftMatch(max_match, match_buffer, distmax, ratiomax, mutual_best_match);
00669         }else
00670         {
00671                 float Z[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}, ti = (1.0e+20F);
00672 
00673                 return __matcher->GetGuidedSiftMatch(max_match, match_buffer, H? H : Z, F? F : Z,
00674                         distmax, ratiomax, H? hdistmax: ti,  F? fdistmax: ti, mutual_best_match);
00675         }
00676 }
00677 
00678 int  SiftMatchGPU::GetSiftMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mutual_best_match)
00679 {
00680         return __matcher->GetSiftMatch(max_match, match_buffer, distmax, ratiomax, mutual_best_match);
00681 }
00682 
00683 SiftMatchGPU* CreateNewSiftMatchGPU(int max_sift)
00684 {
00685         return new SiftMatchGPU(max_sift);
00686 }
00687 


siftgpu
Author(s): Changchang Wu (library), Bence Magyar (ROS wrapper)
autogenerated on Thu Jan 2 2014 11:38:01