Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleHistogramEqualization.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 /**
26  * @example ExampleHistogramEqualization.cpp
27  @relates HistogramEqualization
28  @brief Example for HistogramEqualization usage
29  @ingroup g_examples
30  @author MIP
31 */
32 
33 #include <Base/Image/ImageIO.hh>
34 #include <Base/Image/ImageConvert.hh>
35 #include <Base/Common/FileHandling.hh>
36 #include <Image/HistogramEqualization.hh>
37 #include <Image/HistogramImage.hh>
38 #include <iostream>
39 #include <fstream>
40 
41 using namespace BIAS;
42 using namespace std;
43 
44 //writes a data textfile for plotting with e.g. gnuplot
45 void WriteCumulativeHistogram(Image<unsigned char>& im)
46 {
47  HistogramImage hsrc;
48  hsrc.Init(256,256,1);
49  hsrc.InitHist(256);
50  hsrc.AddHist<unsigned char>(im);
51 
52  ofstream outfile("cumulathist.dat");
53  double sum = 0.0;
54  for(unsigned b=0;b<256; b++)
55  {
56  sum += hsrc.GetBin(b);
57  outfile<<sum<<endl;
58  }
59  outfile.close();
60 }
61 
62 
63 int main(int argc, char *argv[])
64 {
66  if(argc<2)
67  {
68  cout<<"usage: "<<argv[0]<<" <image file>"<<endl;
69  return -1;
70  }
71  Image<unsigned char> InputImg,GreyImg,OutPutImg;
72  if(ImageIO::Load(argv[1],InputImg)!=0)
73  {
74  cout<<"can't load "<<argv[1]<<endl;
75  return -2;
76  }
77  string full(argv[1]);
78  string dir,base,suffix;
79  FileHandling::SplitName(full,dir,base,suffix);
80  ImageConvert::ToGrey(InputImg,GreyImg);
81  ImageIO::Save(dir+base+"grey.mip",GreyImg);
82  HistEq.Filter(GreyImg,OutPutImg);
83  ImageIO::Save(dir+base+"HstEq.mip",OutPutImg);
84  WriteCumulativeHistogram(OutPutImg);
85  return 0;
86 }
int InitHist(unsigned int bincount=256, unsigned int histcount=1)
reserves the internal data structures for histcount histograms with bincount bins in each in one imag...
double GetBin(unsigned int bin, unsigned int hist=0)
Get the counted number of bin from hist, -1 for invalid bin/hist.
int AddHist(const Image< StorageType > &Image, unsigned int hist=0)
calculates the histogram of image and adds them to the internal data structures
static void SplitName(const std::string &fullname, std::string &dir, std::string &base, std::string &suffix)
Split full path into:
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
Class for easy histogram image generation.
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
prototyp for filter computation function
perfoms a histogram equalization on a grey value image
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey