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

Example for HistogramImage usage: sort values in bins, etc...

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 ExampleHistogram.cpp
@relates HistogramImage
@brief Example for HistogramImage usage: sort values in bins, etc...
@ingroup g_examples
@author MIP
*/
#include <Base/Image/Image.hh>
#include <Base/Image/ImageIO.hh>
#include <Base/ImageUtils/ImageDraw.hh>
#include <Base/Math/Random.hh>
#include <Image/HistogramImage.hh>
#include <iostream>
using namespace BIAS;
using namespace std;
/** \file ExampleHistrogram.cpp
\brief usage for the HistogramImage class
*/
int main(int argc, char *argv[])
{
int res, binc=256;
Image<unsigned char> image1, image2;
Histogram2D hist2D;
if (argc <3) {
cerr<< argv[0] <<" <pgm file> <pgm file2>"<<endl;
exit(1);
}
if ((res=ImageIO::ImportImage(argv[1], image1)) != 0){
cerr << "error loading file " << argv[1] << " " << res << endl;
exit (-1);
}
if (image1.GetChannelCount() != 1){
cerr << "wrong channel count in image"<< endl;
}
hist.SetFactor(1);
// 256 bins and three histograms
hist.InitHist(binc, 3);
// use red as color fo histogram 0
hist.SetColor(255,0,0, 0);
// use green as color for histogram 1
hist.SetColor(0,255,0, 1);
// use blue as color for histogram 2
hist.SetColor(0,0,255, 2);
hist.AddHist(image1, 0);
if ((res=ImageIO::ImportImage(argv[2], image2)) != 0){
cerr << "error loading file " << argv[2] << " " << res << endl;
exit (-1);
}
hist.AddHist(image2, 1);
vector<double> h3;
Random r;
for (int i=0; i<1024; i++){
for(int j=0; j<1000; j++)
h3.push_back(r.GetUniformDistributed(DBL_MIN,DBL_MAX));
}
hist.AddHist(h3, 2);
hist.Draw();
//hist.DrawLog();
//hist.Dump();
unsigned st[2], end[2];
double dst[2]={0, 1000}, dend[2];
dend[0] = (double)binc; dend[1] = 1000;
unsigned char col[]={255, 0, 255};
dst[0]=0; dend[0]=binc;
for (int i=1000; i<15000; i+=1000){
dst[1]=dend[1]=i;
hist.CooTransf(dst, st, 0);
hist.CooTransf(dend, end, 0);
cerr <<"drawing "<<st[0]<<", "<<st[1]<<" -> "<<end[0]<<", "<<end[1]<<endl;
ImageDraw<unsigned char>::Line(hist, st, end, col);
}
if (ImageIO::Save("hist", hist, ImageIO::FF_ppm)!=0){
BIASERR("error writing");
}
vector<unsigned> xd, yd;
unsigned char *id1, *id2;
id1=image1.GetImageData();
id2=image2.GetImageData();
unsigned pc=image1.GetPixelCount();
if (image2.GetPixelCount()<pc) pc=image2.GetPixelCount();
for (unsigned i=0; i<pc; i++) {
xd.push_back((unsigned)id1[i]);
yd.push_back((unsigned)id2[i]);
}
hist2D.InitHist(128, 256);
hist2D.SetFactor(2);
hist2D.ZeroHist();
hist2D.AddHist(xd, yd);
// the black stripes in the histogram come from the discretizing
hist2D.DrawLog();
//hist2D.Dump();
//hist2D.WriteASCII();
if (ImageIO::Save("hist2D", hist2D, ImageIO::FF_ppm)!=0){
BIASERR("error writing");
}
return 0;
}