-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrediction.cpp
More file actions
289 lines (249 loc) · 7.79 KB
/
Copy pathPrediction.cpp
File metadata and controls
289 lines (249 loc) · 7.79 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "freenect-playback-wrapper.h"
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <sys/stat.h>
#include "opencv2/opencv.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/core/core.hpp"
#include <opencv2/core.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <direct.h>
#include <io.h>
#include <string>
#include <vector>
#define CV_AA 16
using namespace cv::ml;
using namespace cv;
using namespace std;
/////**************Input parameter**************///////
string Videopath = "B:/Set2";
string Modelpath = "B:/Model.xml";
string savefolder = "B:/predictimage/";
//**********************************************///
HOGDescriptor hog(
Size(640, 480), //winSize
Size(8, 8), //blocksize
Size(8, 8), //blockStride,
Size(8, 8), //cellSize,
9, //nbins,
1, //derivAper,
-1, //winSigma,
HOGDescriptor::L2Hys, //histogramNormType,
0.2, //L2HysThresh,
0,//gammal correction,
64,//nlevels=64
1);
Ptr<SVM> svm = SVM::create();
std::vector<std::vector<float> > imgHOG;
vector<float> descriptors;
Mat newdepth;
Mat initDepth;
Mat initialframe;
Mat initthreshold;
Mat currentThreshold;
bool newfolder = false;
int classcounter = 0;
std::string path = "B:/ResultsData/";
//Create string array of labels for each category
string labels[] = { "Baby","Dog","Dinosaur","Coffee_Tin","Mug",
"Car","Camera","Keyboard","Koala","Blackberry",
"Diet_Coke_Bottle","Duck","Dragon","Android" };
// declare functions
void imgtext(Mat & frame, int response);
Mat translateImg(Mat &img, int offsetx, int offsety);
void imgpreprocess(Mat & Thresimg, Mat & imgcrop);
bool checkDiff(const cv::Mat mat1, const cv::Mat mat2)
{
// treat two empty mat as identical as well
if (mat1.empty() && mat2.empty()) {
return true;
}
// if dimensionality of two mat is not identical, these two mat is not identical
if (mat1.cols != mat2.cols || mat1.rows != mat2.rows || mat1.dims != mat2.dims) {
return false;
}
cv::Mat diff;
cv::compare(mat1, mat2, diff, cv::CMP_NE);
int nz = cv::countNonZero(diff);
return nz == 0;
}
//Put the response text onto the screen next to the object
void imgtext(Mat & frame, int response)
{
switch (response) {
case 0:
{
putText(frame, "Baby", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 1:
{
putText(frame, "Dog", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 2:
{
putText(frame, "Dinosaur", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 3:
{
putText(frame, "Coffee_Tin", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 4:
{
putText(frame, "Mug", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 5:
{
putText(frame, "Car", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 6:
{
putText(frame, "Camera", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 7:
{
putText(frame, "Keyboard", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 8:
{
putText(frame, "Koala", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 9:
{
putText(frame, "Blackberry", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 10:
{
putText(frame, "Coke_Bottle", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 11:
{
putText(frame, "Duck", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 12:
{
putText(frame, "Dragon", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
case 13:
{
putText(frame, "Android", Point2f(100, 200), FONT_HERSHEY_COMPLEX_SMALL, 2, Scalar(151, 220, 100), 1, CV_AA);
break;
}
default: printf(" ");
}
}
void imgpreprocess(Mat & Thresimg, Mat & imgtosave)
{
cv::Mat imgcrop;
//normalize
normalize(Thresimg, Thresimg, 0, 255, NORM_MINMAX, -1, Mat());
//Histogram Equalization
equalizeHist(Thresimg, Thresimg);
//Translate image into a 1 dimensional matrix
translateImg(Thresimg, -35, 30);
// crop image and resize
Rect roi(150, 70, 350, 250);
resize(Thresimg(roi), imgtosave, Size(640, 480));
}
Mat translateImg(Mat &img, int offsetx, int offsety) {
//Translate image
Mat trans_mat = (Mat_<double>(2, 3) << 1, 0, offsetx, 0, 1, offsety);
//Apply affine transformation to the image
warpAffine(img, img, trans_mat, img.size());
return img;
}
int main(int argc, char * argv[])
{
FreenectPlaybackWrapper wrap(Videopath);
cv::Mat currentRGB;
cv::Mat currentDepth;
cv::Mat currentThreshold;
cv::Mat currentContour;
cv::Mat Results(cv::Size(640, 640), CV_8UC3);
// Create the RGB and Depth Windows
cv::namedWindow("Results", WINDOW_NORMAL);
char key = '0';
int framecounter = 0;
uint8_t status = 255;
// svm loading
svm = SVM::load(Modelpath);
while (key != 27 && status != 0)
{
status = wrap.GetNextFrame();
//Grab the RGB from the wrap
if (status & State::UPDATED_RGB)
currentRGB = wrap.RGB;
if (status & State::UPDATED_DEPTH)
{
//Grab depth from the wrap and preprocess
currentDepth = wrap.Depth;
threshold(currentDepth, currentThreshold, 80, 255, THRESH_BINARY);
currentThreshold.copyTo(newdepth);
}
if (framecounter == 0) {
//Grab the initial frame by checking if the framecounter is at the beginning
initialframe = wrap.Depth;
threshold(initialframe, initthreshold, 80, 255, THRESH_BINARY);
initthreshold.copyTo(initDepth);
}
//Increment frame counter
framecounter++;
//Create labels
String currDepth = ("Image" + std::to_string(framecounter) + ".jpg");
//Create new directories for each label in the string array of labels
for (int i = 0; i < 13; i++) {
string label = labels[i];
string newdir = path + label + "/";
mkdir(newdir.c_str());
}
// If there is a difference in dpeth then
if (!checkDiff(newdepth, initDepth)) {
cv::Mat imgtosave;
//Preprocess the image
imgpreprocess(newdepth, imgtosave);
//Compute the descriptors
hog.compute(imgtosave, descriptors);
//Create a response prediction
int response = svm->predict(descriptors);
//Put the response text into the image
imgtext(currentRGB, response);
//Write the file to the correct directory
imwrite(path + "/" + labels[response] + "/" + currDepth, imgtosave);
}
// Show the images in one windows
cv::Mat RGB_mat;
cv::Mat Depth_mat;
cv::Mat Threshold_mat;
cv::Mat Contour_mat;
cv::Mat Gray_mat;
currentRGB.copyTo(Results(cv::Rect(0, 0, 640, 480)));
resize(currentDepth, Depth_mat, Size(currentDepth.cols / 3, currentDepth.rows / 3));
cvtColor(Depth_mat, Depth_mat, COLOR_GRAY2BGR);
Depth_mat.copyTo(Results(cv::Rect(0, 480, Depth_mat.cols, Depth_mat.rows)));
resize(currentThreshold, Threshold_mat, Size(currentThreshold.cols / 3, currentThreshold.rows / 3));
cvtColor(Threshold_mat, Threshold_mat, COLOR_GRAY2BGR);
Threshold_mat.copyTo(Results(cv::Rect(currentDepth.cols / 3, 480, currentThreshold.cols / 3, currentThreshold.rows / 3)));
cv::imshow("Results", Results);
// Check for keyboard input
key = cv::waitKey(10);
}
return 0;
}