All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Segmenter.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/idl/rp/Segmenter.cpp
22 
23 \brief Raster segmenters.
24 */
25 
26 #include "Segmenter.h"
27 #include "../Functions.h"
28 #include "../IdlRaster.h"
29 
30 //#include <terralib/memory/Raster.h>
31 
33 
34 #include <terralib/rp/Segmenter.h>
36 
39 #include <terralib/raster/Band.h>
40 #include <terralib/raster/Utils.h>
41 
42 #include <memory>
43 
44 namespace te
45 {
46  namespace idl
47  {
48  namespace rp
49  {
50  IDL_VPTR RegionGrowingSegmenter(int argc, IDL_VPTR *argv, char *argk)
51  {
52  // getting input parameters
53 
54  IDL_VPTR inputArray = argv[ 0 ];
55  IDL_ENSURE_ARRAY( inputArray );
56 
57  IDL_VPTR minSegmentSize = IDL_CvtUInt( 1, &argv[ 1 ] );
58  IDL_ENSURE_SCALAR( minSegmentSize );
59 
60  IDL_VPTR segmentsSimilarityThreshold = IDL_CvtDbl( 1, &argv[ 2 ] );
61  IDL_ENSURE_SCALAR( segmentsSimilarityThreshold );
62 
63  // creating the input raster
64 
65  te::idl::IdlRaster inputRaster( inputArray, false );
66 
67  const unsigned int nBands = inputRaster.getNumberOfBands();
68  const unsigned int nLines = inputRaster.getNumberOfRows();
69  const unsigned int nCols = inputRaster.getNumberOfColumns();
70 
71 // te::rst::CreateCopy( inputRaster, "inputRaster.tif" );
72 
73  // Creating the algorithm parameters
74 
76 
77  strategyParameters.m_minSegmentSize = minSegmentSize->value.ui;
78 // std::cout << std::endl << strategyParameters.m_minSegmentSize << std::endl;
79 
80  strategyParameters.m_segmentsSimilarityThreshold = segmentsSimilarityThreshold->value.d;
81 // std::cout << std::endl << strategyParameters.m_segmentsSimilarityThreshold << std::endl;
82 
83  te::rp::Segmenter::InputParameters algoInputParams;
84 
85  algoInputParams.m_inputRasterPtr = &inputRaster;
86  for( unsigned int band = 0 ; band < nBands ; ++band )
87  algoInputParams.m_inputRasterBands.push_back( band );
88 
89  algoInputParams.m_strategyName = "RegionGrowing";
90 
91  algoInputParams.setSegStrategyParams( strategyParameters );
92 
93  te::rp::Segmenter::OutputParameters algoOutputParams;
94  algoOutputParams.m_rType = "MEM";
95 
96  // Executing the algorithm
97 
98  te::rp::Segmenter algorithmInstance;
99 
100  if( ! algorithmInstance.initialize( algoInputParams ) )
101  {
102  IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_LONGJMP ,
103  "Terralib segmenter initialization error" );
104  }
105 
106  IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO,
107  "Terralib segmenter initialized" );
108 
109  if( ! algorithmInstance.execute( algoOutputParams ) )
110  {
111  IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_LONGJMP,
112  "Terralib segmenter execution error" );
113  }
114 
115  IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO,
116  "Terralib call finished" );
117 
118  // creating the output array
119 
120  const UCHAR outArrayDataType = terralib2IdlType(
121  algoOutputParams.m_outputRasterPtr->getBand( 0 )->getProperty()->getType() );
122 
123  IDL_ARRAY_DIM outArrayDims;
124  outArrayDims[ 0 ] = (IDL_MEMINT)nCols;
125  outArrayDims[ 1 ] = (IDL_MEMINT)nLines;
126 
127  IDL_VPTR outArray;
128  if( IDL_MakeTempArray( outArrayDataType, 2, outArrayDims,
129  IDL_ARR_INI_NOP, &outArray ) == 0 )
130  {
131  IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_LONGJMP,
132  "Output array allocation error" );
133  }
134 
135  algoOutputParams.m_outputRasterPtr->getBand( 0 )->read( 0, 0,
136  (void*)outArray->value.arr->data );
137 
138  IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO,
139  "Segmentation finished" );
140 
141  return outArray;
142  }
143 
144  } // namespace rp
145  } // namespace idl
146 } // namespace te
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
Definition: Segmenter.h:101
IDL_VPTR RegionGrowingSegmenter(int argc, IDL_VPTR *argv, char *argk)
Raster region growing segmentation.
Definition: Segmenter.cpp:50
It describes one band (or dimension) of a raster.
Segmenter Output Parameters.
Definition: Segmenter.h:149
Raster segmenters.
Raster segmentation.
Definition: Segmenter.h:73
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
Definition: Segmenter.cpp:802
std::size_t getNumberOfBands() const
Returns the number of bands (dimension of cells attribute values) in the raster.
Definition: IdlRaster.h:69
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: Segmenter.h:157
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Segmenter.h:153
Raster segmentation.
void setSegStrategyParams(const SegmenterStrategyParameters &segStratParams)
Set specific segmenter strategy parameters.
Definition: Segmenter.cpp:127
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Segmenter.h:85
Utility functions for the raster module.
It gives access to values in one band (dimension) of a raster.
Segmenter Input Parameters.
Definition: Segmenter.h:81
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
A adaptor to an external existent raster in a idl array form.
Definition: IdlRaster.h:50
Utility functions for dealing with strings.
This is the abstract factory for Rasters.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
Definition: Segmenter.cpp:217
UCHAR terralib2IdlType(const int &tlType)
Convert Terralib data types to IDL data types.
Definition: Functions.cpp:115
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Segmenter.h:87
Raster region growing segmenter strategy.