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         //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
00153         /*
00154          Image Size
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           getting ready the histogram
00168         */
00169         for(i=0;i<size;i++)
00170         {
00171                 dataHistogramPointer[i]=0;
00172         }
00173         
00174         /*
00175         Constructing the Histogram
00176         */
00177         //int k=size/(maxLevelOfGrey-minLevelOfGrey);
00178         int j=0;
00179         for(i=0;i<sizeImage;i++)
00180         {
00181                 /*
00182                  hashing the histogram
00183                 */
00184                 //double p=((float)*dataImagePointer-minLevelOfGrey);
00185                 //j=p*k;
00186                 
00187                 j=getIndex(*dataImagePointer);
00188                 //std::cout<<j<<std::endl;
00189                 dataHistogramPointer[j]++;
00190                 dataImagePointer++;
00191         }
00192         /*
00193         BORRAME
00194         */
00195         /*
00196         k=0;
00197         for(i=0;i<size;i++)
00198         {
00199                 k=dataHistogramPointer[i];
00200         }
00201         */
00202 }
00203 /*
00204 Returns the poins of the histograms
00205 */
00206 vtkImageData* pHistogram::getHistogram()
00207 {
00208         return points;
00209 }
00210 
00211 /*
00212 hash por getting the index for the histogram vector of the original
00213 image
00214 @gValue: Level of grey for which wants the index in the histogrram
00215 */
00216 int pHistogram::getIndex(int gValue)
00217 {
00218 
00219         double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
00220         double k=p*(size-1);
00221         //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
00222         return (int)k;
00223 }
00224 /*
00225 Setting the size
00226 */
00227 void pHistogram::setSize(int nSize)
00228 {
00229         size=nSize;
00230 }
00231 /*
00232         Get Image Size
00233 */
00234 int pHistogram::getImageSize()
00235 { 
00236         return sizeImage;
00237 }
00238 /*
00239         Get Size of the histogram
00240 */
00241 int pHistogram::getSize()
00242 {
00243         return size;
00244 }
00245 /*
00246         Get the maximum value of grey of the histogram
00247 */
00248 int pHistogram::getMaximumLevelOfGrey()
00249 {
00250         return maxLevelOfGrey;
00251 }
00252 /*
00253         Get the minimum value of grey of the histogram
00254 */
00255 int pHistogram::getMinimumLevelOfGrey()
00256 {
00257         return minLevelOfGrey;
00258 }
00259 /*
00260                 get a point of the Histogram
00261                 given the grey value
00262 */
00263 int pHistogram::getHistogramPoint(int gValue)
00264 {
00265         //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
00266         //double k=p*size;
00267 
00268         unsigned short* dataHistogramPointer=NULL;
00269         dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
00270 
00271         return dataHistogramPointer[gValue];
00272 }
00273 
00274 

Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1