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>
44 namespace firevision {
61 BayesHistosToLut::BayesHistosToLut(std::map<hint_t, Histogram *> &histos,
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;