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