-
Notifications
You must be signed in to change notification settings - Fork 60
/
LBPTest.cpp
215 lines (183 loc) · 5.84 KB
/
LBPTest.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* LBPTest.cpp
* Example routines for using the LBP class.
*
* Created on: Jan 25, 2013
* Author: Navid Nourani-Vatani
* Email: Navid.Nourani-Vatani@sydney.edu.au
*
* 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/>.
*/
#include <iostream>
#include <ctime>
#include "LBP.hpp"
#include "LBPGPU.cuh"
using namespace lbp;
/**
* Load and image and calculate the LBP-HF descriptor for the whole image
*/
void example_1( void ) {
cout << endl << "Example 1..." << endl;
// Read an (RGB) image and convert to monochrome
cv::Mat img = imread( "../test_image_1.pgm", 0 );
// convert to double precision
img.convertTo( img, CV_64F );
cout << "image w/h = " << img.rows << "/" << img.cols << " (" << img.rows * img.cols << ")"
<< endl;
// Create an LBP instance of type HF using 8 support points
LBP lbp( 8, LBP_MAPPING_NONE );
// Calculate the descriptor
lbp.calcLBP( img );
// Calculate Fourier tranformed histogram
vector<double> hist = lbp.calcHist().getHist( false );
// Print out the histogram values
double sum = 0;
cout << "hist = [";
for( int i = 0; i < hist.size(); i++ ) {
cout << hist[i] << ", ";
sum += hist[i];
}
cout << "]; " << endl;
cout << "hist sum=" << sum << endl;
}
/**
* Load an image, calculate LBP riu2 and then calculate the histogram on sub-regions of the image
*/
void example_2( void ) {
cout << endl << "Example 2..." << endl;
clock_t startTime, endTime;
// Read an (RGB) image and convert to monochrome
cv::Mat img = imread( "../test_image_1.bmp", 0 );
// convert to double precision
img.convertTo( img, CV_64F );
int w = img.cols, h = img.rows;
// Create an LBP instance of type rotation invariant uniform 2 using 8 support points
LBP lbp( 8, LBP_MAPPING_HF );
// Calculate the descriptor image and get it
lbp.calcLBP( img, 1, true );
// Create a mask same size as the image
Mat mask( h, w, CV_8UC1 );
// Get the histogram for sub-images
for( int j = 0; j < 2; j++ ) {
for( int i = 0; i < 2; i++ ) {
// Reset mask. Will actually not allocate the data as it is
// same size as before.
mask = Mat::zeros( h, w, CV_8UC1 );
// Get a sub-image (ROI) the size of 1/4 of the whole image
int x = w / 2 * i;
int y = h / 2 * j;
int wH = w / 2 - 2;
int hH = h / 2 - 2;
Mat roi( mask, Range( y, y + hH ), Range( x, x + wH ) );
roi = Scalar( 255 );
// Calculate histogram for the ROI
startTime = clock();
vector<double> hist = lbp.calcHist( mask ).getHist();
// Print out the histogram values
cout << "hist(" << j << "," << i << ") = [";
for( int i = 0; i < hist.size(); i++ ) {
cout << hist[i] << ", ";
}
cout << "]; " << endl;
}
}
}
/**
* Calculate a mapping, save it and load it. This is especially useful for
* larger mappings (24 pts) which can takes many seconds to calculate.
*/
void example_3( void ) {
cout << endl << "Example 3..." << endl;
clock_t startTime, endTime;
LBP lbp( 16, LBP_MAPPING_U2 );
cout << lbp.toString() << endl;
startTime = clock();
lbp.saveMapping( "mapping.txt" );
endTime = clock();
cout << "save took " << double( endTime - startTime ) / double( CLOCKS_PER_SEC ) << "s" << endl;
LBP lbp2;
startTime = clock();
lbp2.loadMapping( "mapping.txt" );
endTime = clock();
cout << lbp2.toString() << endl;
cout << "load took " << double( endTime - startTime ) / double( CLOCKS_PER_SEC ) << "s" << endl;
}
void example_4( void ) {
cout << endl << "Example 4..." << endl;
#if 0
unsigned char pixels[] = {78, 87, 84, 81, 92, 98,
75, 86, 82, 74, 82, 90,
77, 87, 85, 76, 74, 80,
91, 98, 91, 81, 77, 79,
90, 95, 85, 80, 84, 88,
91, 91, 83, 79, 86, 90 };
int w = 6, h = 6;
Mat img( h, w, CV_8U, pixels );
#else
Mat img = imread( "../test_image_1.png", 0 );
#endif
img.convertTo( img, CV_64F );
// for( int j = 0; j < img.rows; ++j ) {
// for( int i = 0; i < img.cols; ++i ) {
// printf( "%3d ", (int) img.at<double>( j, i ) );
// }
// printf( "\n" );
// }
clock_t startTime, endTime;
// Create an LBP instance of type HF using 8 support points
LBP lbp( 8, LBP_MAPPING_NONE );
// Calculate the descriptor
startTime = clock();
lbp.calcGPU( img );
endTime = clock();
Mat lbpImg = lbp.getLBPImage();
cout << "Example took " << double( endTime - startTime ) / double( CLOCKS_PER_SEC ) << "s"
<< endl;
#if 0
for( int j = 0; j < lbpImg.rows; ++j ) {
for( int i = 0; i < lbpImg.cols; ++i ) {
printf( "%3d ", (int) lbpImg.at<unsigned char>( j, i ) );
}
printf( "\n" );
}
#elseif 0
namedWindow("res");
imshow("res", lbpImg);
waitKey(0);
#endif
}
int main( int argc, char ** argv ) {
clock_t startTime, endTime;
startTime = clock();
example_1();
endTime = clock();
cout << "Example 1 took " << double( endTime - startTime ) / double( CLOCKS_PER_SEC ) << "s"
<< endl << endl;
startTime = clock();
example_2();
endTime = clock();
cout << "Example 2 took " << double( endTime - startTime ) / double( CLOCKS_PER_SEC ) << "s"
<< endl << endl;
startTime = clock();
example_3();
endTime = clock();
cout << "Example 3 took " << double( endTime - startTime ) / double( CLOCKS_PER_SEC ) << "s"
<< endl << endl;
startTime = clock();
example_4();
endTime = clock();
cout << "Example 4 took " << double( endTime - startTime ) / double( CLOCKS_PER_SEC ) << "s"
<< endl;
return 0;
}