PostClassification.cpp
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/PostClassification.cpp
22 \brief Raster post classification.
23 */
24 
25 #include "PostClassification.h"
26 #include "Macros.h"
27 #include "Functions.h"
28 //#include "../statistics/core/SummaryFunctions.h"
29 #include "../raster/RasterFactory.h"
30 #include "../raster/BandProperty.h"
31 #include "../raster/Grid.h"
32 #include "../raster/Band.h"
33 //#include "../raster/BandIterator.h"
34 //#include "../raster/BandIteratorWindow.h"
35 //#include "../common/progress/TaskProgress.h"
36 
37 #include <memory>
38 //#include <cmath>
39 
40 namespace te
41 {
42  namespace rp
43  {
44 
46  {
47  reset();
48  }
49 
51  {
52  reset();
53  operator=(other);
54  }
55 
57  {
58  reset();
59  }
60 
62  {
63  m_inRasterPtr = nullptr;
64  m_weight = 0;
65  m_threshold = 0;
66  }
67 
70  {
71  reset();
72 
74  m_weight = params.m_weight;
75  m_threshold = params.m_threshold;
76 
77  return *this;
78  }
79 
81  {
82  return new InputParameters(*this);
83  }
84 
86  {
87  reset();
88  }
89 
91  {
92  reset();
93  operator=(other);
94  }
95 
97  {
98  reset();
99  }
100 
102  {
103  m_rType.clear();
104  m_rInfo.clear();
105  m_outputRasterPtr.reset();
106  }
107 
110  {
111  reset();
112 
113  m_rType = params.m_rType;
114  m_rInfo = params.m_rInfo;
115 
116  return *this;
117  }
118 
120  {
121  return new OutputParameters(*this);
122  }
123 
125  {
126  reset();
127  }
128 
130 
132  {
133  if (!m_isInitialized) return false;
134 
135  PostClassification::OutputParameters* outputParamsPtr = dynamic_cast<
136  PostClassification::OutputParameters* >(&outputParams);
137  TERP_TRUE_OR_THROW(outputParamsPtr, "Invalid paramters");
138 
139  // Initializing the output rasters
140 
141  std::vector<te::rst::BandProperty*> bandsProperties;
143  bandsProperties.push_back(bprop);
144 
146 
147  outputParamsPtr->m_outputRasterPtr.reset(
148  te::rst::RasterFactory::make(outputParamsPtr->m_rType, newgrid, bandsProperties, outputParamsPtr->m_rInfo, nullptr, nullptr));
150  "Output raster creation error");
151 
153 
154  const te::rst::Band& bout = *outputParamsPtr->m_outputRasterPtr->getBand(0);
155 
156  for (unsigned int row = 0; row < (outputParamsPtr->m_outputRasterPtr->getNumberOfRows() - 2); row++)
157  {
158  for (unsigned int col = 0; col < (outputParamsPtr->m_outputRasterPtr->getNumberOfColumns() - 2); col++)
159  {
160  double pixels[3][3];//pixels[col][row]
161 
162  for (int i = 0; i < 3; i++)
163  {
164  pixels[i][0] = 0;
165  pixels[i][1] = 0;
166  pixels[i][2] = 0;
167  }
168 
169  std::map<int, int> freq; //<class, frequence>
170  freq.clear();
171 
172  bout.getValue(col + 1, row + 1, pixels[1][1]);
173 
174  int freqvcenter = 0; //frequence of central pixel
175 
176  for (int c = 0; c < 3; c++)
177  {
178  for (int r = 0; r < 3; r++)
179  {
180  bout.getValue(col + static_cast<unsigned int>(c), row + static_cast<unsigned int>(r), pixels[c][r]);
181 
182  if (pixels[c][r] == pixels[1][1])
183  {
184  freqvcenter++;
185  continue;
186  }
187 
188  std::map<int, int>::iterator itFreq = freq.find(static_cast<int>(pixels[c][r]));
189 
190  if (itFreq != freq.end())
191  itFreq->second++;
192  else
193  freq.insert(std::map<int, int>::value_type(static_cast<int>(pixels[c][r]), 1));
194  }
195  }
196 
197  if (freq.size() == 0)
198  continue;
199 
200  freqvcenter += m_inputParameters.m_weight - 1;
201 
202  //Compare central pixel frequence with neighbors pixels
203  std::map<int, int>::iterator itFreq = freq.begin();
204 
205  int bestClass = itFreq->first;
206  int bestFreq = itFreq->second;
207  itFreq++;
208 
209  while (itFreq != freq.end())
210  {
211  if (itFreq->second > bestFreq)
212  {
213  bestClass = itFreq->first;
214  bestFreq = itFreq->second;
215  }
216 
217  itFreq++;
218  }
219 
220  //Gets frequence nearest to threshold
221  itFreq = freq.begin();
222  while (itFreq != freq.end())
223  {
224  if ((itFreq->second > static_cast<int>(m_inputParameters.m_threshold)) && (itFreq->second > bestFreq))
225  {
226  bestFreq = itFreq->second;
227  bestClass = itFreq->first;
228  }
229 
230  itFreq++;
231  }
232 
233  if ((bestFreq > static_cast<int>(m_inputParameters.m_threshold)) && (bestFreq > freqvcenter))
234  outputParamsPtr->m_outputRasterPtr->setValue(col + 1, row + 1, static_cast<double>(bestClass));
235 
236  freq.clear();
237  }
238  }
239 
240  return true;
241  }
242 
244  {
246 
247  m_isInitialized = false;
249  }
250 
252  {
253  reset();
254 
255  PostClassification::InputParameters const* inputParamsPtr = dynamic_cast<
256  PostClassification::InputParameters const* >(&inputParams);
257  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(inputParamsPtr, TE_TR("Invalid parameters"));
258 
260  TE_TR("Invalid raster pointer"));
263  TE_TR("Invalid raster"));
264 
265  TERP_INSTANCE_TRUE_OR_RETURN_FALSE(inputParamsPtr->m_weight > 0,
266  TE_TR("Invalid weight value"));
267 
268  TERP_INSTANCE_TRUE_OR_RETURN_FALSE((inputParamsPtr->m_threshold > 0),
269  TE_TR("Invalid threshold value"));
270 
271  m_inputParameters = *inputParamsPtr;
272 
273  m_isInitialized = true;
274 
275  return true;
276  }
277 
279  {
280  return m_isInitialized;
281  }
282 
284  unsigned int weight, unsigned int threshold)
285  {
286  te::rst::Copy(srcRaster, dstRaster);
287 
288  const te::rst::Band& bout = *dstRaster.getBand(0);
289 
290  for (unsigned int row = 0; row < (dstRaster.getNumberOfRows() - 2); row++)
291  {
292  for (unsigned int col = 0; col < (dstRaster.getNumberOfColumns() - 2); col++)
293  {
294  double pixels[3][3];//pixels[col][row]
295 
296  for (int i = 0; i < 3; i++)
297  {
298  pixels[i][0] = 0;
299  pixels[i][1] = 0;
300  pixels[i][2] = 0;
301  }
302 
303  std::map<int, int> freq; //<class, frequence>
304  freq.clear();
305 
306  bout.getValue(col + 1, row + 1, pixels[1][1]);
307 
308  int freqvcenter = 0; //frequence of central pixel
309 
310  for (int c = 0; c < 3; c++)
311  {
312  for (int r = 0; r < 3; r++)
313  {
314  bout.getValue(col + static_cast<unsigned int>(c), row + static_cast<unsigned int>(r), pixels[c][r]);
315 
316  if (pixels[c][r] == pixels[1][1])
317  {
318  freqvcenter++;
319  continue;
320  }
321 
322  std::map<int, int>::iterator itFreq = freq.find(static_cast<int>(pixels[c][r]));
323 
324  if (itFreq != freq.end())
325  itFreq->second++;
326  else
327  freq.insert(std::map<int, int>::value_type(static_cast<int>(pixels[c][r]), 1));
328  }
329  }
330 
331  if (freq.size() == 0)
332  continue;
333 
334  freqvcenter += weight - 1;
335 
336  // Compare central pixel frequence with neighbors pixels
337  std::map<int, int>::iterator itFreq = freq.begin();
338 
339  int bestClass = itFreq->first;
340  int bestFreq = itFreq->second;
341  itFreq++;
342 
343  while (itFreq != freq.end())
344  {
345  if (itFreq->second > bestFreq)
346  {
347  bestClass = itFreq->first;
348  bestFreq = itFreq->second;
349  }
350 
351  itFreq++;
352  }
353 
354  //Gets frequence nearest to threshold
355  itFreq = freq.begin();
356  while (itFreq != freq.end())
357  {
358  if ((itFreq->second > static_cast<int>(threshold)) && (itFreq->second > bestFreq))
359  {
360  bestFreq = itFreq->second;
361  bestClass = itFreq->first;
362  }
363 
364  itFreq++;
365  }
366 
367  if ((bestFreq > static_cast<int>(threshold)) && (bestFreq > freqvcenter))
368  dstRaster.setIValue(col + 1, row + 1, static_cast<double>(bestClass));
369 
370  freq.clear();
371  }
372  }
373 
374  return true;
375  }
376 
377  } // end namespace rp
378 } // end namespace te
379 
PostClassification output parameters.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
bool m_isInitialized
Is this instance already initialized?
A raster band description.
Definition: BandProperty.h:61
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
TERASTEREXPORT void Copy(const Raster &rin, Raster &rout)
Copies the pixel values from one raster to another.
Raster Processing algorithm output parameters base interface.
virtual void setIValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the imaginary attribute value in a complex band of a cell.
#define _NOEXCEPT_OP(x)
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:242
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
unsigned int m_weight
The weight value defines the number of times the frequency of the central point has to be considered...
AbstractParameters * clone() const
Create a clone copy of this instance.
PostClassification::InputParameters m_inputParameters
Input parameters.
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state...
unsigned int m_threshold
The threshold value is the frequency value above which the central point is modified.
bool initialize(const AlgorithmInputParameters &inputParams) _NOEXCEPT_OP(false)
Initialize the algorithm instance making it ready for execution.
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
BandProperty * getProperty()
Returns the band property.
URI C++ Library.
Definition: Attributes.h:37
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
A raster band description.
bool execute(AlgorithmOutputParameters &outputParams) _NOEXCEPT_OP(false)
Executes the algorithm using the supplied parameters.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
Raster post classification.
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Abstract parameters base interface.
const OutputParameters & operator=(const OutputParameters &params)
static Raster * make()
It creates and returns an empty raster with default raster driver.
AbstractParameters * clone() const
Create a clone copy of this instance.
bool runPostClassification(const te::rst::Raster &srcRaster, te::rst::Raster &dstRaster, unsigned int weight, unsigned int threshold)
Apply the post classification.
bool isInitialized() const
Returns true if the algorithm instance is initialized and ready for execution.
Raster Processing algorithm input parameters base interface.
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
const InputParameters & operator=(const InputParameters &params)
Raster Processing functions.
te::rst::Raster const * m_inRasterPtr
Input raster.
virtual void reset() _NOEXCEPT_OP(false)
Clear all internal allocated objects and reset the algorithm to its initial state.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
#define TERP_INSTANCE_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged, the current instance erro...
Definition: Macros.h:200
void reset() _NOEXCEPT_OP(false)
Clear all internal allocated resources and reset the parameters instance to its initial state...
unsigned int col
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:150
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.