Commit de96e0d9 authored by Pierre Lassalle's avatar Pierre Lassalle

New criteria

parent 3b980eec
......@@ -17,3 +17,9 @@
#=========================================================================
add_executable(RegionMergingSegmentation RegionMergingSegmentation.cxx)
target_link_libraries(RegionMergingSegmentation OTBGRM)
add_executable(SpringSegmentation SpringSegmentation.cxx)
target_link_libraries(SpringSegmentation OTBGRM)
add_executable(FullLambdaScheduleSegmentation FullLambdaScheduleSegmentation.cxx)
target_link_libraries(FullLambdaScheduleSegmentation OTBGRM)
#include <iostream>
#include <otbImage.h>
#include <otbVectorImage.h>
#include <otbImageFileReader.h>
#include <otbImageFileWriter.h>
#include "lsrmFullLambdaScheduleSegmenter.h"
int main(int argc, char *argv[])
{
if(argc != 5)
{
std::cerr << "Usage: ./" << argv[0] << "\n"
<< "\t[input image path] : (.jpg, .png, .tif)\n"
<< "\t[output clustered image] : (.jpg, .png, .tif)\n"
<< "\t[output label image] : (.tif)\n"
<< "\t[lambda threshold] : unlimited positive value"
<< std::endl;
return 1;
}
const char * inFileName = argv[1];
const char * clusterFileName = argv[2];
const char * labelFileName = argv[3];
const float lambda = atof(argv[4]);
typedef float PixelType;
typedef unsigned long int LabelPixelType;
typedef unsigned char ClusterPixelType;
typedef otb::VectorImage<PixelType, 2> InputImageType;
typedef otb::Image<LabelPixelType, 2> LabelImageType;
typedef otb::VectorImage<ClusterPixelType, 2> ClusterImageType;
typedef otb::ImageFileReader<InputImageType> InputImageReaderType;
typedef otb::ImageFileWriter<LabelImageType> LabelImageWriterType;
typedef otb::ImageFileWriter<ClusterImageType> ClusterImageWriterType;
typedef lsrm::FullLambdaScheduleSegmenter<InputImageType> SegmenterType;
auto imgReader = InputImageReaderType::New();
imgReader->SetFileName(inFileName);
imgReader->Update();
SegmenterType segmenter;
segmenter.SetThreshold(lambda);
segmenter.SetInput(imgReader->GetOutput());
segmenter.Update();
auto labelWriter = LabelImageWriterType::New();
labelWriter->SetFileName(labelFileName);
labelWriter->SetInput(segmenter.GetLabeledClusteredOutput());
labelWriter->Update();
auto clusterWriter = ClusterImageWriterType::New();
clusterWriter->SetFileName(clusterFileName);
clusterWriter->SetInput(segmenter.GetClusteredImageOutput());
clusterWriter->Update();
return 0;
}
#include <iostream>
#include <otbImage.h>
#include <otbVectorImage.h>
#include <otbImageFileReader.h>
#include <otbImageFileWriter.h>
#include "lsrmSpringSegmenter.h"
int main(int argc, char *argv[])
{
if(argc != 5)
{
std::cerr << "Usage: ./" << argv[0] << "\n"
<< "\t[input image path] : (.jpg, .png, .tif)\n"
<< "\t[output clustered image] : (.jpg, .png, .tif)\n"
<< "\t[output label image] : (.tif)\n"
<< "\t[distance threshold] : unlimited positive value"
<< std::endl;
return 1;
}
const char * inFileName = argv[1];
const char * clusterFileName = argv[2];
const char * labelFileName = argv[3];
const float distThreshold = atof(argv[4]);
typedef float PixelType;
typedef unsigned long int LabelPixelType;
typedef unsigned char ClusterPixelType;
typedef otb::VectorImage<PixelType, 2> InputImageType;
typedef otb::Image<LabelPixelType, 2> LabelImageType;
typedef otb::VectorImage<ClusterPixelType, 2> ClusterImageType;
typedef otb::ImageFileReader<InputImageType> InputImageReaderType;
typedef otb::ImageFileWriter<LabelImageType> LabelImageWriterType;
typedef otb::ImageFileWriter<ClusterImageType> ClusterImageWriterType;
typedef lsrm::SpringSegmenter<InputImageType> SegmenterType;
auto imgReader = InputImageReaderType::New();
imgReader->SetFileName(inFileName);
imgReader->Update();
SegmenterType segmenter;
segmenter.SetThreshold(distThreshold);
segmenter.SetInput(imgReader->GetOutput());
segmenter.Update();
auto labelWriter = LabelImageWriterType::New();
labelWriter->SetFileName(labelFileName);
labelWriter->SetInput(segmenter.GetLabeledClusteredOutput());
labelWriter->Update();
auto clusterWriter = ClusterImageWriterType::New();
clusterWriter->SetFileName(clusterFileName);
clusterWriter->SetInput(segmenter.GetClusteredImageOutput());
clusterWriter->Update();
return 0;
}
......@@ -9,8 +9,6 @@ namespace lsrm
template<class TImage>
BaatzSegmenter<TImage>::BaatzSegmenter() : Superclass()
{
this->m_DoBFSegmentation = true;
this->m_NumberOfIterations = 75;
}
template<class TImage>
......
#ifndef __LSRM_FULL_LAMBDA_SCHEDULE_SEGMENTER_H
#define __LSRM_FULL_LAMBDA_SCHEDULE_SEGMENTER_H
#include "lsrmSegmenter.h"
namespace lsrm
{
struct FLSNode : Node<FLSNode>
{
std::vector<float> m_Means;
};
struct FLSParam{};
template<class TImage>
class FullLambdaScheduleSegmenter : public Segmenter< TImage, FLSNode, FLSParam>
{
public:
/* Some convenient typedefs */
typedef Segmenter<TImage, FLSNode, FLSParam> Superclass;
typedef TImage ImageType;
typedef typename Superclass::GraphType GraphType;
typedef typename Superclass::NodePointerType NodePointerType;
typedef typename Superclass::GraphOperatorType GraphOperatorType;
typedef GraphToOtbImage<GraphType> IOType;
FullLambdaScheduleSegmenter();
void Update();
float ComputeMergingCost(NodePointerType n1, NodePointerType n2);
void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2);
void InitFromImage();
};
} // end of namespace lsrm
#include "lsrmFullLambdaScheduleSegmenter.txx"
#endif
#ifndef __LSRM_FULL_LAMBDA_SCHEDULE_SEGMENTER_TXX
#define __LSRM_FULL_LAMBDA_SCHEDULE_SEGMENTER_TXX
namespace lsrm
{
template<class TImage>
FullLambdaScheduleSegmenter<TImage>::FullLambdaScheduleSegmenter() : Superclass()
{
}
template<class TImage>
void
FullLambdaScheduleSegmenter<TImage>::InitFromImage()
{
typedef itk::ImageRegionIterator<TImage> ImageIterator;
this->m_ImageWidth = this->m_InputImage->GetLargestPossibleRegion().GetSize()[0];
this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
std::size_t idx = 0;
ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
for(it.GoToBegin(); !it.IsAtEnd(); ++it)
{
this->m_Graph.m_Nodes[idx]->m_Means.reserve(this->m_NumberOfComponentsPerPixel);
for(std::size_t b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
{
this->m_Graph.m_Nodes[idx]->m_Means.push_back(it.Get()[b]);
}
++idx;
}
}
template<class TImage>
float
FullLambdaScheduleSegmenter<TImage>::ComputeMergingCost(NodePointerType n1, NodePointerType n2)
{
float eucDist = 0.0;
const float a1 = static_cast<float>(n1->m_Area);
const float a2 = static_cast<float>(n2->m_Area);
const float a_sum = a1 + a2;
for(unsigned int b = 0; b < this->m_NumberOfComponentsPerPixel; b++)
{
eucDist += (n1->m_Means[b] - n2->m_Means[b])*(n1->m_Means[b] - n2->m_Means[b]);
}
// Retrieve the length of the boundary between n1 and n2
auto toN2 = GraphOperatorType::FindEdge(n1, n2);
float cost = (((a1*a2)/a_sum)*eucDist) / (static_cast<float>(toN2->m_Boundary));
return cost;
}
template<class TImage>
void
FullLambdaScheduleSegmenter<TImage>::UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2)
{
const float a1 = static_cast<float>(n1->m_Area);
const float a2 = static_cast<float>(n2->m_Area);
const float a_sum = a1 + a2;
for(unsigned int b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
{
n1->m_Means[b] = (a1 * n1->m_Means[b] + a2 * n2->m_Means[b]) / a_sum;
}
}
template<class TImage>
void
FullLambdaScheduleSegmenter<TImage>::Update()
{
GraphOperatorType::InitNodes(this->m_InputImage, this->m_Graph, *this, FOUR);
bool prev_merged = false;
if(this->m_NumberOfIterations > 0)
{
prev_merged =
GraphOperatorType::PerfomAllIterationsWithLMBFAndConstThreshold(this->m_Graph, *this,
this->m_Threshold, this->m_NumberOfIterations,
this->m_ImageWidth, this->m_ImageHeight);
if(prev_merged && this->m_DoBFSegmentation)
{
prev_merged =
GraphOperatorType::PerfomAllIterationsWithBFAndConstThreshold(this->m_Graph, *this,
this->m_Threshold, this->m_NumberOfIterations,
this->m_ImageWidth, this->m_ImageHeight);
}
}
else
{
assert(this->m_DoBFSegmentation == true);
prev_merged =
GraphOperatorType::PerfomAllIterationsWithBFAndConstThreshold(this->m_Graph, *this,
this->m_Threshold, this->m_NumberOfIterations,
this->m_ImageWidth, this->m_ImageHeight);
}
}
}
#endif
This diff is collapsed.
......@@ -28,7 +28,10 @@ namespace lsrm
/* Default constructor and destructor */
Segmenter(){};
Segmenter(){
this->m_DoBFSegmentation = true;
this->m_NumberOfIterations = 75;
};
~Segmenter(){};
/*
......
#ifndef __LSRM_SPRING_SEGMENTER_H
#define __LSRM_SPRING_SEGMENTER_H
#include "lsrmSegmenter.h"
namespace lsrm
{
struct SpringNode : Node<SpringNode>
{
std::vector<float> m_Means;
};
struct SpringParam{};
template<class TImage>
class SpringSegmenter : public Segmenter< TImage, SpringNode, SpringParam>
{
public:
/* Some convenient typedefs */
typedef Segmenter<TImage, SpringNode, SpringParam> Superclass;
typedef TImage ImageType;
typedef typename Superclass::GraphType GraphType;
typedef typename Superclass::NodePointerType NodePointerType;
typedef typename Superclass::GraphOperatorType GraphOperatorType;
typedef GraphToOtbImage<GraphType> IOType;
SpringSegmenter();
void Update();
float ComputeMergingCost(NodePointerType n1, NodePointerType n2);
void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2);
void InitFromImage();
};
} // end of namespace lsrm
#include "lsrmSpringSegmenter.txx"
#endif
#ifndef __LSRM_SPRING_SEGMENTER_TXX
#define __LSRM_SPRING_SEGMENTER_TXX
#include <otbImageFileReader.h>
#include <itkImageRegionIterator.h>
namespace lsrm
{
template<class TImage>
SpringSegmenter<TImage>::SpringSegmenter() : Superclass()
{
}
template<class TImage>
void
SpringSegmenter<TImage>::InitFromImage()
{
typedef itk::ImageRegionIterator<TImage> ImageIterator;
this->m_ImageWidth = this->m_InputImage->GetLargestPossibleRegion().GetSize()[0];
this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
std::size_t idx = 0;
ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
for(it.GoToBegin(); !it.IsAtEnd(); ++it)
{
this->m_Graph.m_Nodes[idx]->m_Means.reserve(this->m_NumberOfComponentsPerPixel);
for(std::size_t b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
{
this->m_Graph.m_Nodes[idx]->m_Means.push_back(it.Get()[b]);
}
++idx;
}
}
template<class TImage>
float
SpringSegmenter<TImage>::ComputeMergingCost(NodePointerType n1, NodePointerType n2)
{
float eucDist = 0.0;
for(unsigned int b = 0; b < this->m_NumberOfComponentsPerPixel; b++)
{
eucDist += (n1->m_Means[b] - n2->m_Means[b])*(n1->m_Means[b] - n2->m_Means[b]);
}
return (static_cast<float>(std::sqrt(eucDist)));
}
template<class TImage>
void
SpringSegmenter<TImage>::UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2)
{
const float a1 = static_cast<float>(n1->m_Area);
const float a2 = static_cast<float>(n2->m_Area);
const float a_sum = a1 + a2;
for(unsigned int b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
{
n1->m_Means[b] = (a1 * n1->m_Means[b] + a2 * n2->m_Means[b]) / a_sum;
}
}
template<class TImage>
void
SpringSegmenter<TImage>::Update()
{
GraphOperatorType::InitNodes(this->m_InputImage, this->m_Graph, *this, FOUR);
bool prev_merged = false;
if(this->m_NumberOfIterations > 0)
{
prev_merged =
GraphOperatorType::PerfomAllIterationsWithLMBFAndConstThreshold(this->m_Graph, *this,
this->m_Threshold, this->m_NumberOfIterations,
this->m_ImageWidth, this->m_ImageHeight);
if(prev_merged && this->m_DoBFSegmentation)
{
prev_merged =
GraphOperatorType::PerfomAllIterationsWithBFAndConstThreshold(this->m_Graph, *this,
this->m_Threshold, this->m_NumberOfIterations,
this->m_ImageWidth, this->m_ImageHeight);
}
}
else
{
assert(this->m_DoBFSegmentation == true);
prev_merged =
GraphOperatorType::PerfomAllIterationsWithBFAndConstThreshold(this->m_Graph, *this,
this->m_Threshold, this->m_NumberOfIterations,
this->m_ImageWidth, this->m_ImageHeight);
}
}
}
#endif
File added
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