25 #include <fvutils/colormap/bayes/bayes_generator.h>
26 #include <fvutils/statistical/histogram_file.h>
27 #include <fvutils/statistical/histogram_block.h>
29 #include <fvutils/color/yuv.h>
30 #include <fvutils/statistical/histogram.h>
31 #include <fvutils/colormap/yuvcm.h>
32 #include <fvutils/colormap/bayes/bayes_histos_to_lut.h>
33 #include <core/exception.h>
57 BayesColormapGenerator::BayesColormapGenerator(
unsigned int lut_depth, hint_t fg_object,
unsigned int lut_width,
unsigned int lut_height)
59 this->lut_width = lut_width;
60 this->lut_height = lut_height;
61 this->lut_depth = lut_depth;
63 set_fg_object(fg_object);
69 image_width = image_height = 0;
72 bhtl =
new BayesHistosToLut(histos, lut_depth, fg_object, lut_width, lut_height);
73 cm = bhtl->get_colormap();
78 BayesColormapGenerator::~BayesColormapGenerator()
80 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
81 delete histo_it->second;
84 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
85 delete histo_it->second;
88 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
89 delete histo_it->second;
92 delete[] selection_mask;
100 BayesColormapGenerator::set_fg_object(hint_t
object)
102 if (H_UNKNOWN ==
object)
105 if ( fg_histos.find(
object) == fg_histos.end() ) {
106 fg_histos[object] =
new Histogram(lut_width, lut_height, lut_depth);
107 bg_histos[object] =
new Histogram(lut_width, lut_height, lut_depth, 2);
108 histos[object] =
new Histogram(lut_width, lut_height, lut_depth);
121 BayesColormapGenerator::set_buffer(
unsigned char *buffer,
122 unsigned int width,
unsigned int height)
124 this->buffer = buffer;
126 image_height = height;
128 selection_mask =
new bool[image_width * image_height];
130 for (
unsigned int i = 0; i < image_width * image_height; ++i) {
131 selection_mask[i] =
false;
134 norm_size = image_width * image_height;
142 BayesColormapGenerator::get_current()
154 BayesColormapGenerator::is_in_region(
unsigned int x,
unsigned int y)
156 return selection_mask[image_width * y + x];
164 BayesColormapGenerator::set_selection(vector< rectangle_t > region)
166 this->region = region;
168 for (
unsigned int i = 0; i < image_width * image_height; ++i) {
169 selection_mask[i] =
false;
172 vector<rectangle_t>::iterator it;
175 for (it = region.begin(); it != region.end(); it++) {
176 for (
unsigned int w = 0; w < (*it).extent.w; ++w) {
177 for (
unsigned int h = 0; h < (*it).extent.h; ++h) {
178 unsigned int x = (*it).start.x + w;
179 unsigned int y = (*it).start.y + h;
181 selection_mask[image_width * y + x] =
true;
193 BayesColormapGenerator::set_min_probability(
float min_prob)
195 bhtl->setMinProbability( min_prob );
201 BayesColormapGenerator::consider()
204 if (region.size() == 0) {
205 cout <<
"Region empty, cannot consider" << endl;
209 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
210 (*histo_it).second->reset_undo();
213 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
214 (*histo_it).second->reset_undo();
221 for (
unsigned int w = 0; w < image_width; ++w) {
222 for (
unsigned int h = 0; h < image_height; ++h) {
224 y = YUV422_PLANAR_Y_AT(buffer, image_width, w, h);
225 u = YUV422_PLANAR_U_AT(buffer, image_width, image_height, w, h);
226 v = YUV422_PLANAR_V_AT(buffer, image_width, image_height, w, h);
228 unsigned int y_index = (
unsigned int)( y / 256.0f *
float(lut_depth) );
229 unsigned int u_index = (
unsigned int)( u / 256.0f *
float(lut_width) );
230 unsigned int v_index = (
unsigned int)( v / 256.0f *
float(lut_height) );
232 if ( is_in_region(w, h) ) {
233 fg_histos[fg_object]->inc_value(u_index, v_index, y_index );
235 bg_histos[fg_object]->inc_value(u_index, v_index, y_index );
238 cout <<
"." << flush;
246 BayesColormapGenerator::calc()
249 bhtl->calculateLutValues(
false );
255 BayesColormapGenerator::undo()
257 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
258 (*histo_it).second->undo();
261 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
262 (*histo_it).second->undo();
265 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
266 (*histo_it).second->undo();
273 BayesColormapGenerator::reset()
275 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
276 (*histo_it).second->reset();
279 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
280 (*histo_it).second->reset();
283 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
284 (*histo_it).second->reset();
289 for (
unsigned int i = 0; i < image_width * image_height; ++i) {
290 selection_mask[i] =
false;
297 BayesColormapGenerator::reset_undo()
299 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
300 (*histo_it).second->reset_undo();
303 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
304 (*histo_it).second->reset_undo();
307 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
308 (*histo_it).second->reset_undo();
317 BayesColormapGenerator::has_histograms()
326 std::map< hint_t, Histogram * > *
327 BayesColormapGenerator::get_histograms()
337 BayesColormapGenerator::load_histograms(
const char *filename)
341 histogram_file.
read(filename);
344 HistogramFile::HistogramBlockList::iterator lit;
346 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it) {
347 delete histo_it->second;
349 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
350 delete histo_it->second;
352 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
353 delete histo_it->second;
361 for (lit = histogram_list.begin(); lit != histogram_list.end(); ++lit)
363 if ( (*lit)->object_type() == H_BACKGROUND )
365 bg_histogram_block = *lit;
366 lut_width = bg_histogram_block->
width();
367 lut_height = bg_histogram_block->
height();
368 lut_depth = bg_histogram_block->
depth();
374 if ( !bg_histogram_block )
376 throw fawkes::Exception(
"Histograms file does not contain a background histogram");
381 for (lit = histogram_list.begin(); lit != histogram_list.end(); ++lit)
383 hint_t cur_object = (*lit)->object_type();
385 if (cur_object == H_BACKGROUND)
388 fg_histos[cur_object] =
new Histogram(*lit);
389 bg_histos[cur_object] =
new Histogram(bg_histogram_block);
391 norm_size += fg_histos[cur_object]->get_sum();
394 norm_size += bg_histos.begin()->second->get_sum();
397 HistogramMap::iterator hit;
398 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
399 hint_t cur_object = histo_it->first;
401 for (hit = fg_histos.begin(); hit != fg_histos.end(); ++hit) {
402 if (cur_object == hit->first)
405 for (
unsigned int x = 0; x < lut_width; ++x) {
406 for (
unsigned int y = 0; y < lut_height; ++y) {
407 for (
unsigned int z = 0; z < lut_depth; ++z) {
408 unsigned int val = hit->second->get_value(x, y, z);
409 histo_it->second->add(x, y, z, val);
417 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++histo_it) {
418 hint_t cur_object = histo_it->first;
419 float factor = ( norm_size - fg_histos[cur_object]->get_sum() ) /
float( histo_it->second->get_sum() );
424 for (
unsigned int x = 0; x < lut_width; ++x) {
425 for (
unsigned int y = 0; y < lut_height; ++y) {
426 for (
unsigned int z = 0; z < lut_depth; ++z) {
427 unsigned int cur_val = histo_it->second->get_value(x, y, z);
428 unsigned int new_val = (
unsigned int) rint(factor * cur_val);
429 histo_it->second->set_value(x, y, z, new_val);
436 bhtl =
new BayesHistosToLut(histos, lut_depth, H_UNKNOWN, lut_width, lut_height);
437 cm = bhtl->get_colormap();
448 BayesColormapGenerator::save_histograms(
const char *filename)
456 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it)
458 histogram_block = histo_it->second->get_histogram_block();
463 histogram_file.
write(filename);
469 BayesColormapGenerator::normalize_histos()
471 for (histo_it = histos.begin(); histo_it != histos.end(); ++histo_it) {
472 delete histo_it->second;
476 unsigned int fg_size = 0;
481 for (histo_it = fg_histos.begin(); histo_it != fg_histos.end(); ++histo_it)
483 hint_t cur_object = histo_it->first;
485 if ( bg_histos.find(cur_object) == bg_histos.end() ) {
489 Histogram *fg = fg_histos[cur_object];
490 Histogram *bg = bg_histos[cur_object];
492 unsigned int fg_sum = fg->get_sum();
493 unsigned int bg_sum = bg->get_sum();
495 if ( (fg_sum + bg_sum) == 0 )
498 Histogram *h =
new Histogram(lut_width, lut_height, lut_depth);
499 histos[cur_object] = h;
501 norm_factor = norm_size / float(fg_sum + bg_sum);
503 for (
unsigned int x = 0; x < lut_width; ++x) {
504 for (
unsigned int y = 0; y < lut_height; ++y) {
505 for (
unsigned int z = 0; z < lut_depth; ++z) {
506 hval = (
unsigned int) rint(
float(fg->get_value(x, y, z)) * norm_factor);
507 h->set_value(x, y, z, hval);
512 fg_size += h->get_sum();
516 Histogram *bh =
new Histogram(lut_width, lut_height, lut_depth);
517 histos[H_BACKGROUND] = bh;
518 for (histo_it = bg_histos.begin(); histo_it != bg_histos.end(); ++ histo_it)
520 hint_t cur_object = histo_it->first;
522 Histogram *fg = fg_histos[cur_object];
523 Histogram *bg = bg_histos[cur_object];
525 unsigned int fg_sum = fg->get_sum();
526 unsigned int bg_sum = bg->get_sum();
528 if ( (fg_sum + bg_sum) == 0 )
531 norm_factor = norm_size / float(fg_sum + bg_sum);
533 for (
unsigned int x = 0; x < lut_width; ++x) {
534 for (
unsigned int y = 0; y < lut_height; ++y) {
535 for (
unsigned int z = 0; z < lut_depth; ++z) {
537 hval = (
unsigned int) rint(
float(bg->get_value(x, y, z)) * norm_factor);
538 bh->add(x, y, z, hval);
541 std::map< hint_t, Histogram * >::iterator hit;
542 for (hit = histos.begin(); hit != histos.end(); ++hit) {
543 if (hit->first == cur_object || hit->first == H_BACKGROUND)
546 hval = hit->second->get_value(x, y, z);
547 bh->sub(x, y, z, hval);
555 norm_factor = (norm_size - fg_size) /
float( bh->get_sum() );
557 for (
unsigned int x = 0; x < lut_width; ++x) {
558 for (
unsigned int y = 0; y < lut_height; ++y) {
559 for (
unsigned int z = 0; z < lut_depth; ++z) {
560 hval = (
unsigned int) rint(
float(bh->get_value(x, y, z)) * norm_factor );
561 bh->set_value(x, y, z, hval);
void set_object_type(hint_t object_type)
Set the type of the object the histogram is associated with.
LUT generation by using Bayesian method on histograms.
HistogramBlockList histogram_blocks()
Generates a list of histogram blocks attached to the file.
uint16_t depth() const
Returns the the depth of the histogram.
uint16_t height() const
Returns the the height of the histogram.
Fawkes library namespace.
virtual void write(const char *file_name)
Write file.
virtual void read(const char *file_name)
Read file.
Base class for exceptions in Fawkes.
uint16_t width() const
Returns the the width of the histogram.
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.
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.
A fileformat for histograms.