root / rgbdslam / external / siftgpu / src / SiftGPU / SiftMatchCU.cpp @ 9240aaa3
History | View | Annotate | Download (4.97 KB)
1 | 9240aaa3 | Alex | ////////////////////////////////////////////////////////////////////////////
|
---|---|---|---|
2 | // File: SiftMatchCU.cpp
|
||
3 | // Author: Changchang Wu
|
||
4 | // Description : implementation of the SiftMatchCU class.
|
||
5 | // CUDA-based implementation of SiftMatch
|
||
6 | //
|
||
7 | // Copyright (c) 2007 University of North Carolina at Chapel Hill
|
||
8 | // All Rights Reserved
|
||
9 | //
|
||
10 | // Permission to use, copy, modify and distribute this software and its
|
||
11 | // documentation for educational, research and non-profit purposes, without
|
||
12 | // fee, and without a written agreement is hereby granted, provided that the
|
||
13 | // above copyright notice and the following paragraph appear in all copies.
|
||
14 | //
|
||
15 | // The University of North Carolina at Chapel Hill make no representations
|
||
16 | // about the suitability of this software for any purpose. It is provided
|
||
17 | // 'as is' without express or implied warranty.
|
||
18 | //
|
||
19 | // Please send BUG REPORTS to ccwu@cs.unc.edu
|
||
20 | //
|
||
21 | ////////////////////////////////////////////////////////////////////////////
|
||
22 | |||
23 | #if defined(CUDA_SIFTGPU_ENABLED)
|
||
24 | |||
25 | #include "GL/glew.h" |
||
26 | #include <iostream> |
||
27 | #include <vector> |
||
28 | #include <algorithm> |
||
29 | #include <stdlib.h> |
||
30 | #include <math.h> |
||
31 | using namespace std; |
||
32 | |||
33 | |||
34 | #include "GlobalUtil.h" |
||
35 | #include "CuTexImage.h" |
||
36 | #include "SiftGPU.h" |
||
37 | #include "ProgramCU.h" |
||
38 | #include "SiftMatchCU.h" |
||
39 | |||
40 | |||
41 | SiftMatchCU::SiftMatchCU(int max_sift):SiftMatchGPU()
|
||
42 | { |
||
43 | _num_sift[0] = _num_sift[1] = 0; |
||
44 | _id_sift[0] = _id_sift[1] = 0; |
||
45 | _have_loc[0] = _have_loc[1] = 0; |
||
46 | _max_sift = max_sift <=0 ? 4096 : ((max_sift + 31)/ 32 * 32) ; |
||
47 | _initialized = 0;
|
||
48 | } |
||
49 | |||
50 | void SiftMatchCU::SetMaxSift(int max_sift) |
||
51 | { |
||
52 | max_sift = ((max_sift + 31)/32)*32; |
||
53 | if(max_sift > GlobalUtil::_texMaxDimGL) max_sift = GlobalUtil::_texMaxDimGL;
|
||
54 | _max_sift = max_sift; |
||
55 | } |
||
56 | |||
57 | |||
58 | int SiftMatchCU::CheckCudaDevice(int device) |
||
59 | { |
||
60 | return ProgramCU::CheckCudaDevice(device);
|
||
61 | } |
||
62 | |||
63 | void SiftMatchCU::InitSiftMatch()
|
||
64 | { |
||
65 | if(_initialized) return; |
||
66 | GlobalUtil::_GoodOpenGL = max(GlobalUtil::_GoodOpenGL, 1);
|
||
67 | _initialized = 1;
|
||
68 | } |
||
69 | |||
70 | |||
71 | void SiftMatchCU::SetDescriptors(int index, int num, const unsigned char* descriptors, int id) |
||
72 | { |
||
73 | if(_initialized == 0) return; |
||
74 | if (index > 1) index = 1; |
||
75 | if (index < 0) index = 0; |
||
76 | _have_loc[index] = 0;
|
||
77 | //the same feature is already set
|
||
78 | if(id !=-1 && id == _id_sift[index]) return ; |
||
79 | _id_sift[index] = id; |
||
80 | if(num > _max_sift) num = _max_sift;
|
||
81 | _num_sift[index] = num; |
||
82 | _texDes[index].InitTexture(8 * num, 1, 4); |
||
83 | _texDes[index].CopyFromHost((void*)descriptors);
|
||
84 | } |
||
85 | |||
86 | |||
87 | void SiftMatchCU::SetDescriptors(int index, int num, const float* descriptors, int id) |
||
88 | { |
||
89 | if(_initialized == 0) return; |
||
90 | if (index > 1) index = 1; |
||
91 | if (index < 0) index = 0; |
||
92 | if(num > _max_sift) num = _max_sift;
|
||
93 | |||
94 | sift_buffer.resize(num * 128 /4); |
||
95 | unsigned char * pub = (unsigned char*) &sift_buffer[0]; |
||
96 | for(int i = 0; i < 128 * num; ++i) |
||
97 | { |
||
98 | pub[i] = int(512 * descriptors[i] + 0.5); |
||
99 | } |
||
100 | SetDescriptors(index, num, pub, id); |
||
101 | } |
||
102 | |||
103 | |||
104 | void SiftMatchCU::SetFeautreLocation(int index, const float* locations, int gap) |
||
105 | { |
||
106 | if(_num_sift[index] <=0) return; |
||
107 | _texLoc[index].InitTexture(_num_sift[index], 1, 2); |
||
108 | if(gap == 0) |
||
109 | { |
||
110 | _texLoc[index].CopyFromHost(locations); |
||
111 | }else
|
||
112 | { |
||
113 | sift_buffer.resize(_num_sift[index] * 2);
|
||
114 | float* pbuf = (float*) (&sift_buffer[0]); |
||
115 | for(int i = 0; i < _num_sift[index]; ++i) |
||
116 | { |
||
117 | pbuf[i*2] = *locations++;
|
||
118 | pbuf[i*2+1]= *locations ++; |
||
119 | locations += gap; |
||
120 | } |
||
121 | _texLoc[index].CopyFromHost(pbuf); |
||
122 | } |
||
123 | _have_loc[index] = 1;
|
||
124 | } |
||
125 | |||
126 | int SiftMatchCU::GetGuidedSiftMatch(int max_match, int match_buffer[][2], float H[3][3], float F[3][3], |
||
127 | float distmax, float ratiomax, float hdistmax, float fdistmax, int mbm) |
||
128 | { |
||
129 | |||
130 | if(_initialized ==0) return 0; |
||
131 | if(_num_sift[0] <= 0 || _num_sift[1] <=0) return 0; |
||
132 | if(_have_loc[0] == 0 || _have_loc[1] == 0) return 0; |
||
133 | ProgramCU::MultiplyDescriptorG(_texDes, _texDes+1, _texLoc, _texLoc + 1, |
||
134 | &_texDot, (mbm? &_texCRT: NULL), H, hdistmax, F, fdistmax);
|
||
135 | return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
|
||
136 | } |
||
137 | |||
138 | |||
139 | int SiftMatchCU::GetSiftMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm) |
||
140 | { |
||
141 | if(_initialized ==0) return 0; |
||
142 | if(_num_sift[0] <= 0 || _num_sift[1] <=0) return 0; |
||
143 | ProgramCU::MultiplyDescriptor(_texDes, _texDes + 1, &_texDot, (mbm? &_texCRT: NULL)); |
||
144 | return GetBestMatch(max_match, match_buffer, distmax, ratiomax, mbm);
|
||
145 | } |
||
146 | |||
147 | |||
148 | int SiftMatchCU::GetBestMatch(int max_match, int match_buffer[][2], float distmax, float ratiomax, int mbm) |
||
149 | { |
||
150 | sift_buffer.resize(_num_sift[0] + _num_sift[1]); |
||
151 | int * buffer1 = (int*) &sift_buffer[0], * buffer2 = (int*) &sift_buffer[_num_sift[0]]; |
||
152 | _texMatch[0].InitTexture(_num_sift[0], 1); |
||
153 | ProgramCU::GetRowMatch(&_texDot, _texMatch, distmax, ratiomax); |
||
154 | _texMatch[0].CopyToHost(buffer1);
|
||
155 | if(mbm)
|
||
156 | { |
||
157 | _texMatch[1].InitTexture(_num_sift[1], 1); |
||
158 | ProgramCU::GetColMatch(&_texCRT, _texMatch + 1, distmax, ratiomax);
|
||
159 | _texMatch[1].CopyToHost(buffer2);
|
||
160 | } |
||
161 | int nmatch = 0, j ; |
||
162 | for(int i = 0; i < _num_sift[0] && nmatch < max_match; ++i) |
||
163 | { |
||
164 | j = int(buffer1[i]);
|
||
165 | if( j>= 0 && (!mbm ||int(buffer2[j]) == i)) |
||
166 | { |
||
167 | match_buffer[nmatch][0] = i;
|
||
168 | match_buffer[nmatch][1] = j;
|
||
169 | nmatch++; |
||
170 | } |
||
171 | } |
||
172 | return nmatch;
|
||
173 | } |
||
174 | |||
175 | #endif
|