@@ -10,11 +10,20 @@ using namespace cv::datasets;
10
10
int main (int argc, char *argv[])
11
11
{
12
12
const char *keys =
13
- " { help h usage ? | | show this message }"
14
- " { path p |true| path to dataset }" ;
13
+ " { help h usage ? | | show this message }"
14
+ " { path p |true | path to dataset }"
15
+ " { save s |false| save resized positive images }"
16
+ " { rwidth rw |64 | width of resized positive images }"
17
+ " { rheight rh |128 | height of resized positive images }"
18
+ " { padding |8 | vertical padding of resized positive images }" ;
15
19
16
20
CommandLineParser parser (argc, argv, keys);
21
+ bool savebbox = parser.get <bool >(" save" );
22
+ int rwidth = parser.get <int >(" rwidth" );
23
+ int rheight = parser.get <int >(" rheight" );
24
+ int padding = parser.get <int >(" padding" );
17
25
string path (parser.get <string>(" path" ));
26
+
18
27
if (parser.has (" help" ) || path==" true" )
19
28
{
20
29
parser.printMessage ();
@@ -29,6 +38,8 @@ int main(int argc, char *argv[])
29
38
cout << " train size: " << train_size << endl;
30
39
cout << " test size: " << test_size << endl;
31
40
41
+ int bbox_count = 0 ;
42
+
32
43
for ( size_t i = 0 ; i < train_size; i++ )
33
44
{
34
45
PD_inriaObj *example = static_cast <PD_inriaObj *>(dataset->getTrain ()[i].get ());
@@ -46,18 +57,42 @@ int main(int argc, char *argv[])
46
57
// bounding boxes
47
58
for ( size_t j = 0 ; j < example->bndboxes .size (); j++ )
48
59
{
49
- cout << " object " << j << endl;
50
- int x = example->bndboxes [j].x ;
51
- int y = example->bndboxes [j].y ;
52
- cout << " - xmin = " << x << endl;
53
- cout << " - ymin = " << y << endl;
54
- cout << " - xmax = " << example->bndboxes [j].width + x << endl;
55
- cout << " - ymax = " << example->bndboxes [j].height + y << endl;
56
- rectangle ( img, example->bndboxes [j], Scalar ( 0 , 0 , 255 ), 2 );
60
+ Rect obj_bndbox = example->bndboxes [j]; // bounding box of object
61
+ cout << " - bounding box: " << j << " - " << obj_bndbox << endl;
62
+
63
+ int vpadding, hpadding;
64
+ Rect ex_bndbox; // variable used for calculating expanded bounding box
65
+
66
+ vpadding = cvRound (padding * obj_bndbox.height / rheight); // calculate vertical padding
67
+ ex_bndbox.y = obj_bndbox.y - vpadding;
68
+ ex_bndbox.height = 2 * vpadding + obj_bndbox.height ;
69
+ ex_bndbox.x = obj_bndbox.x + (obj_bndbox.width / 2 );
70
+ ex_bndbox.width = ex_bndbox.height * rwidth / rheight;
71
+ ex_bndbox.x -= (ex_bndbox.width + 1 ) / 2 ;
72
+
73
+ if (obj_bndbox.width > ex_bndbox.width )
74
+ {
75
+ obj_bndbox.x += (obj_bndbox.width - ex_bndbox.width + 1 ) / 2 ;
76
+ obj_bndbox.width = ex_bndbox.width ;
77
+ }
78
+
79
+ hpadding = obj_bndbox.x - ex_bndbox.x ; // calculate horizontal padding
80
+
81
+ if (savebbox)
82
+ {
83
+ Mat dst;
84
+ copyMakeBorder (img (obj_bndbox), dst, vpadding, vpadding, hpadding, hpadding, BORDER_REFLECT);
85
+ resize (dst, dst, Size (rwidth, rheight), 0 , 0 , INTER_AREA);
86
+ imwrite (path + format (" person_%04d.png" , bbox_count++), dst);
87
+ }
88
+ else
89
+ rectangle (img, obj_bndbox, Scalar (0 , 0 , 255 ), 2 );
57
90
}
58
91
59
- imshow (" INRIAPerson Dataset Train Images" , img);
92
+ if (savebbox)
93
+ continue ; // skip UI updates
60
94
95
+ imshow (" INRIAPerson Dataset Train Images" , img);
61
96
cout << " \n Press a key to continue or ESC to exit." << endl;
62
97
int key = waitKey ();
63
98
if ( key == 27 ) break ;
0 commit comments