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


rgbd_registration
Author(s): Ross Kidson
autogenerated on Thu May 23 2013 15:36:53