forked from Horea94/Fruit-Images-Dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_images.cpp
333 lines (271 loc) · 11.6 KB
/
extract_images.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
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
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
// Extracts fruits images from videos
// background should be uniform with small fluctuations
// color_distance constant tells how to which extent 2 background pixels can be considered similar; more is better
// authors: Mihai Oltean & Horea Muresan
// MIT license
// compiled with Visual Studio 2019 Community Edition
// requires OpenCV 4
// if you get stack overflow ... just increase the stack reserve size from Linker menu ...
// How to use:
// 1. Modify the fruits_definition.h file for a new fruit; r_box parameters must be modified for each fruit (see step 3)
// 2. Comment SAVE_IMAGES_TO_DISK and uncomment DISPLAY_ONLY
// 3. You must play with r_box parameters until the fruit fits inside the r_box.
// Also must play with motor_shaft_height_original, shaft_left and shaft_right parameters until motor shaft fits inside the white rectangle.
// 4. Uncomment SAVE_IMAGES_TO_DISK an comment DISPLAY_ONLY
// 5. Modify the color_distance if needed. If color distance is too big the object will not appear. If it is too low the margins of the object are fuzzy.
#include <opencv2/opencv.hpp>
#include <opencv2/imgcodecs.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "fruits_definition.h"
using namespace std;
using namespace cv;
#define VERSION "2019.08.13.0"
#define MAX_IMAGES_TO_EXTRACT 312
//---------------------------------------------------------------------
#define smaller_image_size 100 // result is stored in 100x100 matrices
#define FRUIT_PIXEL 2
#define BACKGROUND_PIXEL 1
#define NOT_MARKED_PIXEL 0
//---------------------------------------------------------------------
int get_color_distance(Vec3b &color1, Vec3b &color2)
{
// distance between 2 colors
int c_red = abs(color1[0] - color2[0]);
int c_green = abs(color1[1] - color2[1]);
int c_blue = abs(color1[2] - color2[2]);
int max = c_red;
if (max < c_green)
max = c_green;
if (max < c_blue)
max = c_blue;
return max;
}
//---------------------------------------------------------------------
void flood_fill_background(int row, int col, Mat &picture, char** filled_matrix, int color_tolerance, int image_size, Vec3b &initial_color)
{
// flood fill starting with position (row, col)
// only 4 neighbours of the current pixel are considered
// 2 pixels are connected if their color distance is smaller than color_tolerance value
// picture matrix is from where the color of pixels are taken
// the flood result is stored in matrix filled_matrix (1 if flooded, 0 otherwise)
/*
for (int i = 0; i < image_size; i++)
for (int j = 0; j < image_size; j++)
if (get_color_distance(picture->at<Vec3b>(Point(0, 0)), picture->at<Vec3b>(Point(i, j))) <= color_tolerance) {
filled_matrix[i][j] = BACKGROUND_PIXEL;
}
*/
if (filled_matrix[row][col] == NOT_MARKED_PIXEL) {
filled_matrix[row][col] = BACKGROUND_PIXEL;
if (col + 1 < image_size &&
get_color_distance(picture.at<Vec3b>(row, col), picture.at<Vec3b>(row, col + 1)) <= color_tolerance
//&& get_color_distance(initial_color, picture->at<Vec3b>(Point(y + 1, x))) <= color_tolerance
)
flood_fill_background(row, col + 1, picture, filled_matrix, color_tolerance, image_size, initial_color);
if (col - 1 >= 0 && get_color_distance(picture.at<Vec3b>(row, col), picture.at<Vec3b>(row, col - 1)) <= color_tolerance
//&& get_color_distance(initial_color, picture->at<Vec3b>(Point(y - 1, x))) <= color_tolerance
)
flood_fill_background(row, col - 1, picture, filled_matrix, color_tolerance, image_size, initial_color);
if (row + 1 < image_size &&
get_color_distance(picture.at<Vec3b>(row, col), picture.at<Vec3b>(row + 1, col)) <= color_tolerance
//&& get_color_distance(initial_color, picture->at<Vec3b>(Point(y, x + 1))) <= color_tolerance
)
flood_fill_background(row + 1, col, picture, filled_matrix, color_tolerance, image_size, initial_color);
if (row - 1 >= 0 &&
get_color_distance(picture.at<Vec3b>(row, col), picture.at<Vec3b>(row - 1, col)) <= color_tolerance
//&& get_color_distance(initial_color, picture->at<Vec3b>(Point(y, x - 1))) <= color_tolerance
)
flood_fill_background(row - 1, col, picture, filled_matrix, color_tolerance, image_size, initial_color);
}
}
//---------------------------------------------------------------------
void flood_fill_fruit(int row, int col, char** filled_matrix, int image_size)
// fills an island of pixels having the same value 1
{
// only 4 neighbours are considered
if (filled_matrix[row][col] == NOT_MARKED_PIXEL) {
filled_matrix[row][col] = FRUIT_PIXEL;
if (col + 1 < image_size && filled_matrix[row][col + 1] == NOT_MARKED_PIXEL)
flood_fill_fruit(row, col + 1, filled_matrix, image_size);
if (col - 1 >= 0 && filled_matrix[row][col - 1] == NOT_MARKED_PIXEL)
flood_fill_fruit(row, col - 1, filled_matrix, image_size);
if (row + 1 < image_size && filled_matrix[row + 1][col] == NOT_MARKED_PIXEL)
flood_fill_fruit(row + 1, col, filled_matrix, image_size);
if (row - 1 >= 0 && filled_matrix[row - 1][col] == NOT_MARKED_PIXEL)
flood_fill_fruit(row - 1, col, filled_matrix, image_size);
}
}
//---------------------------------------------------------------------
Rect compute_fruit_bounding_box(char** matrix, int image_size)
{// compute the bounding box of the fruit
Rect bbox;
// maximal bbox is :
bbox.x = image_size - 1;
bbox.y = image_size - 1;
int right = 0;
int bottom = 0;
for (int row = 0; row < image_size; row++)
for (int col = 0; col < image_size; col++)
if (matrix[row][col] == FRUIT_PIXEL) {
if (bbox.x > col)
bbox.x = col;
if (bbox.y > row)
bbox.y = row;
if (right < col)
right = col;
if (bottom < row)
bottom = row;
}
bbox.width = right - bbox.x + 1;
bbox.height = bottom - bbox.y + 1;
return bbox;
}
//---------------------------------------------------------------------
bool remove_background(Mat &input_image, Mat &out_image)
{
int image_width = input_image.cols; //smaller_image_size;
int image_height = input_image.rows; //smaller_image_size;
// allocate memory
char **matrix = new char*[image_height];
for (int row = 0; row < image_height; row++) {
matrix[row] = new char[image_width];
for (int col = 0; col < image_width; col++)
matrix[row][col] = NOT_MARKED_PIXEL; // 2 - fruit // 1- background
}
// star point is every pixel from the border
// top side
for (int col = 0; col < image_width; col++)
flood_fill_background(0, col, input_image, matrix, color_distance, image_width, input_image.at<Vec3b>(0, col));
// left side
for (int row = 0; row < image_height; row++)
flood_fill_background(row, 0, input_image, matrix, color_distance, image_width, input_image.at<Vec3b>(row, 0));
// right side
for (int row = 0; row < image_height; row++)
flood_fill_background(row, image_width - 1, input_image, matrix, color_distance, image_width, input_image.at<Vec3b>(row, image_width - 1));
/*
// try to remove the motor shaft
for (int col = 0; col < image_width; col++)
if (col < shaft_left || col > shaft_right)
flood_fill_background(image_height - motor_shaft_height_original - 1, col, input_image, matrix, 1, image_width,
input_image.at<Vec3b>(image_height - motor_shaft_height_original - 1, col)
);
*/
// now I start from the center and fill the object
// I did that because we can have multiple islands and only 1 is of interest
flood_fill_fruit((image_height - motor_shaft_height_original) / 2, image_width / 2, matrix, image_width);
// ignore everything that is below shaft
for (int row = image_height - motor_shaft_height_original; row < image_height; row++)
for (int col = 0; col < image_width; col++)
matrix[row][col] = BACKGROUND_PIXEL;
// find bounding box
Rect fruit_bbox;// (0, 0, image_width - 1, image_height - 1);
fruit_bbox = compute_fruit_bounding_box(matrix, image_width);
// keep only the part of the image that contains the fruit
if (fruit_bbox.width > 0 && fruit_bbox.height > 0) {
// clone the fruit only in a temporary matrix
//Mat fruit_clone = image(fruit_bbox).clone();
int max_size = fruit_bbox.height;
if (max_size < fruit_bbox.width)
max_size = fruit_bbox.width;
Mat fruit_clone = Mat(max_size + 5, max_size + 5, input_image.type());
fruit_clone.setTo(Scalar(255, 255, 255));
for (int row = 0; row < image_height; row++)
for (int col = 0; col < image_width; col++)
if (matrix[row][col] == FRUIT_PIXEL) // fruit
fruit_clone.at<Vec3b>(row - fruit_bbox.y + (max_size - fruit_bbox.height) / 2, col - fruit_bbox.x + (max_size - fruit_bbox.width) / 2) = input_image.at<Vec3b>(row, col);
// make the out image white
out_image.setTo(Scalar(255, 255, 255));
// replace the original matrix with the smaller image containig the fruit
out_image = fruit_clone; // new_image;
// delete memory
for (int row = 0; row < image_height; row++)
delete[] matrix[row];
delete[] matrix;
return true;
}
else {
// delete memory
for (int row = 0; row < image_height; row++)
delete[] matrix[row];
delete[] matrix;
return false; // image is empty; no fruit detected; change parameters !
}
}
//---------------------------------------------------------------------
int main(void)
{
printf("Program version = %s\n", VERSION);
// open video
VideoCapture input_video(input_file_name + ".avi");
if (!input_video.isOpened()){
printf("Could not open the input video! Press ENTER to terminate.");
getchar();
return -1;
}
// folder where to save the images
string folderCreateCommand = "mkdir \"" + input_file_name + "\"";
system(folderCreateCommand.c_str());
Mat input_image;
// create window
namedWindow("out_image", 1);
Mat out_image;
for (int i = 0; i < 3; i++)// skip first 3 frames
input_video >> input_image;
int frame_index = frame_start_index;
while (1) {
// take a capture from input stream
input_video >> input_image;
if (input_image.empty())
break;
// extract a submatrix (having corners stored in r_box) from the original matrix
Mat smaller_image(r_box.width, r_box.height, input_image.type());
smaller_image.setTo(Scalar(255, 255, 255));
for (int row = 0; row < r_box.height; row++)
for (int col = 0; col < r_box.width; col++)
if (row + r_box.y < input_image.rows && col + r_box.x < input_image.cols)
smaller_image.at<Vec3b>(row, col) = input_image.at<Vec3b>(row + r_box.y, col + r_box.x);
//smaller_image = input_image(r_box);
// compute image file name
string out_filename = input_file_name + "\\" + to_string(frame_index) + "_" + to_string(smaller_image_size) + ".jpg";
#ifndef DISPLAY_ONLY
// resize
//resize(smaller_image, smaller_image, Size(smaller_image_size, smaller_image_size));
// remove margins
if (remove_background(smaller_image, out_image)) {
imshow("out_image", out_image);
// show the results on the screen
resize(out_image, out_image, Size(smaller_image_size, smaller_image_size));
// resize again
// imshow("image", smaller_image);
#ifdef SAVE_IMAGES_TO_DISK
if (!imwrite(out_filename, out_image)) {
printf("Cannot write image!\n");
break;
}
#endif
frame_index++;
if (frame_index >= MAX_IMAGES_TO_EXTRACT + frame_start_index) // keep only the first 328 images
break;
}
#endif
#ifdef SAVE_IMAGES_TO_DISK
if (!imwrite(out_filename, out_image)) {
printf("Cannot write image!\n");
break;
}
#else
// draw shaft
rectangle(smaller_image, Rect(shaft_left, smaller_image.rows - motor_shaft_height_original, shaft_right - shaft_left, motor_shaft_height_original), Scalar(255, 255, 255));
imshow("image", smaller_image);
#endif
int key = waitKey(1); // key pressed
if (key == 27) // Escape pressed ?
break;
}
input_video.release();
destroyWindow("out_image");
return 0;
}
//---------------------------------------------------------------------