Commit 8a1a5758 authored by Manuel Grizonnet's avatar Manuel Grizonnet

ADD: add labelMap example 1 in progress

parent c2e83b44
......@@ -10,6 +10,8 @@ TARGET_LINK_LIBRARIES(ShapeAttributeComputation OTBCommon OTBIO ${OTB_IO_UTILITI
ADD_EXECUTABLE(KeepNObjects KeepNObjects.cxx )
TARGET_LINK_LIBRARIES(KeepNObjects OTBCommon OTBIO ${OTB_IO_UTILITIES_DEPENDENT_LIBRARIES})
ADD_EXECUTABLE(RadiometricAttributesLabelMapFilterExample otbRadiometricAttributesLabelMapFilterExample.cxx )
TARGET_LINK_LIBRARIES(RadiometricAttributesLabelMapFilterExample OTBCommon OTBIO OTBBasicFilters OTBSpatialReasoning OTBFeatureExtraction OTBLearning ${OTB_IO_UTILITIES_DEPENDENT_LIBRARIES})
IF( NOT OTB_DISABLE_CXX_TESTING AND BUILD_TESTING )
......@@ -46,7 +48,9 @@ ADD_TEST(trTeOBIAShapeAttributeComputationTest ${EXE_TESTS}
${INPUTDATA}/MSLabeledOutput.tif
${TEMP}/OBIAShapeAttribute.txt
)
#need to add the test of radiometric labelmap filter
INCLUDE_DIRECTORIES(${OTB_SOURCE_DIR}/Testing/Code)
ADD_EXECUTABLE(otbOBIAExamplesTests otbOBIAExamplesTests.cxx)
TARGET_LINK_LIBRARIES(otbOBIAExamplesTests ITKAlgorithms ITKStatistics ITKNumerics OTBBasicFilters OTBCommon OTBDisparityMap OTBIO OTBSpatialReasoning OTBChangeDetection OTBFeatureExtraction OTBLearning OTBMultiScale OTBFusion OTBTesting)
......
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
// Software Guide : BeginCommandLineArgs
// INPUTS: {XX.tif}
// OUTPUTS: {XX.txt}
// Software Guide : EndCommandLineArgs
// Software Guide : BeginLatex
//
// This example shows the basic approch to perform
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "otbImage.h"
#include "otbVectorImage.h"
#include "otbImageFileReader.h"
#include "otbImageFileWriter.h"
#include "otbMeanShiftImageFilter.h"
#include "itkLabelImageToLabelMapFilter.h"
#include "otbAttributesMapLabelObject.h"
#include "itkLabelMap.h"
#include "otbShapeAttributesLabelMapFilter.h"
#include "otbStatisticsAttributesLabelMapFilter.h"
#include "otbRadiometricAttributesLabelMapFilter.h"
#include "otbAttributesMapOpeningLabelMapFilter.h"
#include "itkLabelMapToLabelImageFilter.h"
int main(int argc, char * argv[])
{
if(argc != 10)
{
std::cerr<<"Usage: "<<argv[0]<<" reffname outfname attribute_name lowerThan tresh spatialRadius rangeRadius minregionsize scale"<<std::endl;
return EXIT_FAILURE;
}
const char * reffname = argv[1];
const char * outfname = argv[2];
const char * attr = argv[3];
bool lowerThan = atoi(argv[4]);
double thresh = atof(argv[5]);
const unsigned int spatialRadius = atoi(argv[6]);
const double rangeRadius = atof(argv[7]);
const unsigned int minRegionSize = atoi(argv[8]);
const double scale = atoi(argv[9]);
const unsigned int Dimension = 2;
// Labeled image type
typedef unsigned short LabelType;
typedef double PixelType;
typedef otb::Image<LabelType,Dimension> LabeledImageType;
typedef otb::Image<PixelType,Dimension> ImageType;
typedef otb::VectorImage<PixelType,Dimension> VectorImageType;
typedef otb::ImageFileReader<LabeledImageType> LabeledReaderType;
typedef otb::ImageFileReader<ImageType> ReaderType;
typedef otb::ImageFileReader<VectorImageType> VectorReaderType;
typedef otb::ImageFileWriter<LabeledImageType> WriterType;
// Label map typedef
typedef otb::AttributesMapLabelObject<LabelType,Dimension,double> LabelObjectType;
typedef itk::LabelMap<LabelObjectType> LabelMapType;
typedef itk::LabelImageToLabelMapFilter<LabeledImageType,LabelMapType> LabelMapFilterType;
typedef otb::ShapeAttributesLabelMapFilter<LabelMapType> ShapeLabelMapFilterType;
typedef otb::StatisticsAttributesLabelMapFilter<LabelMapType,ImageType> StatisticsLabelMapFilterType;
typedef otb::RadiometricAttributesLabelMapFilter<LabelMapType,VectorImageType> RadiometricLabelMapFilterType;
typedef otb::AttributesMapOpeningLabelMapFilter<LabelMapType> OpeningLabelMapFilterType;
typedef itk::LabelMapToLabelImageFilter<LabelMapType,LabeledImageType> LabelMapToLabeledImageFilterType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(reffname);
//std::cout << reffname << std::endl;
//reader->Update();
//std::cout << reffname << std::endl;
VectorReaderType::Pointer vreader = VectorReaderType::New();
vreader->SetFileName(reffname);
//Segment the ref image using Mean Shift
typedef otb::MeanShiftImageFilter<ImageType,ImageType, LabeledImageType> FilterType;
FilterType::Pointer filter = FilterType::New();
filter->SetSpatialRadius(spatialRadius);
filter->SetRangeRadius(rangeRadius);
filter->SetMinimumRegionSize(minRegionSize);
filter->SetScale(scale);
filter->SetInput(reader->GetOutput());
LabelMapFilterType::Pointer labelMapFilter = LabelMapFilterType::New();
labelMapFilter->SetInput(filter->GetLabeledClusteredOutput());
labelMapFilter->SetBackgroundValue(itk::NumericTraits<LabelType>::max());
ShapeLabelMapFilterType::Pointer shapeLabelMapFilter = ShapeLabelMapFilterType::New();
shapeLabelMapFilter->SetInput(labelMapFilter->GetOutput());
StatisticsLabelMapFilterType::Pointer statisticsLabelMapFilter = StatisticsLabelMapFilterType::New();
statisticsLabelMapFilter->SetInput1(shapeLabelMapFilter->GetOutput());
statisticsLabelMapFilter->SetInput2(reader->GetOutput());
statisticsLabelMapFilter->Update();
RadiometricLabelMapFilterType::Pointer radiometricLabelMapFilter = RadiometricLabelMapFilterType::New();
radiometricLabelMapFilter->SetInput1(statisticsLabelMapFilter->GetOutput());
radiometricLabelMapFilter->SetInput2(vreader->GetOutput());
//radiometricLabelMapFilter->SetReducedAttributeSet(false);
radiometricLabelMapFilter->SetRedChannelIndex(1);
radiometricLabelMapFilter->SetNIRChannelIndex(2);
radiometricLabelMapFilter->Update();
OpeningLabelMapFilterType::Pointer opening = OpeningLabelMapFilterType::New();
opening->SetInput(radiometricLabelMapFilter->GetOutput());
opening->SetAttributeName(attr);
opening->SetReverseOrdering(lowerThan);
opening->SetLambda(thresh);
LabelMapToLabeledImageFilterType::Pointer labelMap2LabeledImage = LabelMapToLabeledImageFilterType::New();
labelMap2LabeledImage->SetInput(opening->GetOutput());
WriterType::Pointer writer = WriterType::New();
writer->SetFileName(outfname);
writer->SetInput(labelMap2LabeledImage->GetOutput());
writer->Update();
return EXIT_SUCCESS;
}
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