PaletteRaster.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3 This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5 TerraLib is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9 
10 TerraLib is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License
16 along with TerraLib. See COPYING. If not, write to
17 TerraLib Team at <terralib-team@terralib.org>.
18 */
19 
20 /*!
21 \file terralib/rp/PaletteRaster.h
22 
23 \brief Raster functions to generate palette image.
24 */
25 
26 #ifndef __TERRALIB_RP_INTERNAL_PALETTERASTER_H
27 #define __TERRALIB_RP_INTERNAL_PALETTERASTER_H
28 
29 
30 
31 // TerraLib
32 #include "Config.h"
33 #include "../raster/Band.h"
34 #include "../raster/Grid.h"
35 #include "../raster/Raster.h"
36 #include "../maptools/RasterTransform.h"
37 
38 //stl
39 #include <list>
40 
41 namespace te
42 {
43  namespace rp
44  {
45 
46 #define NCUBESDIM 32 //!< Number of cubes in a dimension
47 #define CUBESIZE 8 //!< CUBESIZE = 256 / NCUBESDIM
48 
49  //! \class CubeColor
50  //! \brief Defines a cubic volume in the RGB color space
51  //!
52  //! The class CubeColor defines a cube of size CUBESIZE (=8) centered
53  //! in a given point in RGB color space. It allows the insertion of
54  //! RGB triplets, calculation of the mean color value (per color)
55  //! of triplets inserted and keeping control of population of
56  //! inserted colors as well.
57  //! from Spring code
59  {
60  public:
61 
62  /*!
63  \brief CubeColor constructor
64  \param ir: Red component of color cube center
65  \param ig: Green component of color cube center
66  \param ib: Blue component of color cube center
67  */
68  CubeColor(int ir, int ig, int ib);
69 
70  /*!
71  \brief Destructor
72  */
74 
75  /*!
76  \brief Inserts a triple rgb in CubeColor.
77  \param r: red component of color
78  \param g: green component of color
79  \param b: blue component of color
80  */
81  void InsertTripleRGB(int r, int g, int b);
82 
83  /*!
84  \brief Get red component of cube center
85  \return Index of color red
86  */
87  int IndexR() { return m_ibr; }
88 
89  /*!
90  \brief Get green component of cube center
91  \return Index of color green
92  */
93  int IndexG() { return m_ibg; }
94 
95  /*!
96  \brief Get blue component of cube center
97  \return Index of color blue
98  */
99  int IndexB() { return m_ibb; }
100 
101  /*!
102  \brief Get i-th mean color component value.
103  \param i: color component ( 0 = red, 1 = green, 2 = blue )
104  \return i-th mean color component value
105  */
106  double Mean(int i) { return (i < 3 && i >= 0) ? (m_mean[i]) : (0); }
107 
108  /*!
109  \brief Get number of Image rgb triples in the CubeColor
110  \return the population of CubeColor
111  */
112  double Pop() { return m_pop; }
113 
114  /*!
115  \brief Get the index of GroupColor associated with CubeColor
116  \return the index of CubeColor in the GroupColor.
117  */
118  int GroupColorIndex() { return m_igc; }
119 
120  /*!
121  \brief Sets group color index
122  */
123  void SetGroupColorIndex(int index) { m_igc = index; }
124 
125  protected:
126  int m_ibr, //!< Index of color red in the color cube space
127  m_ibg, //!< Index of color green in the color cube space
128  m_ibb; //!< Index of color blue in the color cube space
129  int m_igc; //!< Index of GroupColor associated with CubeColor
130  double m_mean[3]; //!< Mean of triples from an Image rgb belonging to this CubeColor
131  double m_pop; //!< Number of Image rgb triples in the CubeColor
132 
133  };
134 
135  //! \class GroupColor
136  //! \brief Defines a classification context
137  //!
138  //! The class GroupColor is a container of CubeColor objects allowing
139  //! the generation of statistics ( mean value and standard deviation )
140  //! of the colors in the group.
141  //! from Spring code
143  {
144  public:
145 
146  /*!
147  \brief Constructor
148  \param ident: group color identifier
149  */
150  GroupColor(int ident);
151 
152  /*!
153  \brief Destructor
154  */
156 
157  /*!
158  \brief Inserts a CubeColor in the List.
159  \param cc: a CubeColor object
160  */
162 
163  /*!
164  \brief Removes the current item from the list.
165  */
167 
168  /*!
169  \brief Returns the number of CubeColors in this GroupColor
170  \return TRUE: successs or FALSE: failure
171  */
172  size_t GetNCubeColors() { return m_CClist.size(); }
173 
174  /*!
175  \brief Splits the GroupColor in two ones. The new GroupColor will have
176  its id given by newgcid. The list of ColorCubes in the
177  original GroupColor will be divided with the new GroupColor.
178  \param newgcid: new group color identifier
179  \return pointer to CubeColor (NULL if empty)
180  */
181  GroupColor* Split(int newgcid);
182 
183  /*!
184  \brief Return Mean[i] of GroupColor
185  \param i: color component ( 0 = red, 1 = green, 2 = blue )
186  */
187  double Mean(int i) { return (i < 3 && i >= 0 && m_pop != 0) ? ((double)m_Ex[i] / m_pop) : ((double)0); }
188 
189  /*!
190  \brief Returns the biggest variance
191  */
192  double MaxVar() { return m_varmax; }
193 
194  private:
195  int m_id; //!< Id of GroupColor
196  double m_Ex[3]; //!< Expectation of means of CubeColors belonging to this GroupColor
197  double m_Ex2[3]; //!< Expectation of squared means of CubeColors belonging to this GroupColor
198  double m_var[3]; //!< Variance in each dimension of CubeColors belonging to this GroupColor
199  double m_varmax; //!< Biggest variance
200  double m_pop; //!< population of group color
201  std::list<CubeColor*> m_CClist; //!< List of pointers to CubeColors
202  std::list<CubeColor*>::iterator m_itCC; //!< iterator to CubeColors list
203 
204  };
205 
206  //! \class Codifima
207  //!
208  //! \brief Defines a Codifima class
209  //!
210  //! The class Codifima
211  //! describing the images in use (attribute images, classified images,
212  //! segmented images) and training data set as well.
213  //! from Spring code
214 
216  {
217  protected:
218 
220  bool AllocateHist();
223  bool histogram_3d();
226  bool k_means();
227  void Clear();
228 
229  public:
230  /*!
231  \brief Codifies a rgb raster to a paletta raster
232  \param inputRaster input rgb raster
233  \param rasterTransform RasterTransformation of input raster
234  \param outRasterPtr output platter raster
235  \paeam palettesize number of colors of palette( default = 256)
236  */
237  Codifima(const te::rst::Raster& inputRaster,
238  te::map::RasterTransform& rasterTransform,
239  te::rst::Raster& outRasterPtr,
240  int palettesize = 256);
241 
242  /*!
243  \brief Normal destructor
244  */
245  ~Codifima() { Clear(); }
246 
247  /*!
248  \brief Aplies the method
249  \return true or false
250  */
251  bool Apply();
252 
253  /*!
254  \brief Gets the generated palette
255  */
256  std::vector< te::rst::BandProperty::ColorEntry > getPalette() { return m_palette; }
257 
258  protected:
259  const te::rst::Raster* m_inputRaster; //!< input rgb raster
260  te::map::RasterTransform* m_rasterTransform; //!< Raster Transformation from input raster
261  te::rst::Raster *m_outputRaster; //!< output palette raster
262  std::vector< te::rst::BandProperty::ColorEntry > m_palette; //!< generated palette
263 
264  te::rst::Band *m_outBand; //!< output band
265 
266  unsigned int m_outNCols; //!< output columns number
267  unsigned int m_outNRows; //!< output rows number
268 
269  int m_ngc; //!< number of GroupColors
270  int m_firstvr, //!< fisrt valid red value
271  m_lastvr, //!< last valid red value
272  m_firstvg, //!< fisrt valid green value
273  m_lastvg, //!< last valid green value
274  m_firstvb, //!< fisrt valid blue value
275  m_lastvb; //!< last valid blue value
276 
277  short m_blockr[256], //!< red grouping block
278  m_blockg[256], //!< green grouping block
279  m_blockb[256]; //!< blue grouping block
280  double **m_lut; //!< lut - values ​​found for each channel (R,G,B)
281  int m_finalcolors; //!< final number of colors found
282  GroupColor **m_GC; //!< array of GroupColors
283  std::list<CubeColor*> m_CClist; //!< list of CubeColors present in the Image
284  CubeColor ****hist; //!< histogram of CubeColors
285  int ***matclass; //!< classification matrix
286  };
287 
288  } // end namespace rp
289 } // end namespace te
290 
291 #endif
te::rp::Codifima::m_outBand
te::rst::Band * m_outBand
output band
Definition: PaletteRaster.h:264
te::rp::GroupColor::m_CClist
std::list< CubeColor * > m_CClist
List of pointers to CubeColors.
Definition: PaletteRaster.h:201
te::rp::GroupColor
Defines a classification context.
Definition: PaletteRaster.h:143
te
TerraLib.
Definition: AddressGeocodingOp.h:52
te::rst::Band
A raster band description.
Definition: Band.h:64
te::rp::GroupColor::m_itCC
std::list< CubeColor * >::iterator m_itCC
iterator to CubeColors list
Definition: PaletteRaster.h:202
te::rp::GroupColor::RemoveCubeColor
CubeColor * RemoveCubeColor()
Removes the current item from the list.
te::rp::CubeColor::m_ibb
int m_ibb
Index of color blue in the color cube space.
Definition: PaletteRaster.h:128
te::rp::Codifima::getPalette
std::vector< te::rst::BandProperty::ColorEntry > getPalette()
Gets the generated palette.
Definition: PaletteRaster.h:256
te::map::Clear
@ Clear
Definition: Enums.h:179
te::map::RasterTransform
A Raster Transform is a class that defines functions to transform a styled raster.
Definition: RasterTransform.h:66
te::rp::Codifima::m_CClist
std::list< CubeColor * > m_CClist
list of CubeColors present in the Image
Definition: PaletteRaster.h:283
te::rst::Raster
An abstract class for raster data strucutures.
Definition: Raster.h:72
te::rp::Codifima::m_finalcolors
int m_finalcolors
final number of colors found
Definition: PaletteRaster.h:281
te::rp::Codifima::~Codifima
~Codifima()
Normal destructor.
Definition: PaletteRaster.h:245
te::rp::GroupColor::GetNCubeColors
size_t GetNCubeColors()
Returns the number of CubeColors in this GroupColor.
Definition: PaletteRaster.h:172
te::rp::CubeColor::m_ibr
int m_ibr
Index of color red in the color cube space.
Definition: PaletteRaster.h:126
te::rp::Codifima
Defines a Codifima class.
Definition: PaletteRaster.h:216
te::rp::GroupColor::Split
GroupColor * Split(int newgcid)
Splits the GroupColor in two ones. The new GroupColor will have its id given by newgcid....
te::rp::Codifima::Clear
void Clear()
te::rp::Codifima::m_outputRaster
te::rst::Raster * m_outputRaster
output palette raster
Definition: PaletteRaster.h:261
te::rp::Codifima::m_firstvb
int m_firstvb
fisrt valid blue value
Definition: PaletteRaster.h:274
te::rp::GroupColor::Mean
double Mean(int i)
Return Mean[i] of GroupColor.
Definition: PaletteRaster.h:187
te::rp::CubeColor::IndexG
int IndexG()
Get green component of cube center.
Definition: PaletteRaster.h:93
te::rp::Codifima::image_codifying
bool image_codifying()
te::rp::CubeColor::m_pop
double m_pop
Number of Image rgb triples in the CubeColor.
Definition: PaletteRaster.h:131
te::rp::Codifima::m_firstvg
int m_firstvg
fisrt valid green value
Definition: PaletteRaster.h:272
te::rp::Codifima::m_outNCols
unsigned int m_outNCols
output columns number
Definition: PaletteRaster.h:266
te::rp::Codifima::AllocateHist
bool AllocateHist()
te::rp::GroupColor::~GroupColor
~GroupColor()
Destructor.
te::rp::Codifima::color_generation
int color_generation()
te::rp::CubeColor::CubeColor
CubeColor(int ir, int ig, int ib)
CubeColor constructor.
te::rp::CubeColor::Mean
double Mean(int i)
Get i-th mean color component value.
Definition: PaletteRaster.h:106
te::rp::GroupColor::MaxVar
double MaxVar()
Returns the biggest variance.
Definition: PaletteRaster.h:192
te::rp::Codifima::DeallocateMatclass
void DeallocateMatclass()
te::rp::GroupColor::m_pop
double m_pop
population of group color
Definition: PaletteRaster.h:200
te::rp::Codifima::hist
CubeColor **** hist
histogram of CubeColors
Definition: PaletteRaster.h:284
te::rp::CubeColor::GroupColorIndex
int GroupColorIndex()
Get the index of GroupColor associated with CubeColor.
Definition: PaletteRaster.h:118
te::rp::Codifima::m_rasterTransform
te::map::RasterTransform * m_rasterTransform
Raster Transformation from input raster.
Definition: PaletteRaster.h:260
te::rp::Codifima::matclass
int *** matclass
classification matrix
Definition: PaletteRaster.h:285
te::rp::Codifima::Apply
bool Apply()
Aplies the method.
te::rp::Codifima::Codifima
Codifima(const te::rst::Raster &inputRaster, te::map::RasterTransform &rasterTransform, te::rst::Raster &outRasterPtr, int palettesize=256)
Codifies a rgb raster to a paletta raster.
te::rp::CubeColor
Defines a cubic volume in the RGB color space.
Definition: PaletteRaster.h:59
TERPEXPORT
#define TERPEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:139
te::rp::Codifima::m_lastvb
int m_lastvb
last valid blue value
Definition: PaletteRaster.h:275
te::rp::Codifima::m_ngc
int m_ngc
number of GroupColors
Definition: PaletteRaster.h:269
te::rp::Codifima::histogram_3d
bool histogram_3d()
te::rp::CubeColor::m_ibg
int m_ibg
Index of color green in the color cube space.
Definition: PaletteRaster.h:127
te::rp::Codifima::k_means
bool k_means()
te::rp::Codifima::m_outNRows
unsigned int m_outNRows
output rows number
Definition: PaletteRaster.h:267
te::rp::CubeColor::IndexB
int IndexB()
Get blue component of cube center.
Definition: PaletteRaster.h:99
te::rp::CubeColor::Pop
double Pop()
Get number of Image rgb triples in the CubeColor.
Definition: PaletteRaster.h:112
te::rp::Codifima::m_firstvr
int m_firstvr
fisrt valid red value
Definition: PaletteRaster.h:270
te::rp::Codifima::m_lastvr
int m_lastvr
last valid red value
Definition: PaletteRaster.h:271
te::rp::CubeColor::~CubeColor
~CubeColor()
Destructor.
Definition: PaletteRaster.h:73
te::rp::Codifima::m_lut
double ** m_lut
lut - values ​​found for each channel (R,G,B)
Definition: PaletteRaster.h:280
te::rp::GroupColor::m_id
int m_id
Id of GroupColor.
Definition: PaletteRaster.h:195
te::rp::GroupColor::GroupColor
GroupColor(int ident)
Constructor.
te::rp::Codifima::DeallocateHist
void DeallocateHist()
te::rp::Codifima::m_lastvg
int m_lastvg
last valid green value
Definition: PaletteRaster.h:273
Config.h
Proxy configuration file for TerraView (see terraview_config.h).
te::rp::Codifima::m_inputRaster
const te::rst::Raster * m_inputRaster
input rgb raster
Definition: PaletteRaster.h:259
te::rp::CubeColor::m_igc
int m_igc
Index of GroupColor associated with CubeColor.
Definition: PaletteRaster.h:129
te::rp::GroupColor::m_varmax
double m_varmax
Biggest variance.
Definition: PaletteRaster.h:199
te::rp::GroupColor::InsertCubeColor
void InsertCubeColor(CubeColor *cc)
Inserts a CubeColor in the List.
te::rp::Codifima::AllocateMatclass
bool AllocateMatclass()
te::rp::CubeColor::InsertTripleRGB
void InsertTripleRGB(int r, int g, int b)
Inserts a triple rgb in CubeColor.
te::rp::CubeColor::IndexR
int IndexR()
Get red component of cube center.
Definition: PaletteRaster.h:87
te::rp::CubeColor::SetGroupColorIndex
void SetGroupColorIndex(int index)
Sets group color index.
Definition: PaletteRaster.h:123
te::rp::Codifima::m_GC
GroupColor ** m_GC
array of GroupColors
Definition: PaletteRaster.h:282
te::rp::Codifima::m_palette
std::vector< te::rst::BandProperty::ColorEntry > m_palette
generated palette
Definition: PaletteRaster.h:262