最近在做一个目标检测算法,训练时用到了 bootstrap 策略,于是我将 PASCAL 的 Ground Truth 格式的读取函数从 Matlab 改写为 C++。PASCAL 的标注格式为:
# PASCAL Annotation Version 1.00
Image filename : "对应图片路径"
Image size (X x Y x C) : 宽 x 高 x 3
Database : "数据库名称"
Objects with ground truth : 1 { "PASperson" }
# Note that there might be other objects in the image
# for which ground truth data has not been provided.
# Top left pixel co-ordinates : (0, 0)
# Details for object 1 ("PASperson")
# Center point -- not available in other PASCAL databases -- refers
# to person head center
Original label for object 1 "PASperson" : "UprightPerson"
Center point on object 1 "PASperson" (X, Y) : (257, 187)
Bounding box for object 1 "PASperson" (Xmin, Ymin) - (Xmax, Ymax) : (195, 154) - (297, 468)
我写的函数如下:
#include "stdio.h" #include "string.h" #include "stdlib.h" // object bounding rect struct GtRect { int x_min; int y_min; int x_max; int y_max; }; // ground truth of one image struct GtRecord { char* image_name; GtRect* objs; int obj_num; int height; int width; int channels; }; // return true if c is in char set s int _is_chars(char c, const char* s, int n) { for (int i = 0; i != n; ++i) { if (s[i] == c) { return 1; } } return 0; } void _trim_l(char* inout, const char* s) { int len = strlen(inout); int s_len = strlen(s); int i = 0; for (;i != len; ++i) { if (!_is_chars(inout[i], s, s_len)) { break; } } int d = i; int new_len = len - d; for (i = 0; i != new_len; ++i) { inout[i] = inout[i + d]; } inout[new_len] = '