00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00022
00023 #if defined(CUDA_SIFTGPU_ENABLED)
00024
00025 #include "GL/glew.h"
00026 #include <iostream>
00027 #include <vector>
00028 #include <algorithm>
00029 #include <stdlib.h>
00030 #include <math.h>
00031 using namespace std;
00032
00033
00034 #include "GlobalUtil.h"
00035 #include "CuTexImage.h"
00036 #include "SiftGPU.h"
00037 #include "ProgramCU.h"
00038 #include "SiftMatchCU.h"
00039
00040
00041 SiftMatchCU::SiftMatchCU(int max_sift):SiftMatchGPU()
00042 {
00043 _num_sift[0] = _num_sift[1] = 0;
00044 _id_sift[0] = _id_sift[1] = 0;
00045 _have_loc[0] = _have_loc[1] = 0;
00046 _max_sift = max_sift <=0 ? 4096 : ((max_sift + 31)/ 32 * 32) ;
00047 _initialized = 0;
00048 }
00049
00050 void SiftMatchCU::SetMaxSift(int max_sift)
00051 {
00052 max_sift = ((max_sift + 31)/32)*32;
00053 if(max_sift > GlobalUtil::_texMaxDimGL) max_sift = GlobalUtil::_texMaxDimGL;
00054 _max_sift = max_sift;
00055 }
00056
00057
00058 int SiftMatchCU::CheckCudaDevice(int device)
00059 {
00060 return ProgramCU::CheckCudaDevice(device);
00061 }
00062
00063 void SiftMatchCU::InitSiftMatch()
00064 {
00065 if(_initialized) return;
00066 GlobalUtil::_GoodOpenGL = max(GlobalUtil::_GoodOpenGL, 1);
00067 _initialized = 1;
00068 }
00069
00070
00071 void SiftMatchCU::SetDescriptors(int index, int num, const unsigned char* descriptors, int id)
00072 {
00073 if(_initialized == 0) return;
00074 if (index > 1) index = 1;
00075 if (index < 0) index = 0;
00076 _have_loc[index] = 0;
00077
00078 if(id !=-1 && id == _id_sift[index]) return ;
00079 _id_sift[index] = id;
00080 if(num > _max_sift) num = _max_sift;
00081 _num_sift[index] = num;
00082 _texDes[index].InitTexture(8 * num, 1, 4);
00083 _texDes[index].CopyFromHost((void*)descriptors);
00084 }
00085
00086
00087 void SiftMatchCU::SetDescriptors(int index, int num, const float* descriptors, int id)
00088 {
00089 if(_initialized == 0) return;
00090 if (index > 1) index = 1;
00091 if (index < 0) index = 0;
00092 if(num > _max_sift) num = _max_sift;
00093
00094 sift_buffer.resize(num * 128 /4);
00095 unsigned char * pub = (unsigned char*) &sift_buffer[0];
00096 for(int i = 0; i < 128 * num; ++i)
00097 {
00098 pub[i] = int(512 * descriptors[i] + 0.5);
00099 }
00100 SetDescriptors(index, num, pub, id);
00101 }
00102
00103
00104 void SiftMatchCU::SetFeautreLocation(int index, const float* locations, int gap)
00105 {
00106 if(_num_sift[index] <=0) return;
00107 _texLoc[index].InitTexture(_num_sift[index], 1, 2);
00108 if(gap == 0)
00109 {
00110 _texLoc[index].CopyFromHost(locations);
00111 }else
00112 {
00113 sift_buffer.resize(_num_sift[index] * 2);
00114 float* pbuf = (float*) (&sift_buffer[0]);
00115 for(int i = 0; i < _num_sift[index]; ++i)
00116 {
00117 pbuf[i*2] = *locations++;
00118 pbuf[i*2+1]= *locations ++;
00119 locations += gap;
00120 }
00121 _texLoc[index].CopyFromHost(pbuf);
00122 }
00123 _have_loc[index] = 1;
00124 }
00125
00126 int SiftMatchCU::GetGuidedSiftMatch(int max_match, int match_buffer[][2], float H[3][3], float F[3][3],
00127 float distmax, float ratiomax, float hdistmax, float fdistmax, int mbm)
00128 {
00129
00130 if(_initialized ==0) return 0;
00131 if(_num_sift[0] <= 0 || _num_sift[1] <=0) return 0;
00132 if(_have_loc[0] == 0 || _have_loc[1] == 0) return 0;
00133 ProgramCU::MultiplyDescriptorG(_texDes, _texDes+1, _texLoc, _texLoc + 1,
00134 &_texDot, (mbm? &_texCRT: NULL), H, hdistmax, F, fdistmax);
00135 return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
00136 }
00137
00138
00139 int SiftMatchCU::GetSiftMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm)
00140 {
00141 if(_initialized ==0) return 0;
00142 if(_num_sift[0] <= 0 || _num_sift[1] <=0) return 0;
00143 ProgramCU::MultiplyDescriptor(_texDes, _texDes + 1, &_texDot, (mbm? &_texCRT: NULL));
00144 return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
00145 }
00146
00147
00148 int SiftMatchCU::GetBestMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm)
00149 {
00150 sift_buffer.resize(_num_sift[0] + _num_sift[1]);
00151 int * buffer1 = (int*) &sift_buffer[0], * buffer2 = (int*) &sift_buffer[_num_sift[0]];
00152 _texMatch[0].InitTexture(_num_sift[0], 1);
00153 ProgramCU::GetRowMatch(&_texDot, _texMatch, distmax, ratiomax);
00154 _texMatch[0].CopyToHost(buffer1);
00155 if(mbm)
00156 {
00157 _texMatch[1].InitTexture(_num_sift[1], 1);
00158 ProgramCU::GetColMatch(&_texCRT, _texMatch + 1, distmax, ratiomax);
00159 _texMatch[1].CopyToHost(buffer2);
00160 }
00161 int nmatch = 0, j ;
00162 for(int i = 0; i < _num_sift[0] && nmatch < max_match; ++i)
00163 {
00164 j = int(buffer1[i]);
00165 if( j>= 0 && (!mbm ||int(buffer2[j]) == i))
00166 {
00167 match_buffer[nmatch][0] = i;
00168 match_buffer[nmatch][1] = j;
00169 nmatch++;
00170 }
00171 }
00172 return nmatch;
00173 }
00174
00175 #endif
00176