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 
29 #include <fvutils/color/threshold.h>
30 #include <fvutils/color/yuv.h>
31 
32 #include <math.h>
33 #include <stddef.h>
34 
35 namespace firevision {
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 {
44 }
45 
46 const char *
48 {
49  return "ColorModelSimilarity";
50 }
51 
52 /** Determine the color class of a given YUV value.
53  * Color classes have to be defined beforehand with ColorModelSimilarity::add_color.
54  * If multiple color classes have been defined, they are tried in reverse order, i.e. the class
55  * that has been added last is tried first. We return on the first match, so think of the color
56  * classes as a priority list.
57  * @param y Luminance (ignored)
58  * @param u Chroma U
59  * @param v Chroma V
60  * @return The color_t value from the matching color class, or C_OTHER if no match was found.
61  */
62 color_t
63 ColorModelSimilarity::determine(unsigned int y, unsigned int u, unsigned int v) const
64 {
65  for (std::vector<color_class_t *>::const_iterator it = color_classes_.begin();
66  it != color_classes_.end();
67  it++) {
68  if ((*it)->luma_threshold >= 0) {
69  if (is_similar_y(y,
70  u - 0x80,
71  v - 0x80,
72  (*it)->ref_y,
73  (*it)->ref_u,
74  (*it)->ref_v,
75  (*it)->ref_length,
76  (*it)->chroma_threshold,
77  (*it)->saturation_threshold,
78  (*it)->luma_threshold)) {
79  return (*it)->result;
80  }
81  } else {
82  if (is_similar(u - 0x80,
83  v - 0x80,
84  (*it)->ref_u,
85  (*it)->ref_v,
86  (*it)->ref_length,
87  (*it)->chroma_threshold,
88  (*it)->saturation_threshold)) {
89  return (*it)->result;
90  }
91  }
92  }
93  return C_OTHER;
94 }
95 
96 /** Add a color to be recognized by this colormodel.
97  * @param color_class The ColorModelSimilarity::color_class_t that will be returned by
98  * ColorModelSimilarity::determine on a match ColorModelSimilarity::color_class_t
99  */
100 void
102 {
103  color_classes_.push_back(color_class);
104 }
105 
106 /** Add multiple colors to this colormodel.
107  * @param color_classes A list of
108  */
109 void
110 ColorModelSimilarity::add_colors(std::vector<color_class_t *> color_classes)
111 {
112  color_classes_.insert(color_classes_.end(), color_classes.begin(), color_classes.end());
113 }
114 
115 /** Remove all colors from this colormodel.
116  */
117 void
119 {
120  color_classes_.clear();
121 }
122 
123 } /* namespace firevision */
virtual const char * get_name()
Get name of color model.
Definition: similarity.cpp:47
void add_color(color_class_t *color_class)
Add a color to be recognized by this colormodel.
Definition: similarity.cpp:101
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:63
void add_colors(std::vector< color_class_t * > color_classes)
Add multiple colors to this colormodel.
Definition: similarity.cpp:110
void delete_colors()
Remove all colors from this colormodel.
Definition: similarity.cpp:118
Parameters that define a certain color.
Definition: similarity.h:53