Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleHistogram.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 /**
27  * @example ExampleHistogram.cpp
28  @relates HistogramImage
29  @brief Example for HistogramImage usage: sort values in bins, etc...
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <Base/Image/Image.hh>
35 #include <Base/Image/ImageIO.hh>
36 #include <Base/ImageUtils/ImageDraw.hh>
37 #include <Base/Math/Random.hh>
38 #include <Image/HistogramImage.hh>
39 #include <iostream>
40 
41 using namespace BIAS;
42 using namespace std;
43 /** \file ExampleHistrogram.cpp
44  \brief usage for the HistogramImage class
45 */
46 int main(int argc, char *argv[])
47 {
48  int res, binc=256;
49 
50  Image<unsigned char> image1, image2;
51  HistogramImage hist;
52  Histogram2D hist2D;
53 
54  if (argc <3) {
55  cerr<< argv[0] <<" <pgm file> <pgm file2>"<<endl;
56  exit(1);
57  }
58 
59  if ((res=ImageIO::ImportImage(argv[1], image1)) != 0){
60  cerr << "error loading file " << argv[1] << " " << res << endl;
61  exit (-1);
62  }
63 
64  if (image1.GetChannelCount() != 1){
65  cerr << "wrong channel count in image"<< endl;
66  }
67 
68  hist.SetFactor(1);
69 
70  hist.SetDrawMode(HIST_LINES);
71 
72  // 256 bins and three histograms
73  hist.InitHist(binc, 3);
74 
75  // use red as color fo histogram 0
76  hist.SetColor(255,0,0, 0);
77  // use green as color for histogram 1
78  hist.SetColor(0,255,0, 1);
79  // use blue as color for histogram 2
80  hist.SetColor(0,0,255, 2);
81 
82  hist.AddHist(image1, 0);
83 
84  if ((res=ImageIO::ImportImage(argv[2], image2)) != 0){
85  cerr << "error loading file " << argv[2] << " " << res << endl;
86  exit (-1);
87  }
88 
89  hist.AddHist(image2, 1);
90 
91  vector<double> h3;
92  Random r;
93  for (int i=0; i<1024; i++){
94  for(int j=0; j<1000; j++)
95  h3.push_back(r.GetUniformDistributed(DBL_MIN,DBL_MAX));
96  }
97 
98  hist.AddHist(h3, 2);
99 
100  hist.Draw();
101 
102  //hist.DrawLog();
103 
104  //hist.Dump();
105 
106  unsigned st[2], end[2];
107  double dst[2]={0, 1000}, dend[2];
108  dend[0] = (double)binc; dend[1] = 1000;
109  unsigned char col[]={255, 0, 255};
110  dst[0]=0; dend[0]=binc;
111  for (int i=1000; i<15000; i+=1000){
112  dst[1]=dend[1]=i;
113  hist.CooTransf(dst, st, 0);
114  hist.CooTransf(dend, end, 0);
115  cerr <<"drawing "<<st[0]<<", "<<st[1]<<" -> "<<end[0]<<", "<<end[1]<<endl;
116  ImageDraw<unsigned char>::Line(hist, st, end, col);
117  }
118 
119 
120  if (ImageIO::Save("hist", hist, ImageIO::FF_ppm)!=0){
121  BIASERR("error writing");
122  }
123 
124  vector<unsigned> xd, yd;
125  unsigned char *id1, *id2;
126  id1=image1.GetImageData();
127  id2=image2.GetImageData();
128  unsigned pc=image1.GetPixelCount();
129  if (image2.GetPixelCount()<pc) pc=image2.GetPixelCount();
130  for (unsigned i=0; i<pc; i++) {
131  xd.push_back((unsigned)id1[i]);
132  yd.push_back((unsigned)id2[i]);
133  }
134 
135  hist2D.InitHist(128, 256);
136 
137  hist2D.SetFactor(2);
138 
139  hist2D.ZeroHist();
140 
141  hist2D.AddHist(xd, yd);
142 
143  // the black stripes in the histogram come from the discretizing
144  hist2D.DrawLog();
145 
146  //hist2D.Dump();
147  //hist2D.WriteASCII();
148 
149  if (ImageIO::Save("hist2D", hist2D, ImageIO::FF_ppm)!=0){
150  BIASERR("error writing");
151  }
152 
153  return 0;
154 }
155 
void InitHist(unsigned xbincount=256, unsigned ybincount=256)
static int ImportImage(const std::string &filename, ImageBase &result)
Try to load/import an image from a file using external libs.
Definition: ImageIO.cpp:378
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...
int Draw()
actually draws histogram(s) from the internal data structures
double GetUniformDistributed(const double min, const double max)
on succesive calls return uniform distributed random variable between min and max ...
Definition: Random.hh:84
int AddHist(const Image< StorageType > &Image, unsigned int hist=0)
calculates the histogram of image and adds them to the internal data structures
static int Line(Image< StorageType > &im, const unsigned int start[2], const unsigned int end[2], const StorageType value[])
lines
Definition: ImageDraw.cpp:404
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
void SetFactor(unsigned short Factor)
sets the zoomfactor
void CooTransf(double datacoo[2], double imcoo[2], unsigned hist=0)
transforms data coordinates to image coordinates does automatic clipping
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.
int SetColor(unsigned char R, unsigned char G, unsigned char B, unsigned int hist=0)
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
void SetDrawMode(HistMode m)
Select drawing style. Default is HIST_POINTS.
void AddHist(const std::vector< DataType > &xdata, const std::vector< DataType > &ydata)
unsigned long int GetPixelCount() const
returns number of pixels in image
Definition: ImageBase.hh:422
void SetFactor(unsigned short Factor)
sets the zoomfactor
class for producing random numbers from different distributions
Definition: Random.hh:51