Commit 9fd5b912 authored by Pierre Lassalle's avatar Pierre Lassalle

Add BF heuristic

parent b8247f75
......@@ -28,7 +28,7 @@ bool init_args(int argc, char ** argv, po::options_description& desc, po::variab
int main(int argc, char **argv)
{
po::options_description desc("Configuration of the Baatz & Schäpe region merging segmentation");
po::options_description desc("Configuration of the Baatz & Schâpe region merging segmentation");
po::variables_map vm;
if(init_args(argc, argv, desc, vm))
......@@ -56,11 +56,13 @@ bool init_args(int argc, char ** argv, po::options_description& desc, po::variab
{
desc.add_options()
("help", "print mandatory arguments")
("input", po::value<std::string>(), "set input image file")
("output_rgb", po::value<std::string>(), "set output rgb image file")
("cw", po::value<float>(), "set the spectral weight")
("sw", po::value<float>(), "set the shape weight")
("sp", po::value<float>(), "set the scale parameter");
("input", po::value<std::string>(), "set input image file (mandatory)")
("output_rgb", po::value<std::string>(), "set output rgb image file (mandatory)")
("cw", po::value<float>(), "set the spectral weight (mandatory)")
("sw", po::value<float>(), "set the shape weight (mandatory)")
("sp", po::value<float>(), "set the scale parameter (mandatory)")
("iter", po::value<unsigned int>(), "set the number of iterations using LMBF (optional)")
("bf", po::value<int>(), "activate the Best Fitting Heuristic (optional)");
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
......
......@@ -55,10 +55,12 @@ int main(int argc, char **argv)
bool init_args(int argc, char ** argv, po::options_description& desc, po::variables_map& vm)
{
desc.add_options()
("help", "print mandatory arguments")
("input", po::value<std::string>(), "set input image file")
("output_rgb", po::value<std::string>(), "set output rgb image file")
("thresh", po::value<float>(), "set the threshold");
("help", "print arguments")
("input", po::value<std::string>(), "set input image file (mandatory)")
("output_rgb", po::value<std::string>(), "set output rgb image file (mandatory)")
("thresh", po::value<float>(), "set the threshold (mandatory)")
("iter", po::value<unsigned int>(), "set the number of iterations using LMBF (optional)")
("bf", po::value<int>(), "activate the Best Fitting Heuristic");
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
......
......@@ -100,6 +100,7 @@ void
BaatzAlgorithmRM<TInputImage>::Segmentation()
{
bool prev_merged = true;
unsigned int step = 0;
float threshold = this->m_Parameters.m_Scale*this->m_Parameters.m_Scale;
float spec_w = this->m_Parameters.m_SpectralWeight;
float shap_w = this->m_Parameters.m_ShapeWeight;
......@@ -147,10 +148,12 @@ BaatzAlgorithmRM<TInputImage>::Segmentation()
return spec_cost;
};
while(prev_merged)
while(prev_merged && step < this->m_NumberOfIterations)
{
std::cout << "." << std::flush;
//std::cout << "." << std::flush;
prev_merged = false;
++step;
std::cout << step << std::endl;
this->m_RMHandler.UpdateMergingCosts(this->m_RegionList, cost_func);
......@@ -176,6 +179,51 @@ BaatzAlgorithmRM<TInputImage>::Segmentation()
// who have merged into a larger one
this->m_RMHandler.RemoveExpiredVertices(this->m_RegionList);
}
if(prev_merged && this->m_BestFitting)
{
while(prev_merged)
{
//std::cout << "." << std::flush;
std::cout << "Best Fitting" << std::endl;
prev_merged = false;
this->m_RMHandler.UpdateMergingCosts(this->m_RegionList, cost_func);
for(auto& r : this->m_RegionList)
{
// For each explored region, we determine if the LMBF is met
auto ref_region = this->m_RMHandler.CheckBF(r, threshold);
// If it holds then the pointer to ref_region is not null
if(ref_region != nullptr)
{
if(ref_region == r->GetClosestNeighbor())
{
// Process to merge the regions
// Step 1: Merge the specific attributes
UpdateAttribute(ref_region, r);
// Step 2: Internal update (mandatory)
this->m_RMHandler.Update(ref_region, r);
}
else
{
// Process to merge the regions
// Step 1: Merge the specific attributes
UpdateAttribute(ref_region, ref_region->GetClosestNeighbor());
// Step 2: Internal update (mandatory)
this->m_RMHandler.Update(ref_region, ref_region->GetClosestNeighbor());
}
prev_merged = true;
}
}
// After one iteration, you have to remove the expired regions, i.e. regions
// who have merged into a larger one
this->m_RMHandler.RemoveExpiredVertices(this->m_RegionList);
}
}
std::cout << "\n";
std::cout << "\n";
}
......
......@@ -83,6 +83,7 @@ void
FLSAlgorithmRM<TInputImage>::Segmentation()
{
bool prev_merged = true;
unsigned int step = 0;
float threshold = this->m_Parameters*this->m_Parameters;
MergingCostFunction cost_func = [](RegionPointerType r1, RegionPointerType r2)->double
......@@ -97,10 +98,11 @@ FLSAlgorithmRM<TInputImage>::Segmentation()
return cost;
};
while(prev_merged)
while(prev_merged && step < this->m_NumberOfIterations)
{
std::cout << "." << std::flush;
prev_merged = false;
++step;
this->m_RMHandler.UpdateMergingCosts(this->m_RegionList, cost_func);
......@@ -126,6 +128,39 @@ FLSAlgorithmRM<TInputImage>::Segmentation()
// who have merged into a larger one
this->m_RMHandler.RemoveExpiredVertices(this->m_RegionList);
}
if(prev_merged && this->m_BestFitting)
{
while(prev_merged)
{
std::cout << "." << std::flush;
prev_merged = false;
this->m_RMHandler.UpdateMergingCosts(this->m_RegionList, cost_func);
for(auto& r : this->m_RegionList)
{
// For each explored region, we determine if the LMBF is met
auto ref_region = this->m_RMHandler.CheckBF(r, threshold);
// If it holds then the pointer to ref_region is not null
if(ref_region != nullptr)
{
// Process to merge the regions
// Step 1: Merge the specific attributes
UpdateAttribute(ref_region, ref_region->GetClosestNeighbor());
// Step 2: Internal update (mandatory)
this->m_RMHandler.Update(ref_region, ref_region->GetClosestNeighbor());
prev_merged = true;
}
}
// After one iteration, you have to remove the expired regions, i.e. regions
// who have merged into a larger one
this->m_RMHandler.RemoveExpiredVertices(this->m_RegionList);
}
}
std::cout << "\n";
}
......
......@@ -36,7 +36,8 @@ namespace grm
typedef typename InterfaceType::MergingCostFunction MergingCostFunction;
// Empty constructor and destructor
RegionMergingAlgorithm(){}
RegionMergingAlgorithm() : m_BestFitting{true}, m_NumberOfIterations{70}
{}
virtual ~RegionMergingAlgorithm(){}
// Abstract method
......@@ -60,6 +61,7 @@ namespace grm
SetMacro(std::string, OutputRGB);
SetMacro(unsigned int, NumberOfIterations);
void SetDimensionForEncoder(unsigned int, unsigned int);
void SetBestFitting(int f);
protected:
......@@ -71,6 +73,7 @@ namespace grm
std::string m_OutputRGB;
// Algorithm attributes
bool m_BestFitting;
RegionListType m_RegionList;
ParameterType m_Parameters;
......
......@@ -25,9 +25,16 @@
namespace grm
{
/*template<class TInputImage, class TRegion, class TParams>
template<class TInputImage, class TRegion, class TParams>
void
RegionMergingAlgorithm<TInputImage, TRegion, TParams>::*/
RegionMergingAlgorithm<TInputImage, TRegion, TParams>::SetBestFitting(int f)
{
assert(f == 0 || f == 1);
if(f)
m_BestFitting = true;
else
m_BestFitting = false;
}
template<class TInputImage, class TRegion, class TParams>
void
......
......@@ -38,6 +38,7 @@ namespace grm
void InitRegions(RegionListType&);
void UpdateMergingCosts(RegionListType&, MergingCostFunction&);
RegionPointerType CheckBF(RegionPointerType r, double thresh);
RegionPointerType CheckLMBF(RegionPointerType, double);
void UpdateContour(RegionPointerType, RegionPointerType);
void UpdateNeighborhood(RegionPointerType, RegionPointerType);
......
......@@ -151,6 +151,34 @@ namespace grm
return nullptr;
}
template<class TRegion>
typename Interface<TRegion>::RegionPointerType
Interface<TRegion>::CheckBF(RegionPointerType r, double thresh)
{
assert(r != nullptr);
if(r->GetValid())
{
auto cost = std::get<1>(r->FrontEdgeList());
if(cost < thresh)
{
auto closest_neigh = std::get<0>(r->FrontEdgeList());
if(closest_neigh->GetValid())
{
if(r->GetId() < closest_neigh->GetId())
return r;
else
return closest_neigh;
}
else
return nullptr;
}
else
return nullptr;
}
else
return nullptr;
}
template<class TRegion>
void
Interface<TRegion>::UpdateContour(RegionPointerType r1, RegionPointerType r2)
......
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