1 #ifndef _IMAGESET_UTILS_H
2 #define _IMAGESET_UTILS_H
9 template<
class Operator>
10 bool RandomProcess(
const std::string & filename, Operator & op,
const ImageHandle & img,
const size & set_geometry,
int nRand,
const std::vector<datasetobject> &
object)
13 for(
int k=0; k<nRand; ++k)
15 int x0 = rand() % (img.
width - set_geometry.width);
16 int y0 = rand() % (img.height - set_geometry.height);
17 rect roi(x0, y0, x0 + set_geometry.width, y0 + set_geometry.height);
19 bool collision =
false;
21 for(std::vector<datasetobject>::const_iterator i =
object.begin(); i !=
object.end(); ++i)
30 if(!op(filename, img.
crop(roi), -1))
32 std::cerr <<
"Failed to import pattern";
40 template<
class Operator>
41 bool ProcessSet(
datasetin & in,
const size & sz,
int nRand, Operator & op)
44 size set_geometry = sz;
46 if(set_geometry.width != 0 || set_geometry.height!=0)
47 std::cout <<
"ProcessSet with geometry " << set_geometry.width <<
'x' << set_geometry.height << std::endl;
56 std::cout <<
"Warn: \'" << item.
filename <<
"' failed to load: skip." << std::endl;
61 for(std::vector<datasetobject>::const_iterator i=item.
object.begin(); i!=item.
object.end(); ++i)
67 if(roi.x0 == 0 && roi.y0 == 0 && roi.x1 == 0 && roi.y1 == 0)
74 if((roi.x0 < 0) || (roi.y0 < 0) || (roi.x1 > (
int) img.
width) || (roi.y1 > (
int) img.height) || (roi.x0 >= roi.x1) || (roi.y0 >= roi.y1))
76 std::cerr <<
"Warn: " << item.
filename <<
" has a ROI out of bound: roi=" << roi <<
" | image=" << img.
width <<
'x' << img.height << std::endl;
81 if(set_geometry.width == 0 && set_geometry.height==0)
83 set_geometry.width = roi.width();
84 set_geometry.height = roi.height();
85 std::cout <<
"Geometry: " << set_geometry.width <<
'x' << set_geometry.height << std::endl;
88 if(set_geometry == roi.extension())
92 std::cerr <<
"Failed to import image " << item.
filename << std::endl;
101 img2.
alloc(set_geometry.width, set_geometry.height, 1);
103 op(item.
filename, img2, i->category);
113 if(set_geometry.width == 0 || set_geometry.height == 0)
114 std::cerr <<
"Invalid geometry for random sample extraction" << std::endl;
116 std::cout <<
"Extract random from " << item.
filename << std::endl;
117 std::vector<datasetobject> list = item.
object;
122 RandomProcess(item.
filename, op, img, set_geometry, nRand, list);
125 if(img.
width > 2 * set_geometry.width && img.height > 2 * set_geometry.height)
134 for(std::vector<datasetobject>::iterator i = list.begin(); i != list.end(); ++i)
138 i->roi.x1 = (i->roi.x1+1)/2;
139 i->roi.y1 = (i->roi.y1+1)/2;
bool pnm_load(const char *file, Image &out)
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.
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.
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
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