-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliding_window.cpp~
More file actions
209 lines (176 loc) · 6.22 KB
/
sliding_window.cpp~
File metadata and controls
209 lines (176 loc) · 6.22 KB
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
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
using namespace cv;
void test_object_detection(Mat win_img,int OP_class,float prob)
{
/// Separate the image in 3 places ( B, G and R )
vector<Mat> bgr_planes;
cvtColor(img,img,CV_BGR2HSV);
// HSV frames
split( img, bgr_planes );
/// Establish the number of bins for each channel
int histSize = 25;
/// Set the ranges ( for B,G,R )
float range[] = { 0, 256 } ;
const float* histRange = { range };
bool uniform = true; bool accumulate = false;
Mat b_hist, g_hist, r_hist;
/// Compute the histograms:
calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
calcHist( &bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
// Draw the histograms for B, G and R
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histSize );
Mat histImage( hist_h, hist_w, CV_8UC3, Scalar( 0,0,0) );
/// Normalize the result to [ 0, histImage.rows ]
normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
//combine b,g,r hists in a single feature vector
int nrcombine=3;
vector<float> feature; //feature vector
feature.resize(histSize*nrcombine);
for (int i=0; i<feature.size(); i++){
if(i<histSize) feature[i] = b_hist.at<float>(i,0);
else if(i>=histSize && i<histSize*2) feature[i] = g_hist.at<float>(i-histSize,0);
else if(i>=histSize*2 && i<histSize*3) feature[i] = r_hist.at<float>(i-2*histSize,0);
}
/// Draw for each channel
for( int i = 1; i < histSize; i++ )
{
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(b_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(g_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 );
line( histImage, Point( bin_w*(i-1), hist_h - cvRound(r_hist.at<float>(i-1)) ) ,
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 );
}
/// Display
imshow("test img histogram", histImage );
waitKey(0);
// storing test data
ofstream testData;
testData.open("test_data");
testData<<1<<" ";
for (int n = 0; n < feature.size(); n++)
{
testData << (n+1) << ":";
testData << feature.at(n) << " ";
}
//testData << "-1:0";
testData << "\n";
testData.close();
// scaling test_data
int Tscale_argc = 4;
char **Tscale_argv; // -r training_data.range test_data > test_data.scale
Tscale_argv = new char* [Tscale_argc];
for (int i=0; i< Tscale_argc; i++) {
Tscale_argv[i] = new char [100];
}
sprintf(Tscale_argv[1], "%s", "-r");
sprintf(Tscale_argv[2], "%s", "training_data.range");
sprintf(Tscale_argv[3], "%s", "test_data");
char* TscaleOP;
TscaleOP = new char [100];
sprintf(TscaleOP, "%s", "test_data.scale");
vector<float> test_scaled = scale_main(Tscale_argc,Tscale_argv,TscaleOP);
svm_node* x;
x = new svm_node [feature.size()+1];
for (int n = 0; n < feature.size(); n++)
{
svm_node tmp;
tmp.index = n+1;
tmp.value = test_scaled.at(n);
x[n] = tmp;
// cout<< " n " <<n<<"test_scaled[n] " <<test_scaled.at(n) <<endl;
}
x[feature.size()].index = -1;
double *prob_estimates=(double *)malloc(svm_get_nr_class(model)*sizeof(double));
double predictedValue = svm_predict_probability(model, x ,prob_estimates);
// double predictedValue = svm_predict(model, x);
cout<< "Predicted Value[1=buoy 0=no_buoy]: " << predictedValue <<endl;
cout<<"Prob of being buoy "<<prob_estimates[1]<<"\n";
// sprintf(command,"./svm_scale -r training_data.range test_data > test_data.scale");
// system(command);
sprintf(command,"./svm_predict -b 1 test_data.scale training_data.model svmOutput");
system(command);
}
int main(int argc, char** argv)
{
Mat img=imread(argv[1]);
Rect window; // sliding window
// Input parameters
int min_width = 20;
int min_height = 20;
int max_width = 150;
int max_height = 150;
int scale_factor = 2;
float aspect_ratio = 1; //(height/width)
// Sliding Window parameters
int delta_x = 25;
int delta_y = 25;
Scalar color(255,0,0);
// Sliding window variables
int img_width = img.cols;
int img_height = img.rows;
int start_width, start_height; // starting window size
int scale_number;
if(aspect_ratio >= 1)
{
start_width = min(min_width,min_height);
start_height = aspect_ratio*start_width;
}
else
{
start_height = min(min_width,min_height);
start_width = start_height/aspect_ratio;
}
scale_number = ceil(log(max(max_width,max_height)/max(min_width,min_height))/log(scale_factor));
// Starting window
window.x = 0;
window.y = 0;
window.width = start_width;
window.height = start_height;
cout<<"scale no "<<scale_number<<"\n";
for (int i=0; i< scale_number; i++)
{
color= Scalar(i*255/scale_number,0,0);
int sliding_num_x = ceil((img_width-window.width)/delta_x);
int sliding_num_y = ceil((img_height-window.height)/delta_y);
for(int j=0; j<sliding_num_y; j++)
{
for(int k=0; k<sliding_num_x; k++)
{
//rectangle(img,window,color);
Mat win_img = img(window).clone();
float prob; int OP_class;
test_object_detection(win_img, OP_class, prob);
if(OP_class==1)
{
cout<<"buoy: prob "<<prob<<"\n";
}
else
{
cout<<"not buoy: prob "<<prob<<"\n";
}
imshow ("window",win_img);
waitKey(0);
window.x = window.x + delta_x;
}
window.x = 0;
window.y = window.y + delta_y;
}
window.y = 0;
window.width = window.width*scale_factor;
window.height = window.height*scale_factor;
}
return 0;
}