X-Boost  2.3.8
ImageSet.h
1 #ifndef _IMAGESET_UTILS_H
2 #define _IMAGESET_UTILS_H
3 
4 #include "IO/datasetin.h"
5 #include "Utility/ImageUtils.h"
6 
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)
11 {
12  // for any candidate in the set
13  for(int k=0; k<nRand; ++k)
14  {
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);
18 
19  bool collision = false;
20  // Check Collision
21  for(std::vector<datasetobject>::const_iterator i = object.begin(); i != object.end(); ++i)
22  if(overlap(roi, i->roi))
23  {
24  collision = true;
25  break;
26  }
27 
28  if(!collision)
29  {
30  if(!op(filename, img.crop(roi), -1)) // only negative
31  {
32  std::cerr << "Failed to import pattern";
33  return false;
34  }
35  }
36  }
37  return true;
38 }
39 
40 template<class Operator>
41 bool ProcessSet(datasetin & in, const size & sz, int nRand, Operator & op)
42 {
43  datasetitem item;
44  size set_geometry = sz; //
45 
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;
48 
49  // iterate on input data
50  while(in.next(item))
51  {
52 // bool have_negative = false;
53  Image img;
54  if(!pnm_load(item.filename.c_str(), img))
55  {
56  std::cout << "Warn: \'" << item.filename << "' failed to load: skip." << std::endl;
57  }
58  else
59  {
60  // for any candidate in the set (positive/negative)
61  for(std::vector<datasetobject>::const_iterator i=item.object.begin(); i!=item.object.end(); ++i)
62  if(i->category != 0) // DO NOT CARE
63  {
64  rect roi = i->roi;
65 
66  // whole image ROI
67  if(roi.x0 == 0 && roi.y0 == 0 && roi.x1 == 0 && roi.y1 == 0)
68  {
69  roi.x1 = img.width;
70  roi.y1 = img.height;
71  }
72 
73  // CHECK ROI
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))
75  {
76  std::cerr << "Warn: " << item.filename << " has a ROI out of bound: roi=" << roi << " | image=" << img.width << 'x' << img.height << std::endl;
77  }
78  else
79  {
80 
81  if(set_geometry.width == 0 && set_geometry.height==0)
82  {
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;
86  }
87 
88  if(set_geometry == roi.extension()) // se sono uguali viene fatto un Crop
89  {
90 
91  if(!op(item.filename, img.crop(roi), i->category))
92  std::cerr << "Failed to import image " << item.filename << std::endl;
93  }
94  else
95  {
96  // resize
97  Image img2;
98 
99  // TODO: avoid upsampling?
100 
101  img2.alloc(set_geometry.width, set_geometry.height, 1);
102  Resample(img2, img.crop(roi), rect(0,0, roi.width(), roi.height()));
103  op(item.filename, img2, i->category);
104  }
105 
106  }
107 
108  }
109 
110  // se e' un file da estrazione automatica di negativi, li usa
111  if(item.auto_negative && (nRand>0))
112  {
113  if(set_geometry.width == 0 || set_geometry.height == 0)
114  std::cerr << "Invalid geometry for random sample extraction" << std::endl;
115 
116  std::cout << "Extract random from " << item.filename << std::endl;
117  std::vector<datasetobject> list = item.object;
118 
119  // for all octaves:
120  for(;;) {
121 
122  RandomProcess(item.filename, op, img, set_geometry, nRand, list);
123 
124  // TODO:
125  if(img.width > 2 * set_geometry.width && img.height > 2 * set_geometry.height)
126  {
127  Image out;
128  out.alloc(img.width/2, img.height/2, 1);
129 
130  // TODO: use downsampling factor different from 2X
131  Downsample2X(out, img);
132 
133 
134  for(std::vector<datasetobject>::iterator i = list.begin(); i != list.end(); ++i)
135  {
136  i->roi.x0 /= 2;
137  i->roi.y0 /= 2;
138  i->roi.x1 = (i->roi.x1+1)/2;
139  i->roi.y1 = (i->roi.y1+1)/2;
140  }
141 
142  std::swap(img, out);
143  }
144  else
145  break;
146  }
147  }
148 
149  }
150 
151  }
152 
153  return true;
154 }
155 
156 #endif
bool pnm_load(const char *file, Image &out)
a virtual pure pattern list reader
Definition: datasetin.h:56
Definition: Image.h:35
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.
method to process 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