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