25 #include <core/exception.h>
26 #include <fvutils/color/yuv.h>
27 #include <fvutils/colormap/bayes/bayes_generator.h>
28 #include <fvutils/colormap/bayes/bayes_histos_to_lut.h>
29 #include <fvutils/colormap/yuvcm.h>
30 #include <fvutils/statistical/histogram.h>
31 #include <fvutils/statistical/histogram_block.h>
32 #include <fvutils/statistical/histogram_file.h>
39 namespace firevision {
53 BayesColormapGenerator::BayesColormapGenerator(
unsigned int lut_depth,
55 unsigned int lut_width,
56 unsigned int lut_height)
58 this->lut_width = lut_width;
59 this->lut_height = lut_height;
60 this->lut_depth = lut_depth;
62 set_fg_object(fg_object);
68 image_width = image_height = 0;
71 bhtl =
new BayesHistosToLut(histos, lut_depth, fg_object, lut_width, lut_height);
72 cm = bhtl->get_colormap();
76 BayesColormapGenerator::~BayesColormapGenerator()
78 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
79 delete histo_it->second;
82 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
83 delete histo_it->second;
86 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
87 delete histo_it->second;
90 delete[] selection_mask;
97 BayesColormapGenerator::set_fg_object(hint_t
object)
99 if (H_UNKNOWN ==
object) {
103 if (fg_histos.find(
object) == fg_histos.end()) {
104 fg_histos[object] =
new Histogram(lut_width, lut_height, lut_depth);
105 bg_histos[object] =
new Histogram(lut_width, lut_height, lut_depth, 2);
106 histos[object] =
new Histogram(lut_width, lut_height, lut_depth);
118 BayesColormapGenerator::set_buffer(
unsigned char *buffer,
unsigned int width,
unsigned int height)
120 this->buffer = buffer;
122 image_height = height;
124 selection_mask =
new bool[image_width * image_height];
126 for (
unsigned int i = 0; i < image_width * image_height; ++i) {
127 selection_mask[i] =
false;
130 norm_size = image_width * image_height;
137 BayesColormapGenerator::get_current()
148 BayesColormapGenerator::is_in_region(
unsigned int x,
unsigned int y)
150 return selection_mask[image_width * y + x];
157 BayesColormapGenerator::set_selection(vector<rectangle_t> region)
159 this->region = region;
161 for (
unsigned int i = 0; i < image_width * image_height; ++i) {
162 selection_mask[i] =
false;
165 vector<rectangle_t>::iterator it;
168 for (it = region.begin(); it != region.end(); it++) {
169 for (
unsigned int w = 0; w < (*it).extent.w; ++w) {
170 for (
unsigned int h = 0; h < (*it).extent.h; ++h) {
171 unsigned int x = (*it).start.x + w;
172 unsigned int y = (*it).start.y + h;
174 selection_mask[image_width * y + x] =
true;
185 BayesColormapGenerator::set_min_probability(
float min_prob)
187 bhtl->setMinProbability(min_prob);
192 BayesColormapGenerator::consider()
194 if (region.size() == 0) {
195 cout <<
"Region empty, cannot consider" << endl;
199 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
200 (*histo_it).second->reset_undo();
203 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
204 (*histo_it).second->reset_undo();
211 for (
unsigned int w = 0; w < image_width; ++w) {
212 for (
unsigned int h = 0; h < image_height; ++h) {
213 y = YUV422_PLANAR_Y_AT(buffer, image_width, w, h);
214 u = YUV422_PLANAR_U_AT(buffer, image_width, image_height, w, h);
215 v = YUV422_PLANAR_V_AT(buffer, image_width, image_height, w, h);
217 unsigned int y_index = (
unsigned int)(y / 256.0f *
float(lut_depth));
218 unsigned int u_index = (
unsigned int)(u / 256.0f *
float(lut_width));
219 unsigned int v_index = (
unsigned int)(v / 256.0f *
float(lut_height));
221 if (is_in_region(w, h)) {
222 fg_histos[fg_object]->inc_value(u_index, v_index, y_index);
224 bg_histos[fg_object]->inc_value(u_index, v_index, y_index);
227 cout <<
"." << flush;
234 BayesColormapGenerator::calc()
237 bhtl->calculateLutValues(
false );
242 BayesColormapGenerator::undo()
244 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
245 (*histo_it).second->undo();
248 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
249 (*histo_it).second->undo();
252 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
253 (*histo_it).second->undo();
259 BayesColormapGenerator::reset()
261 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
262 (*histo_it).second->reset();
265 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
266 (*histo_it).second->reset();
269 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
270 (*histo_it).second->reset();
275 for (
unsigned int i = 0; i < image_width * image_height; ++i) {
276 selection_mask[i] =
false;
282 BayesColormapGenerator::reset_undo()
284 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
285 (*histo_it).second->reset_undo();
288 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
289 (*histo_it).second->reset_undo();
292 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
293 (*histo_it).second->reset_undo();
301 BayesColormapGenerator::has_histograms()
309 std::map<hint_t, Histogram *> *
310 BayesColormapGenerator::get_histograms()
319 BayesColormapGenerator::load_histograms(
const char *filename)
323 histogram_file.
read(filename);
326 HistogramFile::HistogramBlockList::iterator lit;
328 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
329 delete histo_it->second;
331 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
332 delete histo_it->second;
334 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
335 delete histo_it->second;
343 for (lit = histogram_list.begin(); lit != histogram_list.end(); ++lit) {
344 if ((*lit)->object_type() == H_BACKGROUND) {
345 bg_histogram_block = *lit;
346 lut_width = bg_histogram_block->
width();
347 lut_height = bg_histogram_block->
height();
348 lut_depth = bg_histogram_block->
depth();
354 if (!bg_histogram_block) {
355 throw fawkes::Exception(
"Histograms file does not contain a background histogram");
360 for (lit = histogram_list.begin(); lit != histogram_list.end(); ++lit) {
361 hint_t cur_object = (*lit)->object_type();
363 if (cur_object == H_BACKGROUND) {
367 fg_histos[cur_object] =
new Histogram(*lit);
368 bg_histos[cur_object] =
new Histogram(bg_histogram_block);
370 norm_size += fg_histos[cur_object]->get_sum();
373 norm_size += bg_histos.begin()->second->get_sum();
376 HistogramMap::iterator hit;
377 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
378 hint_t cur_object = histo_it->first;
380 for (hit = fg_histos.begin(); hit != fg_histos.end(); ++hit) {
381 if (cur_object == hit->first) {
385 for (
unsigned int x = 0; x < lut_width; ++x) {
386 for (
unsigned int y = 0; y < lut_height; ++y) {
387 for (
unsigned int z = 0; z < lut_depth; ++z) {
388 unsigned int val = hit->second->get_value(x, y, z);
389 histo_it->second->add(x, y, z, val);
397 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
398 hint_t cur_object = histo_it->first;
400 (norm_size - fg_histos[cur_object]->get_sum()) /
float(histo_it->second->get_sum());
406 for (
unsigned int x = 0; x < lut_width; ++x) {
407 for (
unsigned int y = 0; y < lut_height; ++y) {
408 for (
unsigned int z = 0; z < lut_depth; ++z) {
409 unsigned int cur_val = histo_it->second->get_value(x, y, z);
410 unsigned int new_val = (
unsigned int)rint(factor * cur_val);
411 histo_it->second->set_value(x, y, z, new_val);
418 bhtl =
new BayesHistosToLut(histos, lut_depth, H_UNKNOWN, lut_width, lut_height);
419 cm = bhtl->get_colormap();
429 BayesColormapGenerator::save_histograms(
const char *filename)
437 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
438 histogram_block = histo_it->second->get_histogram_block();
443 histogram_file.
write(filename);
448 BayesColormapGenerator::normalize_histos()
450 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
451 delete histo_it->second;
455 unsigned int fg_size = 0;
460 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
461 hint_t cur_object = histo_it->first;
463 if (bg_histos.find(cur_object) == bg_histos.end()) {
467 Histogram *fg = fg_histos[cur_object];
468 Histogram *bg = bg_histos[cur_object];
470 unsigned int fg_sum = fg->get_sum();
471 unsigned int bg_sum = bg->get_sum();
473 if ((fg_sum + bg_sum) == 0) {
477 Histogram *h =
new Histogram(lut_width, lut_height, lut_depth);
478 histos[cur_object] = h;
480 norm_factor = norm_size / float(fg_sum + bg_sum);
482 for (
unsigned int x = 0; x < lut_width; ++x) {
483 for (
unsigned int y = 0; y < lut_height; ++y) {
484 for (
unsigned int z = 0; z < lut_depth; ++z) {
485 hval = (
unsigned int)rint(
float(fg->get_value(x, y, z)) * norm_factor);
486 h->set_value(x, y, z, hval);
491 fg_size += h->get_sum();
495 Histogram *bh =
new Histogram(lut_width, lut_height, lut_depth);
496 histos[H_BACKGROUND] = bh;
497 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
498 hint_t cur_object = histo_it->first;
500 Histogram *fg = fg_histos[cur_object];
501 Histogram *bg = bg_histos[cur_object];
503 unsigned int fg_sum = fg->get_sum();
504 unsigned int bg_sum = bg->get_sum();
506 if ((fg_sum + bg_sum) == 0) {
510 norm_factor = norm_size / float(fg_sum + bg_sum);
512 for (
unsigned int x = 0; x < lut_width; ++x) {
513 for (
unsigned int y = 0; y < lut_height; ++y) {
514 for (
unsigned int z = 0; z < lut_depth; ++z) {
516 hval = (
unsigned int)rint(
float(bg->get_value(x, y, z)) * norm_factor);
517 bh->add(x, y, z, hval);
520 std::map<hint_t, Histogram *>::iterator hit;
521 for (hit = histos.begin(); hit != histos.end(); ++hit) {
522 if (hit->first == cur_object || hit->first == H_BACKGROUND) {
526 hval = hit->second->get_value(x, y, z);
527 bh->sub(x, y, z, hval);
535 norm_factor = (norm_size - fg_size) /
float(bh->get_sum());
537 for (
unsigned int x = 0; x < lut_width; ++x) {
538 for (
unsigned int y = 0; y < lut_height; ++y) {
539 for (
unsigned int z = 0; z < lut_depth; ++z) {
540 hval = (
unsigned int)rint(
float(bh->get_value(x, y, z)) * norm_factor);
541 bh->set_value(x, y, z, hval);
Base class for exceptions in Fawkes.
LUT generation by using Bayesian method on histograms.
virtual void read(const char *file_name)
Read file.
virtual void write(const char *file_name)
Write file.
void set_owns_blocks(bool owns_blocks)
Lets the file take over the ownership and give up the ownership of the blocks, respectively.
This class defines a file block for histograms.
uint16_t height() const
Returns the the height of the histogram.
uint16_t depth() const
Returns the the depth of the histogram.
uint16_t width() const
Returns the the width of the histogram.
void set_object_type(hint_t object_type)
Set the type of the object the histogram is associated with.
A fileformat for histograms.
HistogramBlockList histogram_blocks()
Generates a list of histogram blocks attached to the file.
void add_histogram_block(HistogramBlock *block)
Adds a new histogram block to the file.
std::list< HistogramBlock * > HistogramBlockList
Convenience typdef for a STL list of pointers to histogram blocks.
Fawkes library namespace.