IVT
RGBColorModel.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: RGBColorModel.cpp
37 // Author: Pedram Azad
38 // Date: 2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "RGBColorModel.h"
49 
50 #include "Math/Math3d.h"
51 
52 #include <math.h>
53 #include <stdio.h>
54 
55 
56 
57 // *****************************************************************
58 // Constructor / Destructor
59 // *****************************************************************
60 
62 {
63  m_pTriplets = 0;
64  m_nMaxNumberOfTriplets = 0;
65  m_nCurrentPosition = 0;
66  m_fFactor = 0.5f;
67 }
68 
70 {
71  if (m_pTriplets)
72  delete [] m_pTriplets;
73 }
74 
75 
76 // ****************************************************************************
77 // Methods
78 // ****************************************************************************
79 
80 void CRGBColorModel::Reset(int nMaxNumberOfTriplets)
81 {
82  if (m_pTriplets)
83  delete [] m_pTriplets;
84 
85  m_nMaxNumberOfTriplets = nMaxNumberOfTriplets;
86  m_nCurrentPosition = 0;
87 
88  m_pTriplets = new Vec3d[nMaxNumberOfTriplets];
89 }
90 
91 bool CRGBColorModel::AddRGBTriplet(int r, int g, int b)
92 {
93  if (m_nCurrentPosition >= m_nMaxNumberOfTriplets)
94  return false;
95 
96  Math3d::SetVec(m_pTriplets[m_nCurrentPosition++], float(r), float(g), float(b));
97 
98  return true;
99 }
100 
102 {
103  Mat3d rgb_covariance;
104  int i;
105 
106  Math3d::SetVec(rgb_mean, Math3d::zero_vec);
107 
108  for (i = 0; i < m_nCurrentPosition; i++)
109  Math3d::AddToVec(rgb_mean, m_pTriplets[i]);
110 
111  Math3d::MulVecScalar(rgb_mean, 1.0f / m_nCurrentPosition, rgb_mean);
112 
113  Math3d::SetMat(rgb_covariance, Math3d::zero_mat);
114 
115  for (i = 0; i < m_nCurrentPosition; i++)
116  {
117  const float r = m_pTriplets[i].x - rgb_mean.x;
118  const float g = m_pTriplets[i].y - rgb_mean.y;
119  const float b = m_pTriplets[i].z - rgb_mean.z;
120  rgb_covariance.r1 += r * r;
121  rgb_covariance.r2 += r * g;
122  rgb_covariance.r3 += r * b;
123  rgb_covariance.r4 += r * g;
124  rgb_covariance.r5 += g * g;
125  rgb_covariance.r6 += g * b;
126  rgb_covariance.r7 += r * b;
127  rgb_covariance.r8 += g * b;
128  rgb_covariance.r9 += b * b;
129  }
130 
131  Math3d::MulMatScalar(rgb_covariance, 1.0f / (m_nCurrentPosition - 1), rgb_covariance);
132  Math3d::Invert(rgb_covariance, inverse_rgb_covariance);
133 
134  printf("mean = %f %f %f\n", rgb_mean.x, rgb_mean.y, rgb_mean.z);
135 }
136 
138 {
139  Vec3d diff;
140 
141  Math3d::SubtractVecVec(rgb, rgb_mean, diff);
142 
143  return exp(-m_fFactor * Math3d::EvaluateForm(diff, inverse_rgb_covariance));
144 }
145 
147 {
148  const float rd = (r - rgb_mean.x);
149  const float gd = (g - rgb_mean.y);
150  const float bd = (b - rgb_mean.z);
151 
152  return rd * rd + gd * gd + bd * bd;
153 }
154 
155 bool CRGBColorModel::LoadFromFile(const char *pFileName)
156 {
157  FILE *f = fopen(pFileName, "r");
158  if (!f)
159  return false;
160 
161  if (fscanf(f, "%f %f %f\n", &rgb_mean.x, &rgb_mean.y, &rgb_mean.z) == EOF)
162  {
163  fclose(f);
164  return false;
165  }
166 
167  if (fscanf(f, "%f %f %f %f %f %f %f %f %f\n",
168  &inverse_rgb_covariance.r1, &inverse_rgb_covariance.r2, &inverse_rgb_covariance.r3,
169  &inverse_rgb_covariance.r4, &inverse_rgb_covariance.r5, &inverse_rgb_covariance.r6,
170  &inverse_rgb_covariance.r7, &inverse_rgb_covariance.r8, &inverse_rgb_covariance.r9) == EOF)
171  {
172  fclose(f);
173  return false;
174  }
175 
176  fclose(f);
177 
178  return true;
179 }
180 
181 bool CRGBColorModel::SaveToFile(const char *pFileName)
182 {
183  FILE *f = fopen(pFileName, "w");
184  if (!f)
185  return false;
186 
187  if (fprintf(f, "%.10f %.10f %.10f\n", rgb_mean.x, rgb_mean.y, rgb_mean.z) < 0)
188  {
189  fclose(f);
190  return false;
191  }
192 
193  if (fprintf(f, "%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f\n",
194  inverse_rgb_covariance.r1, inverse_rgb_covariance.r2, inverse_rgb_covariance.r3,
195  inverse_rgb_covariance.r4, inverse_rgb_covariance.r5, inverse_rgb_covariance.r6,
196  inverse_rgb_covariance.r7, inverse_rgb_covariance.r8, inverse_rgb_covariance.r9) < 0)
197  {
198  fclose(f);
199  return false;
200  }
201 
202  fclose(f);
203 
204  return true;
205 }
206 
208 {
209  Math3d::SetVec(rgb_mean, mean);
210 }
211 
212 void CRGBColorModel::SetInverseCovariance(const Mat3d &inverse_covariance)
213 {
214  Math3d::SetMat(inverse_rgb_covariance, inverse_covariance);
215 }
void Invert(const Mat3d &matrix, Mat3d &result)
Definition: Math3d.cpp:657
float y
Definition: Math3d.h:75
float r4
Definition: Math3d.h:95
float r3
Definition: Math3d.h:95
const Mat3d zero_mat
Definition: Math3d.cpp:61
bool AddRGBTriplet(int r, int g, int b)
void SetInverseCovariance(const Mat3d &inverse_covariance)
float r1
Definition: Math3d.h:95
bool LoadFromFile(const char *pFileName)
float r7
Definition: Math3d.h:95
float EvaluateForm(const Vec3d &matrix1, const Mat3d &matrix2)
Definition: Math3d.cpp:626
float x
Definition: Math3d.h:75
float r2
Definition: Math3d.h:95
void Reset(int nMaxNumberOfTriplets)
void SetMean(const Vec3d &mean)
void CalculateColorModel()
float r8
Definition: Math3d.h:95
void SubtractVecVec(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result)
Definition: Math3d.cpp:522
const Vec3d zero_vec
Definition: Math3d.cpp:59
bool SaveToFile(const char *pFileName)
Data structure for the representation of a 3D vector.
Definition: Math3d.h:73
void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9)
Definition: Math3d.cpp:257
float z
Definition: Math3d.h:75
float CalculateColorDistanceSquared(int r, int g, int b)
float r6
Definition: Math3d.h:95
void AddToVec(Vec3d &vec, const Vec3d &vectorToAdd)
Definition: Math3d.cpp:481
Data structure for the representation of a 3x3 matrix.
Definition: Math3d.h:93
void MulMatScalar(const Mat3d &matrix, float scalar, Mat3d &result)
Definition: Math3d.cpp:509
float r9
Definition: Math3d.h:95
void MulVecScalar(const Vec3d &vec, float scalar, Vec3d &result)
Definition: Math3d.cpp:502
float CalculateColorProbability(const Vec3d &rgb)
float r5
Definition: Math3d.h:95
void SetVec(Vec3d &vec, float x, float y, float z)
Definition: Math3d.cpp:243