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>
45 namespace firevision {
65 BayesHistosToLut::BayesHistosToLut(std::map<hint_t, Histogram*> &histos,
66 unsigned int d, hint_t
object,
unsigned int w,
unsigned int h)
79 min_probability = 0.3;
82 min_prob_yellow = 0.0;
100 return string(
"BayesHistosToLut");
112 if (fg_object == H_BALL) {
149 if ( object_probabilities.find(
object) != object_probabilities.end() ) {
150 return object_probabilities[object];
152 cout <<
"returning 0" << endl;
170 unsigned int sum = 0;
171 for (
unsigned int y = 0; y < depth; ++y) {
172 sum += histograms[object]->get_value(u, v, y);
175 return (
float(sum) /
float(numberOfOccurrences[
object]) );
192 return (
float(histograms[
object]->get_value(u, v, y)) /
float(numberOfOccurrences[
object]) );
210 float sumOfProbabilities = 0.0;
211 map<hint_t, Histogram*>::iterator hit;
212 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
216 if (sumOfProbabilities != 0) {
240 float sumOfProbabilities = 0.0;
241 map<hint_t, Histogram*>::iterator hit;
242 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
246 if (sumOfProbabilities != 0) {
264 hint_t mostLikelyObject = H_UNKNOWN;
265 float probOfMostLikelyObject = 0.0;
266 map<hint_t, Histogram*>::iterator hit;
267 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
270 if (tmp > probOfMostLikelyObject) {
271 probOfMostLikelyObject = tmp;
272 mostLikelyObject = (hint_t)hit->first;
276 if (probOfMostLikelyObject > min_probability) {
277 return mostLikelyObject;
295 hint_t mostLikelyObject = H_UNKNOWN;
296 float probOfMostLikelyObject = 0.0;
297 map<hint_t, Histogram*>::iterator hit;
298 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
301 if (tmp > probOfMostLikelyObject) {
302 probOfMostLikelyObject = tmp;
303 mostLikelyObject = (hint_t)hit->first;
307 if (probOfMostLikelyObject > min_probability) {
308 return mostLikelyObject;
321 map<hint_t, Histogram*>::iterator hit;
322 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
323 unsigned int total = 0;
324 for (
unsigned int v = 0; v < height; ++v) {
325 for (
unsigned int u = 0; u < width; ++u) {
326 for (
unsigned int y = 0; y < depth; ++y) {
327 unsigned int tmp = ((
Histogram*)(hit->second))->get_value(u, v, y);
333 numberOfOccurrences[ (hint_t)hit->first ] = total;
344 hint_t color_with_highest_prob;
347 for (
unsigned int y = 0; y < depth; ++y) {
349 for (
unsigned int v = 0; v < height; ++v) {
350 for (
unsigned int u = 0; u < width; ++u) {
354 color_with_highest_prob = H_UNKNOWN;
355 map<hint_t, Histogram*>::iterator hit;
356 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
358 if (numberOfOccurrences[ (hint_t)hit->first ] > 0) {
359 current_prob = float( hit->second->get_value(u, v, y) ) /
float( numberOfOccurrences[ hit->first ] );
362 if ( current_prob > highest_prob &&
363 current_prob > min_probability ) {
365 highest_prob = current_prob;
366 color_with_highest_prob = hit->first;
387 unsigned int old_undo = 0;
391 Histogram *histo_fg = histograms[fg_object];
392 Histogram *histo_bg = histograms[H_BACKGROUND];
396 cout <<
"Histogram::calculateLutValues: There are not enough undos possible for background histogram, not penalizing" << endl;
398 unsigned int bg_median = histo_bg->
get_median();
400 unsigned int bg_val = 0;
404 cout <<
"Histogram: Setting low bg vals to median. median=" << bg_median
405 <<
" avg=" << bg_average << endl;
407 for (
unsigned int v = 0; v < height; ++v) {
408 for (
unsigned int u = 0; u < width; ++u) {
409 for (
unsigned int y = 0; y < depth; ++y) {
411 if ( histo_fg->
get_value(u, v, y) == 0 ) {
413 if (bg_val < bg_average) {
414 histo_bg->
set_value(u, v, y, bg_average);
427 map<hint_t, Histogram*>::iterator hit;
428 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
429 unsigned int total = 0;
430 for (
unsigned int y = 0; y < depth; ++y) {
431 for (
unsigned int v = 0; v < height; ++v) {
432 for (
unsigned int u = 0; u < width; ++u) {
433 unsigned int tmp = hit->second->get_value(u, v, y);
439 numberOfOccurrences[hit->first] = total;
440 cout <<
"[" << hit->first <<
"]: " << numberOfOccurrences[hit->first] <<
" occurences" << endl;
443 unsigned int total_count = 0;
444 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
445 total_count += hit->second->get_sum();
450 for (hit = histograms.begin(); hit != histograms.end(); hit++) {
451 object_probabilities[hit->first] = (float)hit->second->get_sum() / (float)total_count;
458 unsigned int count_ball = 0;
459 unsigned int count_field = 0;
460 unsigned int count_line = 0;
461 unsigned int count_robot = 0;
462 unsigned int count_background = 0;
463 unsigned int count_goal = 0;
464 unsigned int count_unknown = 0;
468 for (
unsigned int y = 0; y < depth; ++y) {
470 for (
unsigned int u = 0; u < width; ++u) {
472 for (
unsigned int v = 0; v < height; ++v) {
476 switch(mostLikelyObject) {
501 cout <<
"(BayesHistosToLut::calculateLutValues(): Invalid object." << endl;
502 throw fawkes::Exception(
"BayesHistosToLut::calculateLutValues(): Invalid object.");
509 printf(
"d/w/h: %u/%u/%u ball: %d field: %d line: %d robot: %d goal: %d background: %d unknown: %d\n",
510 depth, width, height, count_ball, count_field, count_line,
511 count_robot, count_goal, count_background, count_unknown);
514 Histogram *histo_bg = histograms[H_BACKGROUND];
554 cmf.
write(filename.c_str());
564 min_probability = min_prob;
576 min_prob_ball = min_prob;
579 min_prob_green = min_prob;
582 min_prob_yellow = min_prob;
585 min_prob_blue = min_prob;
588 min_prob_white = min_prob;
591 min_prob_black = min_prob;