1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
27 //----------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------
31 #include "pHistogram.h"
32 //#include "vtkJPEGReader.h"
33 #include "vtkImageData.h"
34 #include "vtkImageCast.h"
43 // ----------------------------------------------------------------------------
44 // WX headers inclusion.
45 // For compilers that support precompilation, includes <wx/wx.h>.
46 // ----------------------------------------------------------------------------
53 //----------------------------------------------------------------------------
54 // Class implementation
55 //----------------------------------------------------------------------------
57 //IMPLEMENT_CLASS(pHistogram, wxObject)
59 //----------------------------------------------------------------------------
61 //----------------------------------------------------------------------------
62 pHistogram::pHistogram(std::string filePath)
65 points= vtkImageData::New();
71 pHistogram::pHistogram(vtkImageData* imageData)
73 points= vtkImageData::New();
78 vtkImageCast* cast= vtkImageCast::New();
79 cast->SetInput(imageData);
80 cast->SetOutputScalarTypeToInt();
84 buildHistogram(imageData);
87 pHistogram::~pHistogram()
89 if(points!=NULL)points->Delete();
91 //----------------------------------------------------------------------------
93 //----------------------------------------------------------------------------
95 void pHistogram::setImagePath(std::string filePath)
100 void pHistogram::buildHistogram()
103 reader: is the reader of the image
104 histogramVector:Is the image data of the original image
105 imageData: vtkImageData of the image that we are reading
106 range :it has the (min/max) range of the levels of grey
108 points= vtkImageData::New();
109 vtkMetaImageReader *reader = vtkMetaImageReader::New();
110 vtkImageData* imageData=NULL;
114 reader->SetFileName(path.c_str());
119 imageData=reader->GetOutput();
120 imageData->GetScalarRange(range);
121 initializePoints(size);
122 setPoints(imageData);
127 Calculate the histogram and save it in the attribute points
128 it is used if the user had given the imageData
129 Pre: Size had to be setted if the user wants
130 another value that 100
132 void pHistogram::buildHistogram(vtkImageData* imageData)
134 initializePoints(size);
135 setPoints(imageData);
139 getting ready the points
141 void pHistogram::initializePoints(int xDimension)
143 //setting image data of the points
144 points->SetDimensions(xDimension,1,1);
145 //EED points->SetScalarTypeToUnsignedShort();
146 points->SetScalarTypeToShort();
147 points->AllocateScalars();
152 constructing the histogram
154 void pHistogram::setPoints(vtkImageData* imageData)
159 unsigned short* dataImagePointerUS = NULL;
160 short* dataImagePointerS = NULL;
161 double* dataImagePointerD = NULL;
163 short* dataHistogramPointer = NULL;
165 dataImagePointerUS = (unsigned short*)imageData->GetScalarPointer(0,0,0);
166 dataImagePointerS = (short*)imageData->GetScalarPointer(0,0,0);
167 dataImagePointerD = (double*)imageData->GetScalarPointer(0,0,0);
169 dataHistogramPointer = (short*)points->GetScalarPointer(0,0,0);
178 imageData->GetScalarRange(range);
180 Setting the minimun and maximum levels of grey
182 maxLevelOfGrey=range[1];
183 minLevelOfGrey=range[0];
184 //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
189 imageData->GetExtent(ext);
199 getting ready the histogram
203 dataHistogramPointer[i]=0;
207 Constructing the Histogram
209 //int k=size/(maxLevelOfGrey-minLevelOfGrey);
211 for(i=0;i<sizeImage;i++)
214 hashing the histogram
216 //double p=((float)*dataImagePointer-minLevelOfGrey);
218 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
220 j=getIndex(*dataImagePointerUS);
221 dataImagePointerUS++;
223 if (imageData->GetScalarType()==VTK_SHORT)
225 j=getIndex(*dataImagePointerS);
228 if (imageData->GetScalarType()==VTK_DOUBLE)
230 j=getIndex(*dataImagePointerD);
233 //EED j=getIndex(*dataImagePointer);
235 //std::cout<<j<<std::endl;
236 dataHistogramPointer[j]++;
245 k=dataHistogramPointer[i];
250 Returns the poins of the histograms
252 vtkImageData* pHistogram::getHistogram()
258 hash por getting the index for the histogram vector of the original
260 @gValue: Level of grey for which wants the index in the histogrram
262 int pHistogram::getIndex(double gValue)
265 double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
267 //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
273 void pHistogram::setSize(int nSize)
280 int pHistogram::getImageSize()
285 Get Size of the histogram
287 int pHistogram::getSize()
292 Get the maximum value of grey of the histogram
294 int pHistogram::getMaximumLevelOfGrey()
296 return maxLevelOfGrey;
299 Get the minimum value of grey of the histogram
301 int pHistogram::getMinimumLevelOfGrey()
303 return minLevelOfGrey;
306 get a point of the Histogram
309 int pHistogram::getHistogramPoint(int gValue)
311 //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
314 unsigned short* dataHistogramPointer=NULL;
315 dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
317 return dataHistogramPointer[gValue];