pHistogram.cxx

Go to the documentation of this file.
00001 
00002 //----------------------------------------------------------------------------
00003 // Class definition include
00004 //----------------------------------------------------------------------------
00005 
00006 #include "pHistogram.h"
00007 //#include "vtkJPEGReader.h"
00008 #include "vtkImageData.h"
00009 #include  "vtkImageCast.h"
00010 //-----------------
00011 // C++
00012 //-----------------
00013 #include <iostream>
00014 #include <fstream>
00015 #include <string>
00016 #include <vector>
00017 
00018 // ----------------------------------------------------------------------------
00019 // WX headers inclusion.
00020 // For compilers that support precompilation, includes <wx/wx.h>.
00021 // ----------------------------------------------------------------------------
00022 
00023 /*
00024 #ifndef WX_PRECOMP
00025 #include <wx/wx.h>
00026 #endif
00027 */
00028 //----------------------------------------------------------------------------
00029 // Class implementation
00030 //----------------------------------------------------------------------------
00031 
00032 //IMPLEMENT_CLASS(pHistogram, wxObject)
00033 
00034 //----------------------------------------------------------------------------
00035 // Constructors
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         //cast
00052         /*
00053         vtkImageCast* cast= vtkImageCast::New();
00054         cast->SetInput(imageData);
00055         cast->SetOutputScalarTypeToInt();
00056         cast->Update();
00057         */
00058         //build the histogram
00059         buildHistogram(imageData);
00060 }
00061 
00062 pHistogram::~pHistogram()
00063 {
00064         if(points!=NULL)points->Delete();
00065 }
00066 //----------------------------------------------------------------------------
00067 // Methods
00068 //----------------------------------------------------------------------------
00069 
00070 void pHistogram::setImagePath(std::string filePath)
00071 {
00072         path=filePath;
00073 }
00074 
00075 void pHistogram::buildHistogram()
00076 {
00077         /*
00078                 reader: is the reader of the image
00079                 histogramVector:Is the image data of the original image
00080                 imageData: vtkImageData of the image that we are reading
00081                 range :it has the (min/max) range of the levels of grey
00082         */
00083         points= vtkImageData::New();
00084         vtkMetaImageReader *reader = vtkMetaImageReader::New();
00085         vtkImageData* imageData=NULL;
00086         double range[2];
00087         
00088         //reading
00089         reader->SetFileName(path.c_str());
00090         reader->Update();
00091 
00092         
00093         // getting the data 
00094         imageData=reader->GetOutput();
00095         imageData->GetScalarRange(range);
00096         initializePoints(size);
00097         setPoints(imageData);
00098         
00099 }
00100 
00101 /*
00102 Calculate the histogram and save it in the attribute points
00103 it is used if the user had given the imageData
00104 Pre: Size had to be setted if the user wants
00105 another value that 100
00106 */
00107 void pHistogram::buildHistogram(vtkImageData* imageData)
00108 {
00109         initializePoints(size);
00110         setPoints(imageData);   
00111 }
00112 
00113 /*
00114   getting ready the points
00115 */
00116 void pHistogram::initializePoints(int xDimension)
00117 {
00118         //setting image data of the points
00119         points->SetDimensions(xDimension,1,1);
00120         points->SetScalarTypeToUnsignedShort();
00121         points->AllocateScalars();
00122         points->Update();
00123 }
00124 
00125 /*
00126          constructing the histogram
00127 */
00128 void pHistogram::setPoints(vtkImageData* imageData)
00129 {
00130         /*
00131                 Pointers
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          Range of greys
00141         */
00142         double range[2];
00143         if(imageData==NULL)
00144                 range[1]=1;
00145         else
00146                 imageData->GetScalarRange(range);
00147         /*
00148          Setting the  minimun and maximum levels of grey
00149         */
00150         maxLevelOfGrey=(int)range[1];
00151         minLevelOfGrey=(int)range[0];
00152         /*
00153          Image Size
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           getting ready the histogram
00167         */
00168         for(i=0;i<size;i++)
00169         {
00170                 dataHistogramPointer[i]=0;
00171         }
00172         
00173         /*
00174         Constructing the Histogram
00175         */
00176         //int k=size/(maxLevelOfGrey-minLevelOfGrey);
00177         int j=0;
00178         for(i=0;i<sizeImage;i++)
00179         {
00180                 /*
00181                  hashing the histogram
00182                 */
00183                 //double p=((float)*dataImagePointer-minLevelOfGrey);
00184                 //j=p*k;
00185                 
00186                 j=getIndex(*dataImagePointer);
00187                 dataHistogramPointer[j]++;
00188                 dataImagePointer++;
00189         }
00190         /*
00191         BORRAME
00192         */
00193         /*
00194         k=0;
00195         for(i=0;i<size;i++)
00196         {
00197                 k=dataHistogramPointer[i];
00198         }
00199         */
00200 }
00201 /*
00202 Returns the poins of the histograms
00203 */
00204 vtkImageData* pHistogram::getHistogram()
00205 {
00206         return points;
00207 }
00208 
00209 /*
00210 hash por getting the index for the histogram vector of the original
00211 image
00212 @gValue: Level of grey for which wants the index in the histogrram
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 Setting the size
00222 */
00223 void pHistogram::setSize(int nSize)
00224 {
00225         size=nSize;
00226 }
00227 /*
00228         Get Image Size
00229 */
00230 int pHistogram::getImageSize()
00231 { 
00232         return sizeImage;
00233 }
00234 /*
00235         Get Size of the histogram
00236 */
00237 int pHistogram::getSize()
00238 {
00239         return size;
00240 }
00241 /*
00242         Get the maximum value of grey of the histogram
00243 */
00244 int pHistogram::getMaximumLevelOfGrey()
00245 {
00246         return maxLevelOfGrey;
00247 }
00248 /*
00249         Get the minimum value of grey of the histogram
00250 */
00251 int pHistogram::getMinimumLevelOfGrey()
00252 {
00253         return minLevelOfGrey;
00254 }
00255 /*
00256                 get a point of the Histogram
00257                 given the grey value
00258 */
00259 int pHistogram::getHistogramPoint(int gValue)
00260 {
00261         //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
00262         //double k=p*size;
00263 
00264         unsigned short* dataHistogramPointer=NULL;
00265         dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
00266 
00267         return dataHistogramPointer[gValue];
00268 }
00269 
00270 

Generated on Fri Jun 12 00:08:33 2009 for creaMaracasVisu by  doxygen 1.5.7.1