Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleBlobDetectorLevelSet.cpp

Example for usage of class BlobDetectorLevelSet Usage: Simple start this program. The input: ExampleSegmentationLevelSet.pgm (a picture of Bodo Rosenhahn) The output: __orig.pgm, __set[000-070].pgm __lev.pgm

Author
Bodo Rosenhahn for MIP by Dennis Herzog
/*
This file is part of the BIAS library (Basic ImageAlgorithmS).
Copyright (C) 2003-2009 (see file CONTACT for details)
Multimediale Systeme der Informationsverarbeitung
Institut fuer Informatik
Christian-Albrechts-Universitaet Kiel
BIAS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
BIAS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with BIAS; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
@example ExampleBlobDetectorLevelSet.cpp
@relates BlobDetectorLevelSet
@brief Example for usage of class BlobDetectorLevelSet
Usage: Simple start this program.
The input: ExampleSegmentationLevelSet.pgm (a picture of Bodo Rosenhahn)
The output: __orig.pgm, __set[000-070].pgm __lev.pgm
@ingroup g_examples
@author Bodo Rosenhahn for MIP by Dennis Herzog
*/
#include "../BlobDetectorLevelSet.hh"
// std includes
#include <string>
#include <iostream>
// bias includes
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/Image/ImageConvert.hh>
#include <Utils/Param.hh>
using namespace std;
using namespace BIAS;
BIAS::Param*const para = new Param();
void WriteImage(const BIAS::ImageBase& im,
const string name, const int id = -1)
{
stringstream num;
if(id>=0)
num<<name<<FileHandling::LeadingZeroString(3,id)<<".pgm";
else
num<<name<<".pgm";
ImageIO::Save(num.str(),const_cast<ImageBase&>(im), ImageIO::FF_pgm);
}
void ReadImage(BIAS::Image<unsigned char>& im, const string name)
{
ImageIO::ImportImage(name, buf);
ImageConvert::ToGrey(buf, im);
}
void AddParams()
{
para->AddParamBool("help","shows help on params",false,'h');
para->AddParamString("image","image used for evaluation",
"./ExampleSegmentationLevelSet.pgm");
para->AddParamString("level","initial level set");
para->AddParamString("o","output file of final level set","__lev",'o');
para->AddParamVecInt("lv",
"rectangle specifying level set "
"(overwhelmed by --level) [x1 y1 x2 y2]",
vec0i(0) );
para->AddParamString("eval","evaluation region (roi)");
para->AddParamVecInt("ev",
"rectangle specifying evaluation region "
"(overwhelmed by --eval) [x1 y1 x2 y2]", vec0i(0) );
para->AddParamInt("n","do n iterations",80);
}
vec0i GetVec(const string param) { return *para->GetParamVecInt(param); }
string GetStr(const string param) { return *para->GetParamString(param); }
int GetInt(const string param) { return *para->GetParamInt(param); }
vec2i GetVec(const string param, const int i, const vec2i def = vec2i(0,0)) {
cout << "KKKKKKKKKKKKK"<<GetVec(param)<<endl;
if ( (int)GetVec(param).Size() > i*2 )
return vec2i( GetVec(param).SubVec(2,i*2) );
else
return def;
}
int main(int args, char **arg)
{
cout << "starting..." << endl;
// initialise the param stuff and show help if requested
AddParams();
para->ParseCommandLine(args,arg);
if ( *para->GetParamBool("help") == true ) {
para->Usage();
return 0;
}
// SetmentationLevelSet does all the work
// initialise image (as defined by params)
ReadImage(image, GetStr("image"));
// image dimenstions
const int w = image.GetWidth();
const int h = image.GetHeight();
// ? initialise image's roi
if ( GetStr("eval") != "" ) {
ReadImage(eval, GetStr("eval"));
image.GetROI()->SetMaskImage(eval);
BIASASSERT( (int)eval.GetWidth()==w && (int)eval.GetHeight()==h );
BIASASSERT( eval.GetChannelCount()==1 );
}
// set image (containing possibly msk) as lset's image to work on
lset.Init(image, image.GetROI()->MaskValid() );
// load level set as image, iff specified by params
if ( GetStr("level") != "" ) {
ReadImage(levelset, GetStr("level"));
lset.GetLevel() = levelset;
BIASASSERT( (int)levelset.GetWidth()==w && (int)levelset.GetHeight()==h );
BIASASSERT( levelset.GetChannelCount()==1 );
}
// set evaluation & level set squared (as defined by params)
if ( GetStr("eval") == "" )
lset.SetEvaluationSquare( GetVec("ev",0, vec2i(0,0)),
GetVec("ev",1, vec2i(w,h)) );
if ( GetStr("level") == "" )
lset.InitLevelSetSquare( GetVec("lv",0, vec2i(w/4,h/4)),
GetVec("lv",1, vec2i(w/4*3,h/4*3)) );
cout << "initialised." << endl;
// reused buffer for segmented image
// write initial image and it's initial segmentation (defined by level set)
WriteImage(lset.GetImage(),"./__image");
WriteImage(lset.GetEval(),"./__image_eval");
WriteImage(lset.GetLevel(),"./__image_level");
lset.GetSegmentation(im_seg);
WriteImage(im_seg,"./__seg",0);
cout << "running." << endl;
// evolute n times (as defined by params) and store the segmentation
for (int i = 1; i <= GetInt("n"); i++) {
cout << "Iteration: " << i << endl;
lset.Evolute();
lset.GetSegmentation(im_seg);
WriteImage(im_seg,"./__seg",i);
}
// write resulting level set if requested
if ( GetStr("o") != "" )
WriteImage(lset.GetLevel(), GetStr("o"));
cout << "done." << endl;
return 0;
}