Fawkes API  Fawkes Development Version
similarity.h
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 #ifndef _FIREVISION_MODELS_COLOR_SIMILARITY_H_
28 #define _FIREVISION_MODELS_COLOR_SIMILARITY_H_
29 
30 #include "colormodel.h"
31 
32 #include <fvutils/color/rgb.h>
33 #include <fvutils/color/rgbyuv.h>
34 
35 #include <cmath>
36 #include <vector>
37 
38 namespace firevision {
39 
41 {
42 public:
44 
45  virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const;
46 
47  virtual const char *get_name();
48 
49  /**
50  * Parameters that define a certain color
51  */
52  typedef struct color_class_t
53  {
54  /** Discrete color_t represented by this class */
55  color_t result;
56 
57  /** YUV U-component of reference color */
58  int ref_u;
59 
60  /** YUV V-component of reference color */
61  int ref_v;
62 
63  /** YUV Y-component of reference color */
64  int ref_y;
65 
66  /** Required luminousity */
68 
69  /** Length of U,V vector, i.e. reference saturation */
71 
72  /** Required chroma similarity */
74 
75  /** Required saturation */
77 
78  /**
79  * Define the RGB values for the reference color
80  * @param ref A 3-element list [R, G, B]
81  */
82  void
83  set_reference(std::vector<unsigned int> &ref)
84  {
85  if (ref.at(0) > 0xff || ref.at(1) > 0xff || ref.at(2) > 0xff)
86  throw "invalid reference color";
87  int r = ref.at(0), g = ref.at(1), b = ref.at(2);
88  int y, u, v;
89  RGB2YUV(r, g, b, y, u, v);
90  ref_u = u - 0x80;
91  ref_v = v - 0x80;
92  ref_y = y;
93  ref_length = sqrt(ref_u * ref_u + ref_v * ref_v);
94  }
95 
96  /**
97  * Initialize a color class
98  * @param expect Discrete color_t represented by this class
99  * @param v A 3-element list [R, G, B]
100  * @param chroma_threshold Required color similarity (higher = more similar), 0..255
101  * @param saturation_threshold Required saturation (higher = more saturation), 0..255
102  * @param luma_threshold Required luminousity similarity (higher = more similar), 0..255, default 0
103  */
104  color_class_t(color_t expect,
105  std::vector<unsigned int> &v,
106  int chroma_threshold,
108  int luma_threshold = 0)
109  {
110  this->result = expect;
111  this->chroma_threshold = chroma_threshold;
112  this->saturation_threshold = saturation_threshold;
113  this->luma_threshold = luma_threshold;
114  set_reference(v);
115  }
117 
118  void add_color(color_class_t *color_class);
119  void add_colors(std::vector<color_class_t *> color_classes);
120  void delete_colors();
121 
122 private:
123  std::vector<color_class_t *> color_classes_;
124 };
125 
126 } /* namespace firevision */
127 
128 #endif /* FIREVISION_MODELS_COLOR_SIMILARITY_H__ */
Matches colors that are similar to given reference colors.
Definition: similarity.h:41
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
struct firevision::ColorModelSimilarity::color_class_t color_class_t
Parameters that define a certain color.
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
Color model interface.
Definition: colormodel.h:32
Parameters that define a certain color.
Definition: similarity.h:53
int chroma_threshold
Required chroma similarity.
Definition: similarity.h:73
int saturation_threshold
Required saturation.
Definition: similarity.h:76
void set_reference(std::vector< unsigned int > &ref)
Define the RGB values for the reference color.
Definition: similarity.h:83
int ref_v
YUV V-component of reference color.
Definition: similarity.h:61
color_t result
Discrete color_t represented by this class.
Definition: similarity.h:55
int ref_length
Length of U,V vector, i.e.
Definition: similarity.h:70
int ref_u
YUV U-component of reference color.
Definition: similarity.h:58
color_class_t(color_t expect, std::vector< unsigned int > &v, int chroma_threshold, int saturation_threshold, int luma_threshold=0)
Initialize a color class.
Definition: similarity.h:104
int ref_y
YUV Y-component of reference color.
Definition: similarity.h:64