pHistogram.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #include "pHistogram.h"
00007
00008 #include "vtkImageData.h"
00009 #include "vtkImageCast.h"
00010
00011
00012
00013 #include <iostream>
00014 #include <fstream>
00015 #include <string>
00016 #include <vector>
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 pHistogram::pHistogram(std::string filePath)
00038 {
00039 path=filePath;
00040 points= vtkImageData::New();
00041 size=100;
00042 sizeImage=0;
00043 buildHistogram();
00044 }
00045
00046 pHistogram::pHistogram(vtkImageData* imageData)
00047 {
00048 points= vtkImageData::New();
00049 size=100;
00050 sizeImage=0;
00051
00052
00053
00054
00055
00056
00057
00058
00059 buildHistogram(imageData);
00060 }
00061
00062 pHistogram::~pHistogram()
00063 {
00064 if(points!=NULL)points->Delete();
00065 }
00066
00067
00068
00069
00070 void pHistogram::setImagePath(std::string filePath)
00071 {
00072 path=filePath;
00073 }
00074
00075 void pHistogram::buildHistogram()
00076 {
00077
00078
00079
00080
00081
00082
00083 points= vtkImageData::New();
00084 vtkMetaImageReader *reader = vtkMetaImageReader::New();
00085 vtkImageData* imageData=NULL;
00086 double range[2];
00087
00088
00089 reader->SetFileName(path.c_str());
00090 reader->Update();
00091
00092
00093
00094 imageData=reader->GetOutput();
00095 imageData->GetScalarRange(range);
00096 initializePoints(size);
00097 setPoints(imageData);
00098
00099 }
00100
00101
00102
00103
00104
00105
00106
00107 void pHistogram::buildHistogram(vtkImageData* imageData)
00108 {
00109 initializePoints(size);
00110 setPoints(imageData);
00111 }
00112
00113
00114
00115
00116 void pHistogram::initializePoints(int xDimension)
00117 {
00118
00119 points->SetDimensions(xDimension,1,1);
00120 points->SetScalarTypeToUnsignedShort();
00121 points->AllocateScalars();
00122 points->Update();
00123 }
00124
00125
00126
00127
00128 void pHistogram::setPoints(vtkImageData* imageData)
00129 {
00130
00131
00132
00133 unsigned short* dataImagePointer=NULL;
00134 unsigned short* dataHistogramPointer=NULL;
00135
00136 dataImagePointer=(unsigned short*)imageData->GetScalarPointer(0,0,0);
00137 dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
00138
00139
00140
00141
00142 double range[2];
00143 if(imageData==NULL)
00144 range[1]=1;
00145 else
00146 imageData->GetScalarRange(range);
00147
00148
00149
00150 maxLevelOfGrey=(int)range[1];
00151 minLevelOfGrey=(int)range[0];
00152
00153
00154
00155
00156 int ext[6];
00157 imageData->GetExtent(ext);
00158 int sx,sy,sz;
00159 sx=ext[1]+1;
00160 sy=ext[3]+1;
00161 sz=ext[5]+1;
00162
00163 sizeImage=sx*sy*sz;
00164
00165 int i;
00166
00167
00168
00169 for(i=0;i<size;i++)
00170 {
00171 dataHistogramPointer[i]=0;
00172 }
00173
00174
00175
00176
00177
00178 int j=0;
00179 for(i=0;i<sizeImage;i++)
00180 {
00181
00182
00183
00184
00185
00186
00187 j=getIndex(*dataImagePointer);
00188
00189 dataHistogramPointer[j]++;
00190 dataImagePointer++;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 }
00203
00204
00205
00206 vtkImageData* pHistogram::getHistogram()
00207 {
00208 return points;
00209 }
00210
00211
00212
00213
00214
00215
00216 int pHistogram::getIndex(int gValue)
00217 {
00218
00219 double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
00220 double k=p*(size-1);
00221
00222 return (int)k;
00223 }
00224
00225
00226
00227 void pHistogram::setSize(int nSize)
00228 {
00229 size=nSize;
00230 }
00231
00232
00233
00234 int pHistogram::getImageSize()
00235 {
00236 return sizeImage;
00237 }
00238
00239
00240
00241 int pHistogram::getSize()
00242 {
00243 return size;
00244 }
00245
00246
00247
00248 int pHistogram::getMaximumLevelOfGrey()
00249 {
00250 return maxLevelOfGrey;
00251 }
00252
00253
00254
00255 int pHistogram::getMinimumLevelOfGrey()
00256 {
00257 return minLevelOfGrey;
00258 }
00259
00260
00261
00262
00263 int pHistogram::getHistogramPoint(int gValue)
00264 {
00265
00266
00267
00268 unsigned short* dataHistogramPointer=NULL;
00269 dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
00270
00271 return dataHistogramPointer[gValue];
00272 }
00273
00274