Commit 59fef71e authored by Arnaud Jaen's avatar Arnaud Jaen

ENH: Add option 4Connected/8Connected to GDALPolygonize in...

ENH: Add option 4Connected/8Connected to GDALPolygonize in LabelImageToVectorDataFilter and LabelImageToOGRDataSourceFilter
parent a0bb7648
......@@ -75,6 +75,9 @@ public:
itkSetMacro(FieldName, std::string);
itkGetMacro(FieldName, std::string);
itkSetMacro(Use8Connected, bool);
itkGetMacro(Use8Connected, bool);
const OGRDataSourceObjectType * GetOutput();
protected:
......@@ -96,6 +99,7 @@ private:
void operator =(const Self&); //purposely not implemented
std::string m_FieldName;
bool m_Use8Connected;
};
......
......@@ -80,7 +80,7 @@ private:
template <class TInputImage>
LabelImageToOGRDataSourceFilter<TInputImage>
::LabelImageToOGRDataSourceFilter() : m_FieldName("DN")
::LabelImageToOGRDataSourceFilter() : m_FieldName("DN"), m_Use8Connected(false)
{
this->SetNumberOfRequiredInputs(1);
this->SetNumberOfRequiredOutputs(1);
......@@ -229,7 +229,17 @@ LabelImageToOGRDataSourceFilter<TInputImage>
outputLayer->CreateField(&field, true);
//Call GDALPolygonize()
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, NULL, NULL, NULL);
char ** options;
options = NULL;
char * option[1];
if (m_Use8Connected == true)
{
std::string opt("8CONNECTED:8");
option[0] = const_cast<char *>(opt.c_str());
options=option;
}
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, options, NULL, NULL);
OGRDataSourceObjectType * decoratedOutput =
......
......@@ -82,6 +82,8 @@ public:
itkSetMacro(FieldName, std::string);
itkGetMacro(FieldName, std::string);
itkSetMacro(Use8Connected, bool);
itkGetMacro(Use8Connected, bool);
protected:
LabelImageToVectorDataFilter();
......@@ -97,6 +99,7 @@ private:
void operator =(const Self&); //purposely not implemented
std::string m_FieldName;
bool m_Use8Connected;
};
......
......@@ -36,7 +36,7 @@ namespace otb
template <class TInputImage, class TPrecision>
LabelImageToVectorDataFilter<TInputImage, TPrecision>
::LabelImageToVectorDataFilter() : m_FieldName("DN")
::LabelImageToVectorDataFilter() : m_FieldName("DN"), m_Use8Connected(false)
{
this->SetNumberOfRequiredInputs(1);
this->SetNumberOfRequiredOutputs(1);
......@@ -159,13 +159,23 @@ LabelImageToVectorDataFilter<TInputImage, TPrecision>
OGRSFDriver * ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName);
OGRDataSource * dataSource = ogrDriver->CreateDataSource("Shape",NULL);
OGRLayer * outputLayer = dataSource->CreateLayer("toto",NULL,wkbMultiPolygon,NULL);
OGRLayer * outputLayer = dataSource->CreateLayer("layer",NULL,wkbMultiPolygon,NULL);
OGRFieldDefn field(m_FieldName.c_str(),OFTInteger);
outputLayer->CreateField(&field, true);
//Call GDALPolygonize()
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, NULL, NULL, NULL);
char ** options;
options = NULL;
char * option[1];
if (m_Use8Connected == true)
{
std::string opt("8CONNECTED:8");
option[0] = const_cast<char *>(opt.c_str());
options=option;
}
GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, options, NULL, NULL);
/** Convert OGR layer into VectorData */
OGRFeatureDefn * dfn = outputLayer->GetLayerDefn();
......
......@@ -135,15 +135,15 @@ public:
itkGetObjectMacro(SegmentationFilter, SegmentationFilterType);
/*itkSetMacro(FieldName, std::string);
itkGetMacro(FieldName, std::string); */
void SetStartLabel(const LabelPixelType & label)
{
m_StartLabel = label;
m_TileMaxLabel = label;
}
itkGetMacro(StartLabel, LabelPixelType);
itkSetMacro(Use8Connected, bool);
itkGetMacro(Use8Connected, bool);
protected:
PersistentStreamingLabelImageToOGRDataFilter();
......@@ -164,6 +164,7 @@ private:
typename SegmentationFilterType::Pointer m_SegmentationFilter;
unsigned int m_TileNumber;
bool m_Use8Connected;
};
......@@ -235,6 +236,16 @@ public:
this->GetFilter()->Initialize();
}
void SetUse8Connected(bool flag)
{
this->GetFilter()->SetUse8Connected(flag);
}
const bool GetUse8Connected()
{
return this->GetFilter()->GetUse8Connected();
}
protected:
/** Constructor */
StreamingVectorizedSegmentationOGR() {}
......
......@@ -33,7 +33,7 @@ namespace otb
template <class TImageType, class TSegmentationFilter>
PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
::PersistentStreamingLabelImageToOGRDataFilter() : m_TileMaxLabel(0), m_StartLabel(0)
::PersistentStreamingLabelImageToOGRDataFilter() : m_TileMaxLabel(0), m_StartLabel(0), m_Use8Connected(false)
{
m_SegmentationFilter = SegmentationFilterType::New();
m_TileNumber = 1;
......@@ -85,6 +85,8 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
extract->SetExtractionRegion( this->GetInput()->GetBufferedRegion() );
extract->Update();
//std::cout<< "extract region " << extract->GetOutput()->GetLargestPossibleRegion()<<std::endl;
chrono.Stop();
//std::cout<< "extract took " << chrono.GetTotal() << " sec"<<std::endl;
......@@ -106,11 +108,13 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
chrono1.Stop();
std::cout<< "segmentation took " << chrono1.GetTotal() << " sec"<<std::endl;
itk::TimeProbe chrono2;
chrono2.Start();
labelImageToOGRDataFilter->SetInput(dynamic_cast<LabelImageType *>(m_SegmentationFilter->GetOutputs().at(labelImageIndex).GetPointer()));
labelImageToOGRDataFilter->SetFieldName(this->GetFieldName());
labelImageToOGRDataFilter->SetUse8Connected(m_Use8Connected);
labelImageToOGRDataFilter->Update();
chrono2.Stop();
......
......@@ -50,6 +50,7 @@ int otbLabelImageToVectorDataFilter(int argc, char * argv[])
reader->SetFileName(infname);
filter->SetInput(reader->GetOutput());
filter->SetUse8Connected(false);
writer->SetFileName(outfname);
writer->SetInput(filter->GetOutput());
......
......@@ -126,6 +126,7 @@ int otbStreamingVectorizedSegmentationOGR(int argc, char * argv[])
filter->GetStreamer()->SetTileDimensionTiledStreaming(atoi(argv[3]));
filter->SetFieldName(fieldName);
filter->SetStartLabel(1);
filter->SetUse8Connected(true);
filter->GetSegmentationFilter()->SetSpatialRadius(5);
filter->GetSegmentationFilter()->SetRangeRadius(15);
filter->GetSegmentationFilter()->SetMinimumRegionSize(100);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment