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

Example for condensation algorithm using the color histogram class ,ColorHistogram

Author
MIP
/*
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 ExampleCondensHisto.cpp
@relates CondensHisto,ColorHistogram
@ingroup g_examples
@ingroup g_stateestimation
@brief Example for condensation algorithm using the color histogram class
@author MIP
*/
#include "StateEstimator/CondensHisto.hh"
#include "Base/ImageUtils/ImageDraw.hh"
#include "Base/Image/ImageBase.hh"
#include "Base/Image/ImageIO.hh"
#include "Base/Image/ImageConvert.hh"
#include "Image/ColorHistogram.hh"
#include "Filter/Rescale.hh"
#include <Base/Common/FileHandling.hh>
#include "math.h"
using namespace BIAS;
using namespace std;
int main(int argc, char** argv)
{
stringstream fileName;
int res = -1;
int binsize=0;
int ux = 341;
int uy = 301;
int lx = 383;
int ly = 353;
BIAS::Image<unsigned char> image,image2, down ,down2;
BIAS::CondensHisto condensation;
//condensation.SetDebugLevel(D_COND_WEIGHTS);
//condensation.SetDebugLevel(condensation.GetDebugLevel() | D_COND_INIT);
//condensation.SetDebugLevel(condensation.GetDebugLevel() | D_COND_PROCESS);
//condensation.SetDebugLevel(condensation.GetDebugLevel() | D_COND_DEBUG);
if (argc<3)
{
cerr<<"Give a bin width and an image as arguments!"<<endl;
cerr<<"Usage:"<< argv[0] <<" binsize imagefile"<<endl;
return -1;
}
if(ImageIO::Load(argv[2], image) != 0)
{
cerr<<"Error reading image!"<<endl;
return -1;
}
//operate on half image sizes
down.Init((int)floor((double)image.GetWidth()/2),
(int)floor((double)image.GetHeight()/2),3);
rescale.SetFactor(2.0);
rescale.Downsample(image,down);
//convert image to HSL
image.Release();
image.Init(down.GetWidth(),down.GetHeight(),3);
res = ImageConvert::ToHSL(down,image);
cerr<<"Image is in HSL colorspace"<<endl;
fileName<<"MeanImage.mip";
ImageIO::Save(fileName.str(),down);
binsize = atoi(argv[1]);
chist.SetBinSize(binsize);
chist.GenerateHist(image,ux/2,uy/2,lx/2,ly/2);
chist.Dump();
condensation.SetArea(Vector3<double>(0,0,0.5),\
Vector3<double>(image.GetWidth(),image.GetHeight(),1.5));
//initialise condensation with amount of initial pixels
condensation.Init(250);
condensation.InitPrior();
condensation.SetMinWeight(0.0);
condensation.InitPriorPositions(250);
//now set the refHisto_ in condensation
condensation.SetActiveImage(image);
condensation.SetReferenceHistogram(chist);
//init a picture for the DrawWeightedSamples(img)
samples.Init(image.GetWidth(),image.GetHeight(),1);
int n =2;
while(n<argc){
//read in next image
if ((res=ImageIO::ImportImage(argv[n], image2)) != 0)
{
cerr<<"Error reading image!"<<endl;
return -1;
}
//convert image to HSL
down2.Init((int)floor((double)image2.GetWidth()/2),
(int)floor((double)image2.GetHeight()/2),3);
rescale.Downsample(image2,down2);
cout<<"Image: "<<n<<" imported."<<endl;
image2.Init(down2.GetWidth(),down2.GetHeight(),3);
ImageConvert::ToHSL(down2,image2);
cerr<<"Image is in HSL colorspace"<<endl;
//set the image as active in the condensation algorithm
condensation.SetActiveImage(image2);
//and now predict new positions(incl. Diffusion etc.
condensation.PredictNewSamplePositions();
//now process next step
condensation.Process();
condensation.DrawWeightedSamples(samples);
stringstream samplesName;
samplesName<<"SamplesImage_"<<FileHandling::LeadingZeroString(n,4)<<".mip";
ImageIO::Save(samplesName.str(),samples);
condensation.DrawMean(down2);
stringstream meanName;
meanName<<"MeanImage2_"<<FileHandling::LeadingZeroString(n,4)<<".mip";
ImageIO::Save(meanName.str(),down2);
cout<<"after Process()"<<endl;
n++;
}
}