All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SegmenterWizardPage.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 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/qt/widgets/rp/SegmenterWizardPage.cpp
22 
23  \brief This file defines a class for a Segmenter Wizard page.
24 */
25 
26 // TerraLib
27 #include "../../../common/StringUtils.h"
28 #include "../../../dataaccess/dataset/DataSet.h"
29 #include "../../../dataaccess/utils/Utils.h"
30 #include "../../../raster/Raster.h"
31 #include "../../../rp/Segmenter.h"
32 #include "../../../rp/SegmenterRegionGrowingStrategy.h"
33 #include "RasterNavigatorWidget.h"
34 #include "SegmenterWizardPage.h"
35 #include "ui_SegmenterWizardPageForm.h"
36 
37 // Qt
38 #include <QGridLayout>
39 #include <QtGui/QCheckBox>
40 #include <QtGui/QIntValidator>
41 #include <QtGui/QMessageBox>
42 
43 // STL
44 #include <memory>
45 
46 
48  : QWizardPage(parent),
49  m_ui(new Ui::SegmenterWizardPageForm),
50  m_layer(0)
51 {
52 // setup controls
53  m_ui->setupUi(this);
54 
56 
57 //build form
58  QGridLayout* displayLayout = new QGridLayout(m_ui->m_frame);
60  m_navigator->showAsPreview(true);
61  m_navigator->hideColorCompositionTool(true);
62  displayLayout->addWidget(m_navigator.get());
63  displayLayout->setContentsMargins(0,0,0,0);
64 
65 //configure page
66  this->setTitle(tr("Segmenter"));
67  this->setSubTitle(tr("Select the type of segmenter and set their specific parameters."));
68 
69  QIntValidator* intValRG = new QIntValidator(this);
70  intValRG->setBottom(0);
71  m_ui->m_minimumSegmentSizeRGLineEdit->setValidator(intValRG);
72 
73  QIntValidator* intValB = new QIntValidator(this);
74  intValB->setBottom(0);
75  m_ui->m_minimumSegmentSizeRGLineEdit_2->setValidator(intValB);
76 
78  m_ui->m_thresholdRGDoubleSpinBox->setValue( regGrowStrategyParameters.m_segmentsSimilarityThreshold );
79  m_ui->m_colorWeightBaatzDoubleSpinBox->setValue( regGrowStrategyParameters.m_colorWeight );
80  m_ui->m_compactnessWeightBaatzDoubleSpinBox->setValue( regGrowStrategyParameters.m_compactnessWeight );
81 
82 //connects
83  connect(m_ui->m_strategyTypeComboBox, SIGNAL(activated(int)), this, SLOT(onStrategyTypeComboBoxActivated(int)));
84  connect(m_navigator.get(), SIGNAL(previewClicked()), this, SLOT(apply()));
85 }
86 
88 {
89 }
90 
92 {
93  int nBands = m_ui->m_bandTableWidget->rowCount();
94 
95  for(int i = 0; i < nBands; ++i)
96  {
97  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
98 
99  if(checkBox->isChecked())
100  {
101  return true;
102  }
103  }
104 
105  return false;
106 }
107 
109 {
110  m_layer = layer;
111 
112  std::list<te::map::AbstractLayerPtr> list;
113 
114  list.push_back(m_layer);
115 
116  m_navigator->set(m_layer, true);
117 
118  listBands();
119 }
120 
122 {
123  return m_layer;
124 }
125 
127 {
128  te::rp::Segmenter::InputParameters algoInputParams;
129 
130  //get input raster
131  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
132  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
133  m_inputRst.reset(ds->getRaster(rpos).release());
134 
135  //set segmenter parameters
136  algoInputParams.m_inputRasterPtr = m_inputRst.get();
137 
138  int nBands = m_ui->m_bandTableWidget->rowCount();
139 
140  for(int i = 0; i < nBands; ++i)
141  {
142  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
143 
144  if(checkBox->isChecked())
145  {
146  algoInputParams.m_inputRasterBands.push_back(i);
147  }
148  }
149 
150  int index = m_ui->m_strategyTypeComboBox->currentIndex();
151 
152  std::string strategyName = m_ui->m_strategyTypeComboBox->itemData(index).toString().toStdString();
153 
154  if(strategyName == "RegionGrowing")
155  {
157  strategyParameters.m_minSegmentSize = m_ui->m_minimumSegmentSizeRGLineEdit->text().toUInt();
158  strategyParameters.m_segmentsSimilarityThreshold = m_ui->m_thresholdRGDoubleSpinBox->value();
160 
161  algoInputParams.m_strategyName = "RegionGrowing";
162  algoInputParams.setSegStrategyParams( strategyParameters );
163  }
164  else if(strategyName == "Baatz")
165  {
167  strategyParameters.m_minSegmentSize = m_ui->m_minimumSegmentSizeRGLineEdit->text().toUInt();
168  strategyParameters.m_segmentsSimilarityThreshold = m_ui->m_thresholdBaatzDoubleSpinBox->value();
170 
171  for(int i = 0; i < nBands; ++i)
172  {
173  QCheckBox* checkBox = (QCheckBox*)m_ui->m_bandTableWidget->cellWidget(i, 0);
174 
175  if(checkBox->isChecked())
176  {
177  QDoubleSpinBox* spinBox = (QDoubleSpinBox*)m_ui->m_bandTableWidget->cellWidget(i, 1);
178 
179  strategyParameters.m_bandsWeights.push_back(spinBox->value());
180  }
181  }
182 
183  strategyParameters.m_colorWeight = m_ui->m_colorWeightBaatzDoubleSpinBox->value();
184  strategyParameters.m_compactnessWeight = m_ui->m_compactnessWeightBaatzDoubleSpinBox->value();
185 
186  algoInputParams.m_strategyName = "RegionGrowing";
187  algoInputParams.setSegStrategyParams( strategyParameters );
188  }
189 
190  return algoInputParams;
191 }
192 
194 {
195  std::string strategyName = m_ui->m_strategyTypeComboBox->itemData(index).toString().toStdString();
196 
197  if(strategyName == "RegionGrowing")
198  {
199  QStringList list;
200  list.append(tr("Band"));
201 
202  m_ui->m_bandTableWidget->setColumnCount(1);
203  m_ui->m_bandTableWidget->setHorizontalHeaderLabels(list);
204  }
205  else if(strategyName == "Baatz")
206  {
207  QStringList list;
208  list.append(tr("Band"));
209  list.append(tr("Weight"));
210 
211  m_ui->m_bandTableWidget->setColumnCount(2);
212  m_ui->m_bandTableWidget->setHorizontalHeaderLabels(list);
213 
214  int nBands = m_ui->m_bandTableWidget->rowCount();
215 
216  for(int i = 0; i < nBands; ++i)
217  {
218  QDoubleSpinBox* spinBox = new QDoubleSpinBox(m_ui->m_bandTableWidget);
219  spinBox->setMinimum(0.0);
220  spinBox->setMaximum(1.0);
221  spinBox->setSingleStep(0.1);
222  spinBox->setDecimals(4);
223  spinBox->setValue(0.3333);
224 
225  m_ui->m_bandTableWidget->setCellWidget(i, 1, spinBox);
226  }
227  }
228 
229  m_ui->m_bandTableWidget->resizeColumnsToContents();
230  m_ui->m_bandTableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);
231 }
232 
234 {
235  QApplication::setOverrideCursor(Qt::WaitCursor);
236 
237 //get preview raster
238  te::rst::Raster* inputRst = m_navigator->getExtentRaster(true);
239 
240  //set segmenters parameters
241  te::rp::Segmenter::InputParameters algoInputParams = getInputParams();
242 
243  algoInputParams.m_inputRasterPtr = inputRst;
244  algoInputParams.m_enableThreadedProcessing = false;
245  algoInputParams.m_enableBlockProcessing = false;
246  algoInputParams.m_enableBlockMerging = false;
247 
248  te::rp::Segmenter::OutputParameters algoOutputParams;
249 
250  std::map<std::string, std::string> rinfo;
251  rinfo["MEM_RASTER_NROWS"] = inputRst->getNumberOfRows();
252  rinfo["MEM_RASTER_NCOLS"] = inputRst->getNumberOfColumns();
253  rinfo["MEM_RASTER_DATATYPE"] = boost::lexical_cast<std::string>(inputRst->getBandDataType(0));
254  rinfo["MEM_RASTER_NBANDS"] = boost::lexical_cast<std::string>(inputRst->getNumberOfBands());
255 
256  algoOutputParams.m_rType = "MEM";
257  algoOutputParams.m_rInfo = rinfo;
258 
259  //run contrast
260  te::rp::Segmenter algorithmInstance;
261 
262  try
263  {
264  if(algorithmInstance.initialize(algoInputParams))
265  {
266  if(algorithmInstance.execute(algoOutputParams))
267  {
268  std::auto_ptr<te::rst::Raster> rst = algoOutputParams.m_outputRasterPtr;
269 
270  m_navigator->drawRaster(rst.get());
271  }
272  }
273  }
274  catch(...)
275  {
276  QMessageBox::warning(this, tr("Warning"), tr("Constrast error."));
277  }
278 
279  QApplication::restoreOverrideCursor();
280 
281  //delete input raster dataset
282  delete inputRst;
283 }
284 
286 {
287  m_ui->m_strategyTypeComboBox->clear();
288 
289  m_ui->m_strategyTypeComboBox->addItem(tr("Region Growing"), "RegionGrowing");
290  m_ui->m_strategyTypeComboBox->addItem(tr("Baatz"), "Baatz");
291 }
292 
294 {
295  assert(m_layer.get());
296 
297  //get input raster
298  std::auto_ptr<te::da::DataSet> ds = m_layer->getData();
299 
300  if(ds.get())
301  {
302  std::size_t rpos = te::da::GetFirstPropertyPos(ds.get(), te::dt::RASTER_TYPE);
303 
304  std::auto_ptr<te::rst::Raster> inputRst = ds->getRaster(rpos);
305 
306  if(inputRst.get())
307  {
308  for(unsigned int i = 0; i < inputRst->getNumberOfBands(); ++i)
309  {
310  m_ui->m_bandTableWidget->setRowCount(0);
311 
312  // initializing the list of bands
313  for(unsigned b = 0 ; b < inputRst->getNumberOfBands(); b++)
314  {
315  int newrow = m_ui->m_bandTableWidget->rowCount();
316  m_ui->m_bandTableWidget->insertRow(newrow);
317 
318  QString bName(tr("Band "));
319  bName.append(QString::number(b));
320 
321  QCheckBox* bandCheckBox = new QCheckBox(bName, this);
322  bandCheckBox->setChecked( true );
323 
324  m_ui->m_bandTableWidget->setCellWidget(newrow, 0, bandCheckBox);
325  }
326  }
327  }
328  }
329 }
std::auto_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
Definition: Segmenter.h:152
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
Definition: Segmenter.h:98
This file defines a class for a Segmenter Wizard page.
void set(te::map::AbstractLayerPtr layer)
This method is used to set the selected layer for segmenter operation.
This file has the RasterNavigatorWidget class.
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Segmenter.h:150
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
Segmenter Input Parameters.
Definition: Segmenter.h:80
bool m_enableBlockMerging
If true, a block merging procedure will be performed (default:true).
Definition: Segmenter.h:94
te::rp::Segmenter::InputParameters getInputParams()
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Segmenter.h:84
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
Segmenter Output Parameters.
Definition: Segmenter.h:144
This class is used to navigate over a DataSetLayer (having a raster representation) and given a set o...
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
double m_colorWeight
The weight given to the color component, deafult:0.5, valid range: [0,1].
bool m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...
Definition: Segmenter.h:92
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
Definition: Segmenter.cpp:706
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
Definition: Segmenter.h:88
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).
The Baatz based features will be used - Reference: Baatz, M.; Schape, A. Multiresolution segmentation...
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:428
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
Definition: Segmenter.cpp:206
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
Raster segmentation.
Definition: Segmenter.h:72
std::vector< double > m_bandsWeights
The weight given to each band, when applicable (note: the bands weights sum must always be 1) or an e...
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
SegmentFeaturesType m_segmentFeatures
What segment features will be used on the segmentation process (default:InvalidFeaturesType).
std::auto_ptr< te::qt::widgets::RasterNavigatorWidget > m_navigator
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Segmenter.h:86
double m_segmentsSimilarityThreshold
Segments similarity treshold - Segments with similarity values below this value will be merged; valid...
An abstract class for raster data strucutures.
Definition: Raster.h:70
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Segmenter.h:148
std::auto_ptr< Ui::SegmenterWizardPageForm > m_ui
void setSegStrategyParams(const SegmenterStrategyParameters &segStratParams)
Set specific segmenter strategy parameters.
Definition: Segmenter.cpp:117
The mean of segments pixel values will be used - Reference: S. A. Bins, L. M. G. Fonseca, G. J. Erthal e F. M. Ii, &quot;Satellite Imagery segmentation: a region growing approach&quot;, VIII Simposio Brasileiro de Sensoriamento Remoto, Salvador, BA, 14-19 abril 1996.