30#include <core/exception.h>
31#include <fvutils/color/color_object_map.h>
32#include <fvutils/colormap/bayes/bayes_histos_to_lut.h>
33#include <fvutils/colormap/cmfile.h>
34#include <fvutils/colormap/yuvcm.h>
35#include <fvutils/statistical/histogram.h>
66: histograms(histos), fg_object(fg_object)
75 min_probability = 0.3;
78 min_prob_yellow = 0.0;
96 return string(
"BayesHistosToLut");
108 if (fg_object == H_BALL) {
145 if (object_probabilities.find(
object) != object_probabilities.end()) {
146 return object_probabilities[object];
148 cout <<
"returning 0" << endl;
164 unsigned int sum = 0;
165 for (
unsigned int y = 0; y < depth; ++y) {
166 sum += histograms[object]->get_value(u, v, y);
169 return (
float(sum) /
float(numberOfOccurrences[
object]));
183 return (
float(histograms[
object]->get_value(u, v, y)) /
float(numberOfOccurrences[
object]));
199 float sumOfProbabilities = 0.0;
200 map<hint_t, Histogram *>::iterator hit;
201 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
202 sumOfProbabilities +=
206 if (sumOfProbabilities != 0) {
226 float sumOfProbabilities = 0.0;
227 map<hint_t, Histogram *>::iterator hit;
228 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
229 sumOfProbabilities +=
233 if (sumOfProbabilities != 0) {
249 hint_t mostLikelyObject = H_UNKNOWN;
250 float probOfMostLikelyObject = 0.0;
251 map<hint_t, Histogram *>::iterator hit;
252 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
255 if (tmp > probOfMostLikelyObject) {
256 probOfMostLikelyObject = tmp;
257 mostLikelyObject = (hint_t)hit->first;
261 if (probOfMostLikelyObject > min_probability) {
262 return mostLikelyObject;
277 hint_t mostLikelyObject = H_UNKNOWN;
278 float probOfMostLikelyObject = 0.0;
279 map<hint_t, Histogram *>::iterator hit;
280 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
283 if (tmp > probOfMostLikelyObject) {
284 probOfMostLikelyObject = tmp;
285 mostLikelyObject = (hint_t)hit->first;
289 if (probOfMostLikelyObject > min_probability) {
290 return mostLikelyObject;
302 map<hint_t, Histogram *>::iterator hit;
303 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
304 unsigned int total = 0;
305 for (
unsigned int v = 0; v < height; ++v) {
306 for (
unsigned int u = 0; u < width; ++u) {
307 for (
unsigned int y = 0; y < depth; ++y) {
308 unsigned int tmp = ((
Histogram *)(hit->second))->get_value(u, v, y);
314 numberOfOccurrences[(hint_t)hit->first] = total;
325 hint_t color_with_highest_prob;
328 for (
unsigned int y = 0; y < depth; ++y) {
330 for (
unsigned int v = 0; v < height; ++v) {
331 for (
unsigned int u = 0; u < width; ++u) {
334 color_with_highest_prob =
336 map<hint_t, Histogram *>::iterator hit;
337 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
339 if (numberOfOccurrences[(hint_t)hit->first] > 0) {
341 float(hit->second->get_value(u, v, y)) / float(numberOfOccurrences[hit->first]);
344 if (current_prob > highest_prob && current_prob > min_probability) {
346 highest_prob = current_prob;
347 color_with_highest_prob = hit->first;
365 unsigned int old_undo = 0;
369 Histogram *histo_fg = histograms[fg_object];
370 Histogram *histo_bg = histograms[H_BACKGROUND];
374 cout <<
"Histogram::calculateLutValues: There are not enough undos possible for background "
375 "histogram, not penalizing"
378 unsigned int bg_median = histo_bg->
get_median();
380 unsigned int bg_val = 0;
384 cout <<
"Histogram: Setting low bg vals to median. median=" << bg_median
385 <<
" avg=" << bg_average << endl;
387 for (
unsigned int v = 0; v < height; ++v) {
388 for (
unsigned int u = 0; u < width; ++u) {
389 for (
unsigned int y = 0; y < depth; ++y) {
392 if (bg_val < bg_average) {
393 histo_bg->
set_value(u, v, y, bg_average);
406 map<hint_t, Histogram *>::iterator hit;
407 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
408 unsigned int total = 0;
409 for (
unsigned int y = 0; y < depth; ++y) {
410 for (
unsigned int v = 0; v < height; ++v) {
411 for (
unsigned int u = 0; u < width; ++u) {
412 unsigned int tmp = hit->second->get_value(u, v, y);
418 numberOfOccurrences[hit->first] = total;
419 cout <<
"[" << hit->first <<
"]: " << numberOfOccurrences[hit->first] <<
" occurences" << endl;
422 unsigned int total_count = 0;
423 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
424 total_count += hit->second->get_sum();
429 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
430 object_probabilities[hit->first] = (float)hit->second->get_sum() / (float)total_count;
436 unsigned int count_ball = 0;
437 unsigned int count_field = 0;
438 unsigned int count_line = 0;
439 unsigned int count_robot = 0;
440 unsigned int count_background = 0;
441 unsigned int count_goal = 0;
442 unsigned int count_unknown = 0;
446 for (
unsigned int y = 0; y < depth; ++y) {
448 for (
unsigned int u = 0; u < width; ++u) {
450 for (
unsigned int v = 0; v < height; ++v) {
454 switch (mostLikelyObject) {
455 case H_BALL: count_ball++;
break;
456 case H_BACKGROUND: count_background++;
break;
458 case H_ROBOT_OPP: count_robot++;
break;
459 case H_FIELD: count_field++;
break;
460 case H_LINE: count_line++;
break;
462 case H_GOAL_BLUE: count_goal++;
break;
463 case H_UNKNOWN: count_unknown++;
break;
465 cout <<
"(BayesHistosToLut::calculateLutValues(): Invalid object." << endl;
466 throw fawkes::Exception(
"BayesHistosToLut::calculateLutValues(): Invalid object.");
473 printf(
"d/w/h: %u/%u/%u ball: %d field: %d line: %d robot: %d goal: %d background: %d "
487 Histogram *histo_bg = histograms[H_BACKGROUND];
526 cmf.
write(filename.c_str());
535 min_probability = min_prob;
546 case H_BALL: min_prob_ball = min_prob;
break;
547 case H_FIELD: min_prob_green = min_prob;
break;
548 case H_GOAL_YELLOW: min_prob_yellow = min_prob;
break;
549 case H_GOAL_BLUE: min_prob_blue = min_prob;
break;
550 case H_LINE: min_prob_white = min_prob;
break;
551 case H_ROBOT: min_prob_black = min_prob;
break;
Base class for exceptions in Fawkes.
float getObjectProb(hint_t object)
Get object probability.
void setMinProbForColor(float min_prob, hint_t hint)
Set min probability for color.
YuvColormap * get_colormap()
Get generated color model.
BayesHistosToLut(std::map< hint_t, Histogram * > &histos, unsigned int d=1, hint_t fg_object=H_UNKNOWN, unsigned int w=256, unsigned int h=256)
Constructor.
std::string getName()
Get name.
void calculateLutValues(bool penalty=false)
Calculate LUT values.
void saveLut(char *file)
Save LUT to file.
~BayesHistosToLut()
Destructor.
void save(std::string filename)
Save LUT to file.
void calculateLutAllColors()
Calculate all LUT colors.
float getAPosterioriProb(hint_t object, unsigned int u, unsigned int v)
P(object| u, v).
float getAPrioriProb(unsigned int u, unsigned int v, hint_t object)
P(u, v| object).
hint_t getMostLikelyObject(unsigned int u, unsigned int v)
Get most likely object.
void setMinProbability(float min_prob)
Set min probability.
static const ColorObjectMap & get_instance()
ColorObjectMap getter.
void add_colormap(Colormap *colormap)
Add colormap.
virtual void write(const char *file_name)
Write file.
unsigned int get_average()
Get average of all values.
unsigned int get_value(unsigned int x, unsigned int y)
Get value from histogram.
unsigned int get_median()
Get median of all values.
unsigned int get_num_undos()
Get number of undos.
unsigned int switch_undo(unsigned int undo_id)
Switch undo to another undo buffer.
void set_value(unsigned int x, unsigned int y, unsigned int value)
Set value in histogram.
virtual unsigned int height() const
Get height of colormap.
virtual void reset()
Reset colormap.
virtual unsigned int depth() const
Get depth of colormap.
virtual unsigned int width() const
Get width of colormap.
virtual void set(unsigned int y, unsigned int u, unsigned int v, color_t c)
Set color class for given YUV value.
virtual unsigned int deepness() const
Get deepness of colormap.