1 /*****************************************************************************
5 ** Purpose: Imagefile classes
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: June 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: imagefile.cpp,v 1.6 2000/06/28 15:25:34 kevin Exp $
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.
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.
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 ******************************************************************************/
33 ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
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);
40 for (int i = -hx; i <= hx; i++) {
41 for (int j = -hy; j <= hy; j++) {
42 double r = sqrt (i * i + j * j);
44 v[i+hx][j+hy] = filter.response (r);
50 ImageFile::display (void)
54 // getPixelValueRange (pmin, pmax);
56 return (displayScaling (1, pmin, pmax));
60 ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax)
65 ImageFileArray v = getArray();
68 int pens [nx * ny * scale * scale ];
70 double view_scale = 255 / (pmax - pmin);
71 int id_X11 = g2_open_X11 (nx * scale, ny * scale);
73 for (int i = 0; i < 256; i++) {
74 double cval = i / 255.;
75 grayscale[i] = g2_ink (id_X11, cval, cval, cval);
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);
85 pens[i++] = grayscale[cval];
89 g2_image (id_X11, 0., 0., nx, ny, pens);
98 // ImageFile::comparativeStatistics Calculate comparative stats
101 // d Normalized root mean squared distance measure
102 // r Normalized mean absolute distance measure
103 // e Worst case distance measure
106 // G.T. Herman, Image Reconstruction From Projections, 1980
109 ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
111 if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
112 sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
115 ImageFileArrayConst v = getArray();
116 ImageFileArrayConst vComp = imComp.getArray();
119 for (int ix = 0; ix < m_nx; ix++) {
120 for (int iy = 0; iy < m_ny; iy++) {
124 myMean /= (m_nx * m_ny);
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]);
141 d = sqrt (sqErrorSum / sqDiffFromMeanSum);
142 r = absErrorSum / absValueSum;
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);
164 ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
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;
177 ImageFile::printStatistics (ostream& os) const
179 double min, max, mean, mode, median, stddev;
181 statistics (min, max, mean, mode, median, stddev);
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;
193 ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
197 ImageFileArrayConst v = getArray();
202 for (int ix = 0; ix < nx; ix++) {
203 for (int iy = 0; iy < ny; iy++) {
213 static const int nbin = 256;
214 int hist[ nbin ] = {0};
215 double spread = max - min;
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);
222 double diff = (v[ix][iy] - mean);
223 stddev += diff * diff;
226 stddev = sqrt(stddev / (nx * ny));
228 int max_binindex = 0;
230 for (int ibin = 0; ibin < nbin; ibin++) {
231 if (hist[ibin] > max_bin) {
232 max_bin = hist[ibin];
237 mode = (max_binindex * spread / (nbin - 1)) + min;