-
Notifications
You must be signed in to change notification settings - Fork 60
/
LBP.hpp
194 lines (164 loc) · 5.4 KB
/
LBP.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* LBP.hpp
* Implements the local binary pattern (LBP) texture descriptors
*
* Created on: Jan 25, 2013
* Author: Navid Nourani-Vatani
* Email: Navid.Nourani-Vatani@sydney.edu.au
*
* The methods implemented here are inspired by the Matlab code available
* from web site of the University of Oulu:
* http://www.cse.oulu.fi/CMV/Downloads/LBPMatlab
* You should cite the appropriate publications when using this code.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// cmath header must be first in MSVC otherwise M_PI is undefined
#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#endif/*_MSC_VER*/
#include <cmath>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <climits>
#include <complex>
#include <string>
#include <fftw3.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "LBPGPU.cuh"
#ifndef _LBP_H_
#define _LBP_H_
#ifndef NULL
#define NULL 0
#endif
// SWIG doesn't understand 'using' directives properly
// so disable them if doing the SWIG pass
#ifndef SWIG
using namespace std;
using namespace cv;
#endif
// enable/disable use of mixed OpenCV API in the code below.
#define DEMO_MIXED_API_USE 0
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
namespace lbp {
enum MappingType {
LBP_MAPPING_NONE = 0,
LBP_MAPPING_U2,
LBP_MAPPING_RI,
LBP_MAPPING_RIU2,
LBP_MAPPING_HF
};
static const string MappingTypeStr[] = {
"none", "u2", "ri", "riu2", "hf" };
class LBP {
public:
LBP();
LBP( unsigned int samples, MappingType type );
~LBP( void );
/**
* Mapping methods
*/
LBP & generateMapping();
LBP & generateMapping( unsigned int samples, MappingType type );
bool saveMapping( string fileName );
bool loadMapping( string fileName );
static MappingType strToType( string s ) {
if( s.compare( "u2" ) == 0 )
return LBP_MAPPING_U2;
else if( s.compare("ri") == 0 )
return LBP_MAPPING_RI;
else if ( s.compare("riu2") == 0 )
return LBP_MAPPING_RIU2;
else if( s.compare("hf") == 0 )
return LBP_MAPPING_HF;
else
return LBP_MAPPING_NONE;
}
MappingType getMapping(void) const {
return type;
}
/**
* Descriptor methods
*/
void calcGPU( Mat img, double radius = 1. ) {
unsigned char * src;
unsigned char * dst;
// LBPMapping mapping;
// calcLBPGPU( src, dst, 10, 10, &mapping );
}
LBP & calcLBP( Mat img, double radius = 1., bool borderCopy=false );
Mat getLBPImage( void ) const {
return lbpImage;
}
bool saveLBPImage( string fileName );
/**
* Histogram methods
*/
LBP & calcHist(void);
LBP & calcHist( Mat mask );
vector<double> getHist( bool norm = true );
vector<double> constructHF( vector<double> h );
/**
* Other methods
*/
std::string toString( bool verbose=false ) const;
private:
// Mapping variables
MappingType type;
vector<int> table;
unsigned int samples;
unsigned int num;
// Fourier Histogram variables
vector< vector<int> > orbits;
double *fftIn;
complex<double> *fftOut;
fftw_plan fftPlan;
unsigned int fftN;
unsigned int fftHermN;
vector<double> hf;
// Histogram
vector<double> h;
// Descriptor variables
Mat lbpImage;
MatND hist;
// Private bit operation methods
int NumberOfSetBits( int i ) {
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}
unsigned int rotateLeft( unsigned int i, unsigned int samples ) {
unsigned int bg = ((i & (1 << (samples - 1))) >> (samples - 1)); // bitget(r,samples)
unsigned int bs = (i << 1) & ((int) pow( 2., (int) samples ) - 1); // bitshift(r, 1, samples)
unsigned int j = (bs + bg) & ((int) pow( 2., (int) samples ) - 1); // bitset( bs, 1, bg )
return j;
}
int trailingZeroInd( unsigned int v ) { // find the number of trailing zeros in v
static const int Mod37BitPosition[] = // map a bit value mod 37 to its position
{ 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0, 25,
22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18 };
return Mod37BitPosition[(-v & v) % 37];
}
void initHF(void);
LBP & calcHist( Mat * img, Mat * mask=NULL );
};
}
;
#endif