utility.h 11.2 KB
Newer Older
1
namespace fs = std::__fs::filesystem;
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
2
using namespace cv;
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using namespace std;

std::string pathBrand, pathFullFrame, pathTape, pathTape224, pathEndTape;

/* ------------------------------------------------------------------------------
FILE SYSTEM FUNCTIONS
------------------------------------------------------------------------------ */

void makeDirectories(std::string fileName, std::string outputPath, bool brands) {
	// Make output directories
	pathBrand = outputPath + "/brand";
	pathFullFrame = outputPath + "/fullFrame";
	pathTape = outputPath + "/tape";
	pathTape224 = outputPath + "/tape224";
	pathEndTape = outputPath + "/endTape";
	if (brands)
		int brandDirectory = fs::create_directory(pathBrand);
	int fullFrameDirectory = fs::create_directory(pathFullFrame);
	int nastroDirectory = fs::create_directory(pathTape);
	int nastro224Directory = fs::create_directory(pathTape224);
	int capsDirectory = fs::create_directory(pathEndTape);
}

Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
26
void saveIrregularityImage(std::string safeTimeLabel, std::string fileName, cv::Mat frame, cv::Mat subFrame, bool endFrame, bool brandFrame) {
27
28
29
30
31
32
33

	// if (savingPinchRoller) {
	// 	cv::imwrite(pathEndTape + "/" + fileName + "_" + safeTimeLabel + ".jpg", frame);
	// } else if (savingBrand) {
	// 	cv::imwrite(pathBrand + "/" + fileName + "_" + safeTimeLabel + ".jpg", frame);
	// 	savingBrand = false;
	// } else {
34

35
36
37
		// Writing image
		cv::imwrite(pathFullFrame + "/" + fileName + "_" + safeTimeLabel + ".jpg", frame);

Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
38
		if (endFrame) {
39
			cv::imwrite(pathEndTape + "/" + fileName + "_" + safeTimeLabel + ".jpg", subFrame);
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
40
41
42
43
		} else if (brandFrame) {
			cv::imwrite(pathBrand + "/" + fileName + "_" + safeTimeLabel + ".jpg", subFrame);
		} else {
			cv::imwrite(pathTape + "/" + fileName + "_" + safeTimeLabel + ".jpg", subFrame);
44
		}
45

46
47
48
		cv::Mat resized224Nas;
		cv::resize(subFrame, resized224Nas, cv::Size(224, 224));
		cv::imwrite(pathTape224 + "/"+ fileName + "_" + safeTimeLabel + ".jpg", resized224Nas);
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
	// }

}


/* ------------------------------------------------------------------------------
TIME FUNCTIONS
------------------------------------------------------------------------------ */

std::string getTimeLabel(int ms) {
				
	int mil = ms % 1000;
	int sec = ms / 1000;
	int min = (sec / 60) % 60;
	int hours = sec / 3600;
	sec = sec % 60;

	std::string hoursStr = std::to_string(hours), minStr = std::to_string(min), secStr = std::to_string(sec), milStr = std::to_string(mil);
	if (hours < 10)
		hoursStr = "0" + hoursStr;
	if (min < 10)
		minStr = "0" + minStr;
	if (sec < 10)
		secStr = "0" + secStr;
	if (mil < 100) {
		if (mil < 10) {
			milStr = "00" + milStr;
		} else {
			milStr = "0" + milStr;
		}
	}
	
	std::string timeLabel = hoursStr + ":" + minStr + ":" + secStr + "." + milStr;

	return timeLabel;

}

// This function is exactly like getTimeLabel, but without punctuation in the returned string
// due to file system necessities
std::string getSafeTimeLabel(int ms) {
				
	int mil = ms % 1000;
	int sec = ms / 1000;
	int min = (sec / 60) % 60;
	int hours = sec / 3600;
	sec = sec % 60;

	std::string hoursStr = std::to_string(hours), minStr = std::to_string(min), secStr = std::to_string(sec), milStr = std::to_string(mil);
	if (hours < 10)
		hoursStr = "0" + hoursStr;
	if (min < 10)
		minStr = "0" + minStr;
	if (sec < 10)
		secStr = "0" + secStr;
	if (mil < 100) {
		if (mil < 10) {
			milStr = "00" + milStr;
		} else {
			milStr = "0" + milStr;
		}
	}
	
	std::string timeLabel = hoursStr + "-" + minStr + "-" + secStr + "-" + milStr;

	return timeLabel;

}

int getMilliCount() {
    timeb tb;
    ftime(&tb);
    int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
    return nCount;
}


int getMilliSpan(int nTimeStart) {
    int nSpan = getMilliCount() - nTimeStart;
    if (nSpan < 0)
        nSpan += 0x100000 * 1000;
    return nSpan;
}


