21 #ifndef _SOURCE_DATA_SET_H
22 #define _SOURCE_DATA_SET_H
45 template<
class TPreprocessorType,
class Aggregator>
59 bool m_export_data_set;
62 std::string m_export_path;
66 SourceDataSet() : m_initialized(false), m_export_data_set(false) { }
73 m_export_data_set =
true;
74 m_export_path = folder;
83 template<
class TPreprocessorType,
class Aggregator>
86 typename TPreprocessorType::ReturnType data;
91 int count = this->templates.size();
93 sprintf(buffer,
"%s/P%06u_%u.pgm", m_export_path.c_str(), count,
frameidx);
95 sprintf(buffer,
"%s/N%06u_%u.pgm", m_export_path.c_str(), count,
frameidx);
102 TPreprocessorType::Process(data, (
unsigned char *) image.
data, image.
width, image.height, image.
stride );
104 else if(image.
bpp == 2)
106 TPreprocessorType::Process(data, (
unsigned short *) image.
data, image.
width, image.height, image.
stride );
110 std::cerr <<
"Invalid bpp = " << image.
bpp <<
" for this data set" << std::endl;
117 this->SetParams(data.second);
118 m_initialized =
true;
123 if(!( static_cast<const ParamType &>(*
this) == data.second))
125 std::cerr <<
"invalid geometry" << std::endl;
131 this->Import(data.first, category);
137 template<
class DataSetType>
140 if(img.
width < set_geometry.width || img.height < set_geometry.height)
142 std::cerr <<
"the image is too small to be used during negative random extraction" << std::endl;
147 for(
int k=0; k<nRand; ++k)
149 int x0 = rand() % (img.
width - set_geometry.width);
150 int y0 = rand() % (img.height - set_geometry.height);
151 rect roi(x0, y0, x0 + set_geometry.width, y0 + set_geometry.height);
153 bool collision =
false;
155 for(std::vector<datasetobject>::const_iterator i =
object.begin(); i !=
object.end(); ++i)
164 if(!set.ImportImage(img.
crop(roi), -1))
166 std::cerr <<
"Failed to import pattern";
185 template<
class DataSetType>
189 size set_geometry = sz;
191 if(set_geometry.width != 0 || set_geometry.height!=0)
192 std::cout <<
"LoadSet with geometry " << set_geometry.width <<
'x' << set_geometry.height << std::endl;
201 std::cout <<
"Warn: \'" << item.
filename <<
"' failed to load: skip." << std::endl;
206 for(std::vector<datasetobject>::const_iterator i = item.
object.begin(); i != item.
object.end(); ++i)
212 if(roi.x0 == 0 && roi.y0 == 0 && roi.x1 == 0 && roi.y1 == 0)
218 if(roi.x0 < 0 || roi.y0 < 0 || roi.x1 > (
int) img.
width || roi.y1 > (int) img.height)
220 std::cout <<
"Warn: ROI #" << (i - item.
object.begin()) <<
" is out of image " << item.
filename << std::endl;
225 if(set_geometry.width == 0 && set_geometry.height==0)
227 set_geometry.width = roi.width();
228 set_geometry.height = roi.height();
229 std::cout <<
"Geometry: " << set_geometry.width <<
'x' << set_geometry.height << std::endl;
232 if(set_geometry == roi.extension())
234 if(!set.ImportImage(img.
crop(roi), i->category))
235 std::cerr <<
"Failed to import image " << item.
filename << std::endl;
242 img2.
alloc(set_geometry.width, set_geometry.height, 1);
244 set.ImportImage(img2, i->category);
254 if(set_geometry.width == 0 || set_geometry.height == 0)
256 std::cerr <<
"Error: Invalid geometry for random sample extraction. Check -r <w> <h> option." << std::endl;
260 std::cout <<
"Extract random from " << item.
filename <<
" ID:" <<
frameidx << std::endl;
261 std::vector<datasetobject> list = item.
object;
268 if(img.
width > 2 * set_geometry.width && img.height > 2 * set_geometry.height)
274 for(std::vector<datasetobject>::iterator i = list.begin(); i != list.end(); ++i)
278 i->roi.x1 = (i->roi.x1+1)/2;
279 i->roi.y1 = (i->roi.y1+1)/2;
DataSetHandle< Aggregator > DataSetType
the dataset (contains DataType plus additional data)
Definition: SourceDataSet.h:50
int frameidx
a counter, used for debug
bool LoadSet(DataSetType &set, datasetin &in, const size &sz, int nRand)
some parameters used in LoadSet
Definition: SourceDataSet.h:186
bool RandomExtract(DataSetType &set, const ImageHandle &img, const size &set_geometry, int nRand, const std::vector< datasetobject > &object)
Definition: SourceDataSet.h:138
bool pnm_load(const char *file, Image &out)
unsigned int bpp
byte per pixel (1 for grayscale image, 2 for 16bit greyscale image, 3 for RGB/YUV/etc color image) ...
Definition: Image.h:37
a virtual pure pattern list reader
Definition: datasetin.h:56
a structure to hold image data (memory)
Definition: Image.h:74
ImageHandle crop(int x0, int y0, int x1, int y1) const
return a subpart of the image (without copy)
Definition: Image.h:47
void Downsample2X(ImageHandle &out, const ImageHandle &in)
Downsample (a factor of 2) the whole image.
bool pnm_write(const ImageHandle &in, const char *file)
bool ImportImage(const ImageHandle &image, int category)
Import an Image and transform it using "Preprocessor".
Definition: SourceDataSet.h:84
void SetExportFolder(const std::string &folder)
Set the folder where store pattern generated in the dataset.
Definition: SourceDataSet.h:72
image/size TODO namespace
Definition: Types.h:39
void Resample(ImageHandle &out, const ImageHandle &in, const rect &area)
Crop and Resample an Image using IntegralImage algorithm.
Definition: SourceDataSet.h:46
long stride
line stride, the delta offset, in bytes, between two different scanline
Definition: Image.h:41
an item used for positive
Definition: datasetin.h:44
unsigned int width
image geometry
Definition: Image.h:39
std::vector< datasetobject > object
List of object.
Definition: datasetin.h:52
bool overlap(const rect &a, const rect &b)
test if 2 rect are overlapped
a rectangle structure
Definition: Types.h:55
virtual class to import images
std::string filename
filename
Definition: datasetin.h:46
unsigned char * data
initial address of the first pixel. It must be cast to correct format (uint8, uint16, rgb, etc etc)
Definition: Image.h:43
bool auto_negative
if the rest of the blob can be used as source for negative
Definition: datasetin.h:50
void alloc(unsigned int w, unsigned int h, unsigned int b)
reserve memory for the image
Definition: Image.h:98