Fawkes API  Fawkes Development Version
similarity.cpp
1 /***************************************************************************
2  * similarity.h - A colormodel that detects colors which are similar to a
3  * given reference color. Tolerance is expressed in maximum saturation and
4  * chroma deviation.
5  *
6  * Uses the algorithm ported from the VLC colorthreshold filter written by
7  * Sigmund Augdal and Antoine Cellerier. Cf.
8  * modules/video_filter/colorthres.c in the VLC source tree.
9  *
10  * (C) 2014 Victor MatarĂ©.
11  ****************************************************************************/
12 
13 /* This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version. A runtime exception applies to
17  * this software (see LICENSE.GPL_WRE file mentioned below for details).
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU Library General Public License for more details.
23  *
24  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
25  */
26 
27 #include "similarity.h"
28 #include <stddef.h>
29 #include <fvutils/color/yuv.h>
30 #include <fvutils/color/threshold.h>
31 #include <math.h>
32 
33 
34 namespace firevision
35 {
36 
37 /** @class ColorModelSimilarity <fvmodels/color/similarity.cpp>
38  * Matches colors that are similar to given reference colors.
39  * @author Victor MatarĂ©
40  */
41 
42 ColorModelSimilarity::ColorModelSimilarity() {}
43 
45  return "ColorModelSimilarity";
46 }
47 
48 /** Determine the color class of a given YUV value.
49  * Color classes have to be defined beforehand with ColorModelSimilarity::add_color.
50  * If multiple color classes have been defined, they are tried in reverse order, i.e. the class
51  * that has been added last is tried first. We return on the first match, so think of the color
52  * classes as a priority list.
53  * @param y Luminance (ignored)
54  * @param u Chroma U
55  * @param v Chroma V
56  * @return The color_t value from the matching color class, or C_OTHER if no match was found.
57  */
58 color_t ColorModelSimilarity::determine(unsigned int y, unsigned int u, unsigned int v) const {
59  for(std::vector<color_class_t *>::const_iterator it = color_classes_.begin();
60  it != color_classes_.end(); it++) {
61  if((*it)->luma_threshold >= 0) {
62  if (is_similar_y(y, u - 0x80, v - 0x80,
63  (*it)->ref_y, (*it)->ref_u, (*it)->ref_v, (*it)->ref_length,
64  (*it)->chroma_threshold, (*it)->saturation_threshold, (*it)->luma_threshold)) {
65  return (*it)->result;
66  }
67  }
68  else {
69  if(is_similar(u - 0x80, v - 0x80,
70  (*it)->ref_u, (*it)->ref_v, (*it)->ref_length,
71  (*it)->chroma_threshold, (*it)->saturation_threshold)) {
72  return (*it)->result;
73  }
74  }
75  }
76  return C_OTHER;
77 }
78 
79 /** Add a color to be recognized by this colormodel.
80  * @param color_class The ColorModelSimilarity::color_class_t that will be returned by
81  * ColorModelSimilarity::determine on a match ColorModelSimilarity::color_class_t
82  */
84  color_classes_.push_back(color_class);
85 }
86 
87 /** Add multiple colors to this colormodel.
88  * @param color_classes A list of
89  */
90 void ColorModelSimilarity::add_colors(std::vector<color_class_t *> color_classes) {
91  color_classes_.insert(color_classes_.end(), color_classes.begin(), color_classes.end());
92 }
93 
94 /** Remove all colors from this colormodel.
95  */
97  color_classes_.clear();
98 }
99 
100 } /* namespace firevision */
101 
102 
Parameters that define a certain color.
Definition: similarity.h:53
void add_color(color_class_t *color_class)
Add a color to be recognized by this colormodel.
Definition: similarity.cpp:83
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const
Determine the color class of a given YUV value.
Definition: similarity.cpp:58
void delete_colors()
Remove all colors from this colormodel.
Definition: similarity.cpp:96
void add_colors(std::vector< color_class_t *> color_classes)
Add multiple colors to this colormodel.
Definition: similarity.cpp:90
virtual const char * get_name()
Get name of color model.
Definition: similarity.cpp:44