/* ------------------------------------------------------------------------------
PARSING FUNCTIONS
------------------------------------------------------------------------------ */

void findFileNameFromPath(std::string* path, std::string* fileName, std::string* extension) {
  *path = path->substr(path->find_last_of("'") + 1, path->size());
  std::string path_without_extension = path->substr(0, path->find_last_of("."));
  *fileName = path_without_extension.substr(path_without_extension.find_last_of("/") + 1, path_without_extension.size());
  *extension = path->substr(path->find_last_of(".") + 1, path->size());
}

// Obtain video file name without path
int findFileName(std::string videoPath, std::string &fileName, std::string &extension) {

    // Divide file name from extension
    findFileNameFromPath(&videoPath, &fileName, &extension);

    if (extension.compare("avi") != 0 && extension.compare("mp4") != 0 && extension.compare("mov") != 0) {
        std::cerr << "Input file extension must be \"avi\", \"mp4\" or \"mov\"." << std::endl;
        return -1;
    } else {
        std::cout << "\nVideo to be analysed: " << std::endl;  
        std::cout << "  File name:  " << fileName << std::endl;
        std::cout << "  Extension:  " << extension << std::endl;
    }
    return 0;
}


/* ------------------------------------------------------------------------------
COMPUTER VISION FUNCTIONS
------------------------------------------------------------------------------ */

Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
168
169
170
171
172
173
174
175
176
177
178
179
180
// Function to detect shape in frame
void detectShape(Ptr<GeneralizedHoughGuil> alg, Mat templateShape, int posThresh, vector<Vec4f> &positivePositions, Mat &positiveVotes, vector<Vec4f> &negativePositions, Mat &negativeVotes, Mat processingArea) {
	
	alg -> setPosThresh(posThresh);
	alg -> setTemplate(templateShape);

	int oldSizePositive = 0;
	int i = 0;
	int maxVote = 0;

	// Process shapes with positive angles
	alg -> setMinAngle(0);
	alg -> setMaxAngle(5);
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
181
	std::cout << "Processing positive angles..." << endl;
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
182
183
184
185
186
187
188
189
190
191
192
193
	while (true) {
		alg -> detect(processingArea, positivePositions, positiveVotes);
		int currentSize = positivePositions.size();
		if (currentSize == 1) {
			// We detected the most interesting shape
			break;
		} else if (currentSize == 0 && oldSizePositive > 0) {
			// It is not possible to detect only one shape with the current parameters
			alg -> setPosThresh(posThresh+i-1); // Decrease position value
			alg -> detect(processingArea, positivePositions, positiveVotes); // Detect all available shapes
			break;
		} else if (currentSize == 0 && oldSizePositive == 0) {
194
			// Impossible to find with these parameters
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
195
			std::cout << "\033[0;31mNot found for positive angles.\033[0m" << endl;
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
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
			break;
		}
		oldSizePositive = currentSize;
		// Find maximum vote
		for (int j = 0; j < positiveVotes.cols / 3; j++) {
			if (positiveVotes.at<int>(3*j) > maxVote)
				maxVote = positiveVotes.at<int>(3*j);
		}

		if (currentSize > 10) {
			i += 5; // To speed up computation when there are too many matches
		} else if (maxVote - (posThresh + i) > 100) {
			i += 100; // To speed up computation when there are few super high matches
		} else {
			i++;
		}
		alg -> setPosThresh(posThresh+i);
	}

	int oldSizeNegative = 0;
	// Reset incremental position value
	i = 0;
	maxVote = 0;
	// Process shapes with negative angles
	alg -> setMinAngle(355);
	alg -> setMaxAngle(360);
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
222
	std::cout << "Processing negative angles..." << endl;
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
223
224
225
226
227
228
229
230
231
232
233
234
235
	while (true) {
		alg -> detect(processingArea, negativePositions, negativeVotes);
		int currentSize = negativePositions.size();
		if (currentSize == 1) {
			// We detected the most interesting shape
			break;
		} else if (currentSize == 0 && oldSizeNegative > 0) {
			// It is not possible to detect only one shape with the current parameters
			alg -> setPosThresh(posThresh+i-1); // Decrease position value
			alg -> detect(processingArea, negativePositions, negativeVotes); // Detect all available shapes
			break;
		} else if (currentSize == 0 && oldSizeNegative == 0) {
			// Impossible to found with these parameters
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
236
			std::cout << "\033[0;31mNot found for negative angles.\033[0m" << endl;
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
			break;
		}
		oldSizeNegative = currentSize;

		// Find maximum vote
		for (int j = 0; j < positiveVotes.cols / 3; j++) {
			if (positiveVotes.at<int>(3*j) > maxVote)
				maxVote = positiveVotes.at<int>(3*j);
		}

		if (currentSize > 10) {
			i += 5; // To speed up computation when there are too many matches
		} else if (maxVote - (posThresh + i) > 100) {
			i += 100; // To speed up computation when there are few super high matches
		} else {
			i++;
		}
		alg -> setPosThresh(posThresh+i);
	}
}

