SegmenterRegionGrowingFunctions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015 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/SegmenterRegionGrowingFunctions.cpp
22  \brief Useful functions for Region Growing segmentation.
23  */
24 
26 
27 #include "../raster/BandProperty.h"
28 #include "../raster/Grid.h"
29 #include "../raster/RasterFactory.h"
30 
31 #include <map>
32 
33 namespace te
34 {
35  namespace rp
36  {
37  namespace rg
38  {
39  void exportSegs2Tif( const SegmentsIdsMatrixT& segmentsIds,
40  bool normto8bits,
41  const std::string& fileName )
42  {
43  std::map<std::string, std::string> rinfo;
44  rinfo["SOURCE"] = fileName;
45 
46  const unsigned int linesNmb = segmentsIds.getLinesNumber();
47  const unsigned int colsNmb = segmentsIds.getColumnsNumber();
48 
49  te::rst::Grid* gridPtr = new te::rst::Grid( colsNmb, linesNmb );
50 
51  std::vector< te::rst::BandProperty* > bandsProps;
52  bandsProps.push_back( new te::rst::BandProperty( 0,
53  (normto8bits ? te::dt::UCHAR_TYPE : te::dt::UINT32_TYPE) ) );
54 
55  te::rst::Raster* rasterPtr = te::rst::RasterFactory::make( "GDAL",
56  gridPtr, bandsProps, rinfo );
57  TERP_TRUE_OR_THROW( rasterPtr, "Invalid pointer" );
58 
59  unsigned int col = 0;
60  unsigned int line = 0;
61 
62  double offset = 0.0;
63  double scale = 1.0;
64 
65  if( normto8bits ) {
66  double minValue = DBL_MAX;
67  double maxValue = -1.0 * DBL_MAX;
68  double value = 0;
69 
70  for( line = 0 ; line < linesNmb ; ++line ) {
71  for( col = 0 ; col < colsNmb ; ++col ) {
72  value = (double)segmentsIds( line, col );
73 
74  if( value > maxValue ) maxValue = value;
75  if( value < minValue ) minValue = value;
76  }
77  }
78 
79  offset = minValue;
80  scale = 254.0 / ( maxValue - minValue );
81  }
82 
83  double value = 0;
84 
85  for( line = 0 ; line < linesNmb ; ++line ) {
86  for( col = 0 ; col < colsNmb ; ++col ) {
87  value = ( ((double)segmentsIds( line, col )) - offset ) * scale;
88  TERP_TRUE_OR_THROW( value <= 255.0, "Invalid value:" +
89  boost::lexical_cast< std::string >( value ) );
90 
91  rasterPtr->setValue( col, line, value , 0 );
92  }
93  }
94 
95  delete rasterPtr;
96  }
97  }
98  }
99 }
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
A raster band description.
Definition: BandProperty.h:61
unsigned int line
An abstract class for raster data strucutures.
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:798
URI C++ Library.
Definition: Attributes.h:37
static Raster * make()
It creates and returns an empty raster with default raster driver.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:791
void exportSegs2Tif(const SegmentsIdsMatrixT &segmentsIds, bool normto8bits, const std::string &fileName)
Export the segments IDs to a tif file.
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