-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencvexample.cpp
More file actions
225 lines (160 loc) · 5.36 KB
/
Copy pathopencvexample.cpp
File metadata and controls
225 lines (160 loc) · 5.36 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
#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 <io.h>
#include <string>
#include <direct.h>
#include "opencv2/features2d/features2d.hpp"
using namespace cv::ml;
using namespace cv;
using namespace std;
using namespace dnn;
FreenectPlaybackWrapper wrap("B:/Set1");
Mat newdepth;
Mat initDepth;
Mat Init_gray;
Mat rgbarr, backgrnd, detected_edges;
Mat initialframe;
Mat currentRGB;
Mat currentDepth;
Mat origdiff;
Mat newdiff;
Mat imggray;
std::string path;
Mat initthreshold;
Mat currentThreshold;
int lowThreshold = 100;
int classcounter = 0;
const int max_lowThreshold = 255;
const int finalratio = 3;
const int kernel_size = 3;
const char* window_name = "Edge Map";
bool newfolder = false;
Mat translateImg(Mat &img, int x, int y) {
Mat trans_mat = (Mat_<double>(2, 3) << 1, 0, x, 0, 1, y);
warpAffine(img, img, trans_mat, img.size());
return img;
}
void imgpreprocess(Mat & Thresimg, Mat & imgtosave)
{
cv::Mat imgcrop;
//normalize
normalize(Thresimg, Thresimg, 0, 255, NORM_MINMAX, -1, Mat());
//Histogram Equalization
equalizeHist(Thresimg, Thresimg);
translateImg(Thresimg, -35, 30);
// crop image and resize
Rect roi(150, 70, 350, 250);
resize(Thresimg(roi), imgtosave, Size(640, 480));
}
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;
}
int main(int argc, char * argv[])
//Create the RGB and Depth Windows
cv::namedWindow("RGB", cv::WindowFlags::WINDOW_AUTOSIZE | cv::WindowFlags::WINDOW_GUI_EXPANDED);
cv::namedWindow("Depth", cv::WindowFlags::WINDOW_AUTOSIZE | cv::WindowFlags::WINDOW_GUI_EXPANDED);
cv::namedWindow("InitialFrame", cv::WindowFlags::WINDOW_AUTOSIZE | cv::WindowFlags::WINDOW_GUI_EXPANDED);
//cv::namedWindow("Threshold", cv::WindowFlags::WINDOW_AUTOSIZE | cv::WindowFlags::WINDOW_GUI_EXPANDED);
//The key value represents a key pressed on the keyboard,
//Where 27 is the ESC key
const std::string labels[] = { "Baby","Dog","Dinosaur","Coffee_Tin","Mug",
"Car","Camera","Keyboard","Koala","Blank","Blackberry",
"Diet_Coke_Bottle","Duck","Dragon","Android" };
char key = '0';
int framecounter = 0;
//Initial background frame
//Status represents curr status of the Playback wrapper
//Value of 0 = finihsed playback
// The status can by bitwise AND to determine if the RGB or
// Depth image has been updated using the State enum.
uint8_t status = 255;
while (key != 27 && status != 0)
{
// Loads in the next frame of Kinect data into the
// wrapper. Also pauses between the frames depending
// on the time between frames.
status = wrap.GetNextFrame();
// Determine if RGB is updated, and grabs the image
// if it has been updated
if (status & State::UPDATED_RGB)
currentRGB = wrap.RGB;
// Determine if Depth is updated, and grabs the image
// if it has been updated
if (status & State::UPDATED_DEPTH)
currentDepth = wrap.Depth;
threshold(currentDepth, currentThreshold, 80, 255, THRESH_BINARY);
currentThreshold.copyTo(newdepth);
if (framecounter == 0) {
initialframe = wrap.Depth;
threshold(initialframe, initthreshold, 80, 255, THRESH_BINARY);
initthreshold.copyTo(initDepth);
}
framecounter++;
//Create labels
String currRGB = ("UpdatedRGB" + std::to_string(framecounter) + ".jpg");
String currDepth = ("Image" + std::to_string(framecounter) + ".jpg");
//Write the image to the filepath
/*
*/
/// Create a matrix of the same type and size as the frame
///Create another matrix of the same type
//backgrnd.create(initialframe.size(), initialframe.type());
//cvtColor(initialframe, Init_gray, COLOR_BGR2GRAY);
/// Create a Trackbar for user to enter threshold
/// Show the image
cv::imshow("InitialFrame", initDepth);
cv::imshow("RGB", currentRGB);
cv::imshow("Depth", newdepth);
if (!checkDiff(newdepth, initDepth) && newfolder == false) {
cout << "TRUE" << endl;
newfolder = true;
if (newfolder) {
path = "B:/TrainingData/" + labels[classcounter] + "/";
cout << path << endl;
if (classcounter != 9) {
mkdir(path.c_str());
}
if (classcounter <= 13) {
classcounter++;
}
}
}
else if (checkDiff(newdepth, initDepth)) {
cout << "FALSE" << endl;
newfolder = false;
}
if (!checkDiff(newdepth, initDepth)) {
cv::Mat imgtosave;
imgpreprocess(newdepth, imgtosave);
imwrite(path + currDepth, imgtosave);
}
// Check for keyboard input
key = cv::waitKey(10);
}
return 0;
}