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 int ext[6];
00156 imageData->GetExtent(ext);
00157 int sx,sy,sz;
00158 sx=ext[1]+1;
00159 sy=ext[3]+1;
00160 sz=ext[5]+1;
00161
00162 sizeImage=sx*sy*sz;
00163
00164 int i;
00165
00166
00167
00168 for(i=0;i<size;i++)
00169 {
00170 dataHistogramPointer[i]=0;
00171 }
00172
00173
00174
00175
00176
00177 int j=0;
00178 for(i=0;i<sizeImage;i++)
00179 {
00180
00181
00182
00183
00184
00185
00186 j=getIndex(*dataImagePointer);
00187 dataHistogramPointer[j]++;
00188 dataImagePointer++;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 }
00201
00202
00203
00204 vtkImageData* pHistogram::getHistogram()
00205 {
00206 return points;
00207 }
00208
00209
00210
00211
00212
00213
00214 int pHistogram::getIndex(int gValue)
00215 {
00216 double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
00217 double k=p*(size-1);
00218 return (int)k;
00219 }
00220
00221
00222
00223 void pHistogram::setSize(int nSize)
00224 {
00225 size=nSize;
00226 }
00227
00228
00229
00230 int pHistogram::getImageSize()
00231 {
00232 return sizeImage;
00233 }
00234
00235
00236
00237 int pHistogram::getSize()
00238 {
00239 return size;
00240 }
00241
00242
00243
00244 int pHistogram::getMaximumLevelOfGrey()
00245 {
00246 return maxLevelOfGrey;
00247 }
00248
00249
00250
00251 int pHistogram::getMinimumLevelOfGrey()
00252 {
00253 return minLevelOfGrey;
00254 }
00255
00256
00257
00258
00259 int pHistogram::getHistogramPoint(int gValue)
00260 {
00261
00262
00263
00264 unsigned short* dataHistogramPointer=NULL;
00265 dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
00266
00267 return dataHistogramPointer[gValue];
00268 }
00269
00270