X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsim%2Fimagefile.cpp;h=a05eae73f81115c6d4dc05c754fea5caf53ed08a;hp=8756bc88317082473d7385d5813e980077aa04b6;hb=2f3d6e2580db607105bb072b13e4aff453ae4495;hpb=99dd1d6ed10db1f669a5fe6af71225a50fc0ddfb diff --git a/libctsim/imagefile.cpp b/libctsim/imagefile.cpp index 8756bc8..a05eae7 100644 --- a/libctsim/imagefile.cpp +++ b/libctsim/imagefile.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: imagefile.cpp,v 1.1 2000/06/19 02:59:34 kevin Exp $ +** $Id: imagefile.cpp,v 1.5 2000/06/26 21:15:24 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 @@ -28,116 +28,44 @@ #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 - * - * PROGRAMMER: Kevin Rosenberg - * DATE: Aug 1984 - * - * FUNCTION - * Provides a set of routines for handling image files - */ - void -image_filter_response (ImageFile& im, const DomainType domain, double bw, const FilterType filt_type, 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); - if (domain == D_SPATIAL) - v[i+hx][j+hy] = filter_spatial_response_analytic (filt_type, r, bw, filt_param); - else - v[i+hx][j+hy] = filter_frequency_response (filt_type, r, bw, 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 = new int [nx * ny * scale * scale ]; + int pens [nx * ny * scale * scale ]; double view_scale = 255 / (pmax - pmin); int id_X11 = g2_open_X11 (nx * scale, ny * scale); @@ -149,7 +77,7 @@ int image_display_scale (const ImageFile& im, const int scale, const double pmin for (int i= 0, iy = ny - 1; iy >= 0; iy--) { for (int ix = 0; ix < nx; ix++) { - int cval = (int) ((v[ix][iy] - pmin) * view_scale); + int cval = static_cast((v[ix][iy] - pmin) * view_scale); if (cval < 0) cval = 0; else if (cval > 255) @@ -160,7 +88,6 @@ int image_display_scale (const ImageFile& im, const int scale, const double pmin g2_image (id_X11, 0., 0., nx, ny, pens); - delete [] pens; return (id_X11); #endif @@ -168,4 +95,148 @@ int image_display_scale (const ImageFile& im, const int scale, const double pmin +// 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; + } + const ImageFileArray const v = getArray(); + ImageFileArray 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; + const ImageFileArray 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((((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.; +} +