** This is part of the CTSim program
** Copyright (C) 1983-2000 Kevin Rosenberg
**
-** $Id: imagefile.cpp,v 1.3 2000/06/22 10:17:28 kevin Exp $
+** $Id: imagefile.cpp,v 1.6 2000/06/28 15:25:34 kevin Exp $
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License (version 2) as
#include "ct.h"
-///////////////////////////////////////////////////////////////////////////
-// CLASS IMPLEMENTATION
-//
-// Name: Array2dFileLabel
-// Purpose: Labels for Array2dFiles
-///////////////////////////////////////////////////////////////////////////
-
-void
-Array2dFileLabel::init (void)
-{
- m_calcTime = 0;
- m_labelType = L_EMPTY;
- TIMEDATE td;
- td_get_tmdt (&td);
- m_year = td.d.year;
- m_month = td.d.month;
- m_day = td.d.date;
- m_hour = td.t.hour;
- m_minute = td.t.minute;
- m_second = td.t.second;
-}
-
-Array2dFileLabel::Array2dFileLabel()
-{
- init();
-}
-
-Array2dFileLabel::Array2dFileLabel(const char* const str, double ctime = 0.)
- : m_strLabel (str)
-{
- init();
-
- m_labelType = L_USER;
- m_calcTime = ctime;
-}
-
-Array2dFileLabel::Array2dFileLabel(const int type, const char* const str, double ctime = 0.)
- : m_strLabel (str)
-{
- init();
-
- m_labelType = type;
- m_calcTime = ctime;
-}
-
-Array2dFileLabel::~Array2dFileLabel()
-{
-}
-
-const string&
-Array2dFileLabel::getDateString (void) const
-{
- ostringstream oss;
- oss << m_month <<"/"<< m_day <<"/"<< m_year << " " << m_hour <<":"<< m_minute <<":"<< m_second;
- m_strDate = oss.str();
- return m_strDate;
-}
-
-
-/* FILE
- * image.c Routines for managing images
- */
-
void
-image_filter_response (ImageFile& im, const char* const domainName, double bw, const char* const filterName, double filt_param, const int opt_trace)
+ImageFile::filterResponse (const char* const domainName, double bw, const char* const filterName, double filt_param)
{
- int hx = im.nx() / 2;
- int hy = im.ny() / 2;
- ImageFileArray v = im.getArray();
+ int hx = (m_nx - 1) / 2;
+ int hy = (m_ny - 1) / 2;
+ ImageFileArray v = getArray();
+ SignalFilter filter (filterName, domainName, bw, filt_param);
for (int i = -hx; i <= hx; i++) {
for (int j = -hy; j <= hy; j++) {
- double r = sqrt(i * i + j * j);
+ double r = sqrt (i * i + j * j);
- v[i+hx][j+hy] = SignalFilter::response (filterName, domainName, bw, r, filt_param);
- if (opt_trace >= TRACE_PHM)
- printf ("r=%8.4f, v=%8.4f\n", r, v[i+hx][j+hy]);
+ v[i+hx][j+hy] = filter.response (r);
}
}
}
int
-image_display (const ImageFile& im)
+ImageFile::display (void)
{
- ImageFileValue pmin, pmax;
+ double pmin, pmax;
- im.getPixelValueRange (pmin, pmax);
+ // getPixelValueRange (pmin, pmax);
- return (image_display_scale (im, 1, pmin, pmax));
+ return (displayScaling (1, pmin, pmax));
}
-int image_display_scale (const ImageFile& im, const int scale, const double pmin, const double pmax)
+int
+ImageFile::displayScaling (const int scale, const ImageFileValue pmin, const ImageFileValue pmax)
{
int grayscale[256];
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArray v = getArray();
#if HAVE_G2_H
int pens [nx * ny * scale * scale ];
+// ImageFile::comparativeStatistics Calculate comparative stats
+//
+// OUTPUT
+// d Normalized root mean squared distance measure
+// r Normalized mean absolute distance measure
+// e Worst case distance measure
+//
+// REFERENCES
+// G.T. Herman, Image Reconstruction From Projections, 1980
+
+bool
+ImageFile::comparativeStatistics (const ImageFile& imComp, double& d, double& r, double& e) const
+{
+ if (imComp.nx() != m_nx && imComp.ny() != m_ny) {
+ sys_error (ERR_WARNING, "Image sizes differ [ImageFile::comparativeStatistics]");
+ return false;
+ }
+ ImageFileArrayConst v = getArray();
+ ImageFileArrayConst vComp = imComp.getArray();
+
+ double myMean = 0.;
+ for (int ix = 0; ix < m_nx; ix++) {
+ for (int iy = 0; iy < m_ny; iy++) {
+ myMean += v[ix][iy];
+ }
+ }
+ myMean /= (m_nx * m_ny);
+
+ double sqErrorSum = 0.;
+ double absErrorSum = 0.;
+ double sqDiffFromMeanSum = 0.;
+ double absValueSum = 0.;
+ for (int ix = 0; ix < m_nx; ix++) {
+ for (int iy = 0; iy < m_ny; iy++) {
+ double diff = v[ix][iy] - vComp[ix][iy];
+ sqErrorSum += diff * diff;
+ absErrorSum += fabs(diff);
+ double diffFromMean = v[ix][iy] - myMean;
+ sqDiffFromMeanSum += diffFromMean * diffFromMean;
+ absValueSum += fabs(v[ix][iy]);
+ }
+ }
+
+ d = sqrt (sqErrorSum / sqDiffFromMeanSum);
+ r = absErrorSum / absValueSum;
+
+ int hx = m_nx / 2;
+ int hy = m_ny / 2;
+ double eMax = -1;
+ for (int ix = 0; ix < hx; ix++) {
+ for (int iy = 0; iy < hy; iy++) {
+ 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]);
+ 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]);
+ double error = fabs (avgPixel - avgPixelComp);
+ if (error > eMax)
+ eMax = error;
+ }
+ }
+
+ e = eMax;
+
+ return true;
+}
+
+
+bool
+ImageFile::printComparativeStatistics (const ImageFile& imComp, ostream& os) const
+{
+ double d, r, e;
+
+ if (comparativeStatistics (imComp, d, r, e)) {
+ os << " Normalized root mean squared distance (d): " << d << endl;
+ os << " Normalized mean absolute distance (r): " << r << endl;
+ os << "Worst case distance (2x2 pixel average) (e): " << e << endl;
+ }
+}
+
+
+void
+ImageFile::printStatistics (ostream& os) const
+{
+ double min, max, mean, mode, median, stddev;
+
+ statistics (min, max, mean, mode, median, stddev);
+
+ os << " min: " << min << endl;
+ os << " max: " << max << endl;
+ os << " mean: " << mean << endl;
+ os << " mode: " << mode << endl;
+ os << "median: " << median << endl;
+ os << "stddev: " << stddev << endl;
+}
+
+
+void
+ImageFile::statistics (double& min, double& max, double& mean, double& mode, double& median, double& stddev) const
+{
+ int nx = m_nx;
+ int ny = m_ny;
+ ImageFileArrayConst v = getArray();
+
+ mean = 0;
+ min = v[0][0];
+ max = v[0][0];
+ for (int ix = 0; ix < nx; ix++) {
+ for (int iy = 0; iy < ny; iy++) {
+ if (v[ix][iy] > max)
+ max = v[ix][iy];
+ if (v[ix][iy] < min)
+ min = v[ix][iy];
+ mean += v[ix][iy];
+ }
+ }
+ mean /= (nx * ny);
+
+ static const int nbin = 256;
+ int hist[ nbin ] = {0};
+ double spread = max - min;
+ mode = 0;
+ stddev = 0;
+ for (int ix = 0; ix < nx; ix++) {
+ for (int iy = 0; iy < ny; iy++) {
+ int b = static_cast<int>((((v[ix][iy] - min) / spread) * (nbin - 1)) + 0.5);
+ hist[b]++;
+ double diff = (v[ix][iy] - mean);
+ stddev += diff * diff;
+ }
+ }
+ stddev = sqrt(stddev / (nx * ny));
+
+ int max_binindex = 0;
+ int max_bin = -1;
+ for (int ibin = 0; ibin < nbin; ibin++) {
+ if (hist[ibin] > max_bin) {
+ max_bin = hist[ibin];
+ max_binindex = ibin;
+ }
+ }
+
+ mode = (max_binindex * spread / (nbin - 1)) + min;
+
+ median = 0.;
+}
+