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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
//
// Copyright 1997-2009 Torsten Rohlfing
//
// Copyright 2004-2012 SRI International
//
// This file is part of the Computational Morphometry Toolkit.
//
// http://www.nitrc.org/projects/cmtk/
//
// The Computational Morphometry Toolkit is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// The Computational Morphometry Toolkit is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with the Computational Morphometry Toolkit. If not, see
// <http://www.gnu.org/licenses/>.
//
// $Revision: 5436 $
//
// $LastChangedDate: 2018-12-10 19:01:20 -0800 (Mon, 10 Dec 2018) $
//
// $LastChangedBy: torstenrohlfing $
//
*/
#include <cmtkconfig.h>
#include <System/cmtkConsole.h>
#include <System/cmtkDebugOutput.h>
#include <System/cmtkCommandLine.h>
#include <System/cmtkExitException.h>
#include <IO/cmtkVolumeIO.h>
#include <Base/cmtkHistogram.h>
#include <Base/cmtkVolume.h>
#include <Base/cmtkTypedArray.h>
#include <iostream>
#include <fstream>
#include <list>
#include <stdio.h>
const char *maskFile = NULL;
const char *outFile = NULL;
std::list<const char*> inFileList;
size_t NumberOfBins = 0;
bool NormalizeBins = false;
bool UserMinMaxValue = false;
cmtk::Types::DataItem UserMinValue = 0;
cmtk::Types::DataItem UserMaxValue = 0;
bool Truncate = false;
bool PaddingFlag = false;
bool PaddingValue = 0;
void
ComputeHistogram
( std::list<cmtk::TypedArray::SmartPtr>& dataList, cmtk::Histogram<double>& histogram )
{
const cmtk::Types::DataItemRange range = histogram.GetRange();
std::list<cmtk::TypedArray::SmartPtr>::const_iterator it = dataList.begin();
for ( ; it != dataList.end(); ++it )
{
const cmtk::TypedArray* data = *it;
const size_t size = data->GetDataSize();
cmtk::Types::DataItem value;
for ( size_t offset = 0; offset < size; ++offset )
if ( data->Get( value, offset ) )
{
if ( !Truncate || ((value >= range.m_LowerBound) && (value <= range.m_UpperBound)) )
histogram.Increment( histogram.ValueToBin( value ) );
}
}
if ( NormalizeBins )
{
histogram.NormalizeMaximum( 1.0 );
}
}
void
ComputeHistogram
( std::list<cmtk::TypedArray::SmartPtr>& dataList, const cmtk::TypedArray* mask, const int maskValue, cmtk::Histogram<double>& histogram )
{
const cmtk::Types::DataItemRange range = histogram.GetRange();
std::list<cmtk::TypedArray::SmartPtr>::const_iterator it = dataList.begin();
for ( ; it != dataList.end(); ++it )
{
const cmtk::TypedArray* data = *it;
const size_t size = data->GetDataSize();
cmtk::Types::DataItem value, mv;
for ( size_t offset = 0; offset < size; ++offset )
if ( data->Get( value, offset ) )
if ( !Truncate || ((value >= range.m_LowerBound) && (value <= range.m_UpperBound)) )
if ( mask->Get( mv, offset ) && ( mv == maskValue ) )
histogram.Increment( histogram.ValueToBin( value ) );
}
if ( NormalizeBins )
{
histogram.NormalizeMaximum( 1.0 );
}
}
void
WriteHistogram( const cmtk::Histogram<double>& histogram, const char* outfile )
{
cmtk::Types::DataItem cumulative = 0;
if ( outfile )
{
std::ofstream stream( outfile );
for ( unsigned int bin = 0; bin < histogram.GetNumberOfBins(); ++bin )
{
cumulative += histogram[bin];
stream << bin << "\t"
<< histogram.BinToValue( bin ) << "\t"
<< histogram[bin] << "\t"
<< cumulative << "\n";
}
}
else
{
for ( unsigned int bin = 0; bin < histogram.GetNumberOfBins(); ++bin )
{
cumulative += histogram[bin];
std::cout << bin << "\t"
<< histogram.BinToValue( bin ) << "\t"
<< histogram[bin] << "\t"
<< cumulative << "\n";
}
}
}
int
doMain ( const int argc, const char* argv[] )
{
try
{
cmtk::CommandLine cl;
cl.SetProgramInfo( cmtk::CommandLine::PRG_TITLE, "Image histogram" );
cl.SetProgramInfo( cmtk::CommandLine::PRG_DESCR, "Create a histogram of image intensities and write as tab-separated text file to standard output" );
cl.SetProgramInfo( cmtk::CommandLine::PRG_SYNTX, "histogram [options] image" );
cl.SetProgramInfo( cmtk::CommandLine::PRG_CATEG, "CMTK.Statistics and Modeling" );
typedef cmtk::CommandLine::Key Key;
cl.AddOption( Key( 'n', "nbins" ), &NumberOfBins, "Number of histogram bins (default: number of grey levels)" );
cl.AddSwitch( Key( 'N', "normalize" ), &NormalizeBins, true, "Normalize histogram to maximum 1.0" );
cl.AddOption( Key( "min" ), &UserMinValue, "User-defined minimum value", &UserMinMaxValue );
cl.AddOption( Key( "max" ), &UserMaxValue, "User-defined maximum value", &UserMinMaxValue );
cl.AddSwitch( Key( 't', "truncate" ), &Truncate, true, "Truncate histogram (do not enter values outside range into first/last bin)" );
cl.AddOption( Key( "pad" ), &PaddingValue, "Image padding value", &PaddingFlag );
cl.AddOption( Key( 'o', "outfile" ), &outFile, "File name pattern for histograms" );
cl.AddOption( Key( 'm', "mask" ), &maskFile, "File name for multi-valued mask file" );
cl.Parse( argc, argv );
const char* next = cl.GetNext();
while ( next )
{
inFileList.push_back( next );
next = cl.GetNextOptional();
}
}
catch ( const cmtk::CommandLine::Exception& e )
{
cmtk::StdErr << e;
return 1;
}
std::list<cmtk::TypedArray::SmartPtr> dataList;
cmtk::Types::DataItem minData = 0, maxData = 0;
std::list<const char*>::const_iterator it = inFileList.begin();
for ( ; it != inFileList.end(); ++it )
{
cmtk::Volume::SmartPtr volume( cmtk::VolumeIO::ReadOriented( *it ) );
if ( ! volume )
{
cmtk::StdErr << "Cannot read image " << *it << "\n";
throw cmtk::ExitException( 1 );
}
cmtk::TypedArray::SmartPtr data = volume->GetData();
if ( ! data )
{
cmtk::StdErr << "Cannot read pixel data for image " << *it << "\n";
throw cmtk::ExitException( 1 );
}
if ( PaddingFlag )
{
data->SetPaddingValue( PaddingValue );
}
dataList.push_back( data );
const cmtk::Types::DataItemRange range = data->GetRange();
if ( it == inFileList.begin() )
{
minData = range.m_LowerBound;
maxData = range.m_UpperBound;
}
else
{
minData = std::min( minData, range.m_LowerBound );
maxData = std::max( maxData, range.m_UpperBound );
}
}
size_t bins = NumberOfBins;
if ( !bins )
{
bins = static_cast<int>( maxData - minData + 1 );
if ( bins > 256 ) bins = 256;
}
cmtk::Histogram<double> histogram( bins );
if ( UserMinMaxValue )
{
histogram.SetRange( cmtk::Types::DataItemRange( UserMinValue, UserMaxValue ) );
}
else
{
histogram.SetRange( cmtk::Types::DataItemRange( minData, maxData ) );
}
if ( ! maskFile )
{
histogram.Reset();
ComputeHistogram( dataList, histogram );
WriteHistogram( histogram, outFile );
}
else
{
cmtk::Volume::SmartPtr mask( cmtk::VolumeIO::ReadOriented( maskFile ) );
if ( mask )
{
const cmtk::TypedArray* maskData = mask->GetData();
bool labelFlags[256];
memset( labelFlags, 0, sizeof( labelFlags ) );
for ( size_t i = 0; i < maskData->GetDataSize(); ++i )
{
cmtk::Types::DataItem l;
if ( maskData->Get( l, i ) )
labelFlags[static_cast<byte>( l )] = true;
}
char fname[PATH_MAX];
for ( int mv = 0; mv < 256; ++mv )
{
if ( labelFlags[mv] )
{
histogram.Reset();
ComputeHistogram( dataList, mask->GetData(), mv, histogram );
if ( snprintf( fname, PATH_MAX, outFile, mv ) > PATH_MAX )
{
cmtk::StdErr << "ERROR: output path exceeds maximum path length\n";
}
else
{
WriteHistogram( histogram, fname );
}
}
}
}
}
return 0;
}
#include "cmtkSafeMain"
|