r123: *** empty log message ***
[ctsim.git] / libctsim / imagefile.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **      Name:         imagefile.cpp
5 **      Purpose:      Imagefile classes
6 **      Programmer:   Kevin Rosenberg
7 **      Date Started: June 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: imagefile.cpp,v 1.6 2000/06/28 15:25:34 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 #include "ct.h"
29
30
31
32 void 
33 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
34 {
35   int hx = (m_nx - 1) / 2;
36   int hy = (m_ny - 1) / 2;
37   ImageFileArray v = getArray();
38   SignalFilter filter (filterName, domainName, bw, filt_param);
39
40   for (int i = -hx; i <= hx; i++) {
41     for (int j = -hy; j <= hy; j++) {
42       double r = sqrt (i * i + j * j);
43       
44       v[i+hx][j+hy] = filter.response (r);
45     }
46   }
47 }
48
49 int
50 ImageFile::display (void)
51 {
52     double pmin, pmax;
53
54     //    getPixelValueRange (pmin, pmax);
55
56     return (displayScaling (1, pmin, pmax));
57 }
58
59 int 
60 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax)
61 {
62     int grayscale[256];
63     int nx = m_nx;
64     int ny = m_ny;
65     ImageFileArray v = getArray();
66
67 #if HAVE_G2_H
68     int pens [nx * ny * scale * scale ];
69
70     double view_scale = 255 / (pmax - pmin);
71     int id_X11 = g2_open_X11 (nx * scale, ny * scale);
72
73     for (int i = 0; i < 256; i++) {
74         double cval = i / 255.;
75         grayscale[i] = g2_ink (id_X11, cval, cval, cval);
76     }
77
78     for (int i= 0, iy = ny - 1; iy >= 0; iy--) {
79         for (int ix = 0; ix < nx; ix++) {
80             int cval = static_cast<int>((v[ix][iy] - pmin) * view_scale);
81             if (cval < 0)  
82               cval = 0;
83             else if (cval > 255) 
84               cval = 255;
85             pens[i++] = grayscale[cval];
86         }
87     }
88
89     g2_image (id_X11, 0., 0., nx, ny, pens);
90
91     return (id_X11);
92
93 #endif
94 }
95
96
97
98 // ImageFile::comparativeStatistics    Calculate comparative stats
99 //
100 // OUTPUT
101 //   d   Normalized root mean squared distance measure
102 //   r   Normalized mean absolute distance measure
103 //   e   Worst case distance measure
104 //
105 // REFERENCES
106 //  G.T. Herman, Image Reconstruction From Projections, 1980
107
108 bool
109 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
110 {
111     if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
112         sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
113         return false;
114     }
115     ImageFileArrayConst v = getArray();
116     ImageFileArrayConst vComp = imComp.getArray();
117
118     double myMean = 0.;
119     for (int ix = 0; ix < m_nx; ix++) {
120         for (int iy = 0; iy < m_ny; iy++) {
121             myMean += v[ix][iy];
122         }
123     }
124     myMean /= (m_nx * m_ny);
125
126     double sqErrorSum = 0.;
127     double absErrorSum = 0.;
128     double sqDiffFromMeanSum = 0.;
129     double absValueSum = 0.;
130     for (int ix = 0; ix < m_nx; ix++) {
131         for (int iy = 0; iy < m_ny; iy++) {
132             double diff = v[ix][iy] - vComp[ix][iy];
133             sqErrorSum += diff * diff;
134             absErrorSum += fabs(diff);
135             double diffFromMean = v[ix][iy] - myMean;
136             sqDiffFromMeanSum += diffFromMean * diffFromMean;
137             absValueSum += fabs(v[ix][iy]);
138         }
139     }
140
141     d = sqrt (sqErrorSum / sqDiffFromMeanSum);
142     r = absErrorSum / absValueSum;
143
144     int hx = m_nx / 2;
145     int hy = m_ny / 2;
146     double eMax = -1;
147     for (int ix = 0; ix < hx; ix++) {
148       for (int iy = 0; iy < hy; iy++) {
149         double avgPixel = 0.25 * (v[2*ix][2*iy] + v[2*ix+1][2*iy] + v[2*ix][2*iy+1] + v[2*ix+1][2*iy+1]);
150         double avgPixelComp = 0.25 * (vComp[2*ix][2*iy] + vComp[2*ix+1][2*iy] + vComp[2*ix][2*iy+1] + vComp[2*ix+1][2*iy+1]);
151         double error = fabs (avgPixel - avgPixelComp);
152         if (error > eMax)
153           eMax = error;
154       }
155     }
156
157     e = eMax;
158
159     return true;
160 }
161
162
163 bool
164 ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
165 {
166   double d, r, e;
167
168   if (comparativeStatistics (imComp, d, r, e)) {
169     os << "  Normalized root mean squared distance (d): " << d << endl;
170     os << "      Normalized mean absolute distance (r): " << r << endl;
171     os << "Worst case distance (2x2 pixel average) (e): " << e << endl;
172   }
173 }
174
175
176 void
177 ImageFile::printStatistics (ostream& os) const
178 {
179     double min, max, mean, mode, median, stddev;
180
181     statistics (min, max, mean, mode, median, stddev);
182
183     os << "   min: " << min << endl;
184     os << "   max: " << max << endl;
185     os << "  mean: " << mean << endl;
186     os << "  mode: " << mode << endl;
187     os << "median: " << median << endl;
188     os << "stddev: " << stddev << endl;
189 }
190
191
192 void
193 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
194 {
195     int nx = m_nx;
196     int ny = m_ny;
197     ImageFileArrayConst v = getArray();
198     
199     mean = 0;
200     min = v[0][0];
201     max = v[0][0];
202     for (int ix = 0; ix < nx; ix++) {
203       for (int iy = 0; iy < ny; iy++) {
204         if (v[ix][iy] > max)
205           max = v[ix][iy];
206         if (v[ix][iy] < min)
207           min = v[ix][iy];
208         mean += v[ix][iy];
209       }
210     }
211     mean /= (nx * ny);
212
213     static const int nbin = 256;
214     int hist[ nbin ] = {0};
215     double spread = max - min;
216     mode = 0;
217     stddev = 0;
218     for (int ix = 0; ix < nx; ix++) {
219       for (int iy = 0; iy < ny; iy++) {
220         int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
221         hist[b]++;
222         double diff = (v[ix][iy] - mean);
223         stddev += diff * diff;
224         }
225     }
226     stddev = sqrt(stddev / (nx * ny));
227
228     int max_binindex = 0;
229     int max_bin = -1;
230     for (int ibin = 0; ibin < nbin; ibin++) {
231       if (hist[ibin] > max_bin) {
232         max_bin = hist[ibin];
233         max_binindex = ibin;
234       }
235     }
236
237     mode = (max_binindex * spread / (nbin - 1)) + min;
238       
239     median = 0.;
240 }
241
242