1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
/*=========================================================================
Program: Visualization Toolkit
Module: ImageHistogramStatistics.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// Test the vtkImageHistogramStatistics class
//
// The command line arguments are:
// -I => run in interactive mode
#include "vtkPNGReader.h"
#include "vtkImageCast.h"
#include "vtkImageHistogramStatistics.h"
#include "vtkImageAccumulate.h"
#include "vtkTestUtilities.h"
#include <math.h>
int ImageHistogramStatistics(int argc, char *argv[])
{
vtkPNGReader *reader = vtkPNGReader::New();
char* fname = vtkTestUtilities::ExpandDataFileName(
argc, argv, "Data/fullhead15.png");
reader->SetFileName(fname);
delete[] fname;
// Use float data to get the most code coverage
vtkImageCast *imageCast = vtkImageCast::New();
imageCast->SetOutputScalarTypeToFloat();
imageCast->SetInputConnection(reader->GetOutputPort());
double minValTest = 0;
double maxValTest = 3714;
double meanValTest = 635.8066572717137;
double medianTest = 190.9279926756695;
double stdevTest = 660.9126299774935;
double tol = 1e-6;
vtkImageHistogramStatistics *statistics = vtkImageHistogramStatistics::New();
statistics->SetInputConnection(imageCast->GetOutputPort());
statistics->GenerateHistogramImageOff();
statistics->Update();
double minVal = statistics->GetMinimum();
double maxVal = statistics->GetMaximum();
double meanVal = statistics->GetMean();
double median = statistics->GetMedian();
double stdev = statistics->GetStandardDeviation();
// uncomment to test vtkImageAccumulate instead
/*
vtkImageAccumulate *accumulate = vtkImageAccumulate::New();
accumulate->SetInputConnection(reader->GetOutputPort());
accumulate->Update();
double minVal = accumulate->GetMin()[0];
double maxVal = accumulate->GetMax()[0];
double meanVal = accumulate->GetMean()[0];
double median = medianTest;
double stdev = accumulate->GetStandardDeviation()[0];
*/
bool retVal = true;
if (fabs((minVal - minValTest)/maxValTest) > tol)
{
cout.precision(16);
cout << "minVal " << minVal << " should be " << minValTest << endl;
retVal = false;
}
if (fabs((maxVal - maxValTest)/maxValTest) > tol)
{
cout.precision(16);
cout << "maxVal " << maxVal << " should be " << maxValTest << endl;
retVal = false;
}
if (fabs((meanVal - meanValTest)/maxValTest) > tol)
{
cout.precision(16);
cout << "meanVal " << meanVal << " should be " << meanValTest << endl;
retVal = false;
}
if (fabs((median - medianTest)/maxValTest) > tol)
{
cout.precision(16);
cout << "median " << median << " should be " << medianTest << endl;
retVal = false;
}
if (fabs((stdev - stdevTest)/maxValTest) > tol)
{
cout.precision(16);
cout << "stdev " << stdev << " should be " << stdevTest << endl;
retVal = false;
}
reader->Delete();
imageCast->Delete();
statistics->Delete();
return !retVal;
}
|