// Function to draw detected shapes in a frame
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
259
RotatedRect drawShapes(Mat frame, vector<Vec4f> &positions, Scalar color, int width, int height, int offsetX, int offsetY, float processingScale) {
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
260
261
262
263
264

	RotatedRect rr;
	Point2f rrpts[4];

	for (int i = 0; i < positions.size(); i++) {
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
265
		Point2f pos(positions[i][0]+offsetX, positions[i][1]+offsetY);
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
266
267
268
		float scale = positions[i][2];
		float angle = positions[i][3];

Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
269
270
		rr.center = pos * processingScale;
		rr.size = Size2f(width * scale * processingScale, height * scale * processingScale);
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
271
272
273
274
275
276
277
278
279
		rr.angle = angle;

		rr.points(rrpts);

		line(frame, rrpts[0], rrpts[1], color, 2);
		line(frame, rrpts[1], rrpts[2], color, 2);
		line(frame, rrpts[2], rrpts[3], color, 2);
		line(frame, rrpts[3], rrpts[0], color, 2);
	}
Nadir Dalla Pozza's avatar
Update.    
Nadir Dalla Pozza committed
280
281

	return rr;
Nadir Dalla Pozza's avatar
Nadir Dalla Pozza committed
282
283
}

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Function to separate even and odd frame half planes
void separateFrame(cv::Mat frame, cv::Mat &frame_dispari, cv::Mat &frame_pari) {

	int i_frame_dispari = 0;
	int i_frame_pari = 0;

	for (int i = 0; i < frame.rows; i++) {
		for (int j = 0; j < frame.cols; j++) {
			if (i % 2 == 0) {
				frame_pari.at<cv::Vec3b>( i_frame_pari, j )[0] = frame.at<cv::Vec3b>(i, j)[0];
				frame_pari.at<cv::Vec3b>( i_frame_pari, j )[1] = frame.at<cv::Vec3b>(i, j)[1];
				frame_pari.at<cv::Vec3b>( i_frame_pari, j )[2] = frame.at<cv::Vec3b>(i, j)[2];
			} else {
				frame_dispari.at<cv::Vec3b>( i_frame_dispari, j )[0] = frame.at<cv::Vec3b>(i, j)[0];
				frame_dispari.at<cv::Vec3b>( i_frame_dispari, j )[1] = frame.at<cv::Vec3b>(i, j)[1];
				frame_dispari.at<cv::Vec3b>( i_frame_dispari, j )[2] = frame.at<cv::Vec3b>(i, j)[2];
			}
		}

		if (i % 2 == 0) {
			i_frame_pari++;
		} else {
			i_frame_dispari++;
		}
	}

	return;

}

// This function finds the straight lines delimiting the rotated rectangle.
void findRectBound(cv::Point p1, cv::Point p2, cv::Point p3, cv::Point p4, double &m1, double &m2, double &m3, double &m4, double &q1, double &q2, double &q3, double &q4) {
	int y1 = p1.y;
	int y2 = p2.y;
	int y3 = p3.y;
	int y4 = p4.y;

	int x1 = p1.x;
	int x2 = p2.x;
	int x3 = p3.x;
	int x4 = p4.x;

	// Finding the straight lines intersecting in y1 [p0.y]
	m1 = (double)(y2 - y1)/(x2 - x1);
	q1 = (y1 - m1*x1);
		
	m4 = (double)(y4 - y1)/(x4 - x1);
	q4 = (y1 - m4*x1);

	// Finding the straight lines intersecting in y3 [p2.y]
	m2 = (double)(y3 - y2)/(x3 - x2);
	q2 = (y3 - m2*x3);
	
	m3= (double)(y4 - y3)/(x4 - x3);
	q3 = (y3 - m3*x3);
}

cv::Mat difference(cv::Mat &prevFrame, cv::Mat &currentFrame) {
	cv::Mat diff = currentFrame.clone();
	for (int i = 0; i < currentFrame.rows; i++) {
		for (int j = 0; j < currentFrame.cols; j++) {
			if (prevFrame.at<cv::Vec3b>(i, j)[0] != currentFrame.at<cv::Vec3b>(i, j)[0] || prevFrame.at<cv::Vec3b>(i, j)[1] != currentFrame.at<cv::Vec3b>(i, j)[1] || prevFrame.at<cv::Vec3b>(i, j)[2] != currentFrame.at<cv::Vec3b>(i, j)[2]) {
				// Different pixels
				diff.at<cv::Vec3b>(i, j)[0] = 0;
			} else {
				// Identical pixels
				diff.at<cv::Vec3b>(i, j)[0] = 255;
			}
		}
	}
	return diff;
}