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;
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;
87 
88  m_pTriplets = new Vec3d[nMaxNumberOfTriplets];
89 }
90 
91 bool CRGBColorModel::AddRGBTriplet(int r, int g, int b)
92 {
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 
107 
108  for (i = 0; i < m_nCurrentPosition; 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 
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",
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",
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 }
float r4
Definition: Math3d.h:95
GLubyte g
Definition: glext.h:5166
float r7
Definition: Math3d.h:95
float r1
Definition: Math3d.h:95
float r2
Definition: Math3d.h:95
float r3
Definition: Math3d.h:95
Data structure for the representation of a 3D vector.
Definition: Math3d.h:73
float x
Definition: Math3d.h:75
float z
Definition: Math3d.h:75
void CalculateColorModel()
Mat3d inverse_rgb_covariance
Definition: RGBColorModel.h:96
const Vec3d zero_vec
Definition: Math3d.cpp:59
void SetVec(Vec3d &vec, float x, float y, float z)
Definition: Math3d.cpp:243
float y
Definition: Math3d.h:75
float r6
Definition: Math3d.h:95
void MulVecScalar(const Vec3d &vec, float scalar, Vec3d &result)
Definition: Math3d.cpp:502
void Invert(const Mat3d &matrix, Mat3d &result)
Definition: Math3d.cpp:657
void AddToVec(Vec3d &vec, const Vec3d &vectorToAdd)
Definition: Math3d.cpp:481
void MulMatScalar(const Mat3d &matrix, float scalar, Mat3d &result)
Definition: Math3d.cpp:509
void SetInverseCovariance(const Mat3d &inverse_covariance)
float r8
Definition: Math3d.h:95
float r9
Definition: Math3d.h:95
void SetMean(const Vec3d &mean)
float CalculateColorProbability(const Vec3d &rgb)
GLubyte GLubyte b
Definition: glext.h:5166
bool AddRGBTriplet(int r, int g, int b)
void Reset(int nMaxNumberOfTriplets)
GLdouble GLdouble GLdouble r
Definition: glext.h:3227
bool LoadFromFile(const char *pFileName)
Vec3d * m_pTriplets
Definition: RGBColorModel.h:98
float r5
Definition: Math3d.h:95
float EvaluateForm(const Vec3d &matrix1, const Mat3d &matrix2)
Definition: Math3d.cpp:626
bool SaveToFile(const char *pFileName)
void SubtractVecVec(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result)
Definition: Math3d.cpp:522
const Mat3d zero_mat
Definition: Math3d.cpp:61
int m_nMaxNumberOfTriplets
Definition: RGBColorModel.h:99
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
Data structure for the representation of a 3x3 matrix.
Definition: Math3d.h:93
float CalculateColorDistanceSquared(int r, int g, int b)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:28