30 #include <fvutils/colormap/bayes/bayes_histos_to_lut.h> 31 #include <fvutils/statistical/histogram.h> 32 #include <fvutils/colormap/yuvcm.h> 33 #include <fvutils/colormap/cmfile.h> 34 #include <core/exception.h> 36 #include <fvutils/color/color_object_map.h> 65 BayesHistosToLut::BayesHistosToLut(std::map<hint_t, Histogram*> &histos,
66 unsigned int d, hint_t fg_object,
unsigned int w,
unsigned int h)
67 : histograms(histos), fg_object(fg_object)
76 min_probability = 0.3;
79 min_prob_yellow = 0.0;
97 return string(
"BayesHistosToLut");
109 if (fg_object == H_BALL) {
146 if ( object_probabilities.find(
object) != object_probabilities.end() ) {
147 return object_probabilities[object];
149 cout <<
"returning 0" << endl;
167 unsigned int sum = 0;
168 for (
unsigned int y = 0; y < depth; ++y) {
169 sum += histograms[object]->get_value(u, v, y);
172 return (
float(sum) /
float(numberOfOccurrences[
object]) );
189 return (
float(histograms[
object]->get_value(u, v, y)) /
float(numberOfOccurrences[
object]) );
207 float sumOfProbabilities = 0.0;
208 map<hint_t, Histogram*>::iterator hit;
209 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
213 if (sumOfProbabilities != 0) {
237 float sumOfProbabilities = 0.0;
238 map<hint_t, Histogram*>::iterator hit;
239 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
243 if (sumOfProbabilities != 0) {
261 hint_t mostLikelyObject = H_UNKNOWN;
262 float probOfMostLikelyObject = 0.0;
263 map<hint_t, Histogram*>::iterator hit;
264 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
267 if (tmp > probOfMostLikelyObject) {
268 probOfMostLikelyObject = tmp;
269 mostLikelyObject = (hint_t)hit->first;
273 if (probOfMostLikelyObject > min_probability) {
274 return mostLikelyObject;
292 hint_t mostLikelyObject = H_UNKNOWN;
293 float probOfMostLikelyObject = 0.0;
294 map<hint_t, Histogram*>::iterator hit;
295 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
298 if (tmp > probOfMostLikelyObject) {
299 probOfMostLikelyObject = tmp;
300 mostLikelyObject = (hint_t)hit->first;
304 if (probOfMostLikelyObject > min_probability) {
305 return mostLikelyObject;
318 map<hint_t, Histogram*>::iterator hit;
319 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
320 unsigned int total = 0;
321 for (
unsigned int v = 0; v < height; ++v) {
322 for (
unsigned int u = 0; u < width; ++u) {
323 for (
unsigned int y = 0; y < depth; ++y) {
324 unsigned int tmp = ((
Histogram*)(hit->second))->get_value(u, v, y);
330 numberOfOccurrences[ (hint_t)hit->first ] = total;
341 hint_t color_with_highest_prob;
344 for (
unsigned int y = 0; y < depth; ++y) {
346 for (
unsigned int v = 0; v < height; ++v) {
347 for (
unsigned int u = 0; u < width; ++u) {
351 color_with_highest_prob = H_UNKNOWN;
352 map<hint_t, Histogram*>::iterator hit;
353 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
355 if (numberOfOccurrences[ (hint_t)hit->first ] > 0) {
356 current_prob = float( hit->second->get_value(u, v, y) ) /
float( numberOfOccurrences[ hit->first ] );
359 if ( current_prob > highest_prob &&
360 current_prob > min_probability ) {
362 highest_prob = current_prob;
363 color_with_highest_prob = hit->first;
384 unsigned int old_undo = 0;
388 Histogram *histo_fg = histograms[fg_object];
389 Histogram *histo_bg = histograms[H_BACKGROUND];
393 cout <<
"Histogram::calculateLutValues: There are not enough undos possible for background histogram, not penalizing" << endl;
395 unsigned int bg_median = histo_bg->
get_median();
397 unsigned int bg_val = 0;
401 cout <<
"Histogram: Setting low bg vals to median. median=" << bg_median
402 <<
" avg=" << bg_average << endl;
404 for (
unsigned int v = 0; v < height; ++v) {
405 for (
unsigned int u = 0; u < width; ++u) {
406 for (
unsigned int y = 0; y < depth; ++y) {
408 if ( histo_fg->
get_value(u, v, y) == 0 ) {
410 if (bg_val < bg_average) {
411 histo_bg->
set_value(u, v, y, bg_average);
424 map<hint_t, Histogram*>::iterator hit;
425 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
426 unsigned int total = 0;
427 for (
unsigned int y = 0; y < depth; ++y) {
428 for (
unsigned int v = 0; v < height; ++v) {
429 for (
unsigned int u = 0; u < width; ++u) {
430 unsigned int tmp = hit->second->get_value(u, v, y);
436 numberOfOccurrences[hit->first] = total;
437 cout <<
"[" << hit->first <<
"]: " << numberOfOccurrences[hit->first] <<
" occurences" << endl;
440 unsigned int total_count = 0;
441 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
442 total_count += hit->second->get_sum();
447 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
448 object_probabilities[hit->first] = (float)hit->second->get_sum() / (float)total_count;
455 unsigned int count_ball = 0;
456 unsigned int count_field = 0;
457 unsigned int count_line = 0;
458 unsigned int count_robot = 0;
459 unsigned int count_background = 0;
460 unsigned int count_goal = 0;
461 unsigned int count_unknown = 0;
465 for (
unsigned int y = 0; y < depth; ++y) {
467 for (
unsigned int u = 0; u < width; ++u) {
469 for (
unsigned int v = 0; v < height; ++v) {
473 switch(mostLikelyObject) {
498 cout <<
"(BayesHistosToLut::calculateLutValues(): Invalid object." << endl;
499 throw fawkes::Exception(
"BayesHistosToLut::calculateLutValues(): Invalid object.");
506 printf(
"d/w/h: %u/%u/%u ball: %d field: %d line: %d robot: %d goal: %d background: %d unknown: %d\n",
507 depth, width, height, count_ball, count_field, count_line,
508 count_robot, count_goal, count_background, count_unknown);
511 Histogram *histo_bg = histograms[H_BACKGROUND];
551 cmf.
write(filename.c_str());
561 min_probability = min_prob;
573 min_prob_ball = min_prob;
576 min_prob_green = min_prob;
579 min_prob_yellow = min_prob;
582 min_prob_blue = min_prob;
585 min_prob_white = min_prob;
588 min_prob_black = min_prob;
unsigned int get_average()
Get average of all values.
virtual unsigned int deepness() const
Get deepness of colormap.
void saveLut(char *file)
Save LUT to file.
void add_colormap(Colormap *colormap)
Add colormap.
float getAPosterioriProb(hint_t object, unsigned int u, unsigned int v)
P(object| u, v).
static const ColorObjectMap & get_instance()
ColorObjectMap getter.
void setMinProbability(float min_prob)
Set min probability.
virtual unsigned int width() const
Get width of colormap.
unsigned int get_median()
Get median of all values.
unsigned int get_num_undos()
Get number of undos.
std::string getName()
Get name.
void setMinProbForColor(float min_prob, hint_t hint)
Set min probability for color.
virtual void write(const char *file_name)
Write file.
Base class for exceptions in Fawkes.
unsigned int get_value(unsigned int x, unsigned int y)
Get value from histogram.
float getAPrioriProb(unsigned int u, unsigned int v, hint_t object)
P(u, v| object).
void set_value(unsigned int x, unsigned int y, unsigned int value)
Set value in histogram.
virtual void set(unsigned int y, unsigned int u, unsigned int v, color_t c)
Set color class for given YUV value.
void calculateLutValues(bool penalty=false)
Calculate LUT values.
void save(std::string filename)
Save LUT to file.
float getObjectProb(hint_t object)
Get object probability.
virtual unsigned int depth() const
Get depth of colormap.
void calculateLutAllColors()
Calculate all LUT colors.
YuvColormap * get_colormap()
Get generated color model.
hint_t getMostLikelyObject(unsigned int u, unsigned int v)
Get most likely object.
virtual void reset()
Reset colormap.
unsigned int switch_undo(unsigned int undo_id)
Switch undo to another undo buffer.
~BayesHistosToLut()
Destructor.
virtual unsigned int height() const
Get height of colormap.