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"
35 #include "vtkComputeHistogram2DOutliers.h"
44 // ----------------------------------------------------------------------------
45 // WX headers inclusion.
46 // For compilers that support precompilation, includes <wx/wx.h>.
47 // ----------------------------------------------------------------------------
54 //----------------------------------------------------------------------------
55 // Class implementation
56 //----------------------------------------------------------------------------
58 //IMPLEMENT_CLASS(pHistogram, wxObject)
60 //----------------------------------------------------------------------------
62 //----------------------------------------------------------------------------
63 pHistogram::pHistogram(std::string filePath)
66 points= vtkImageData::New();
72 pHistogram::pHistogram(vtkImageData* imageData)
74 points= vtkImageData::New();
79 vtkImageCast* cast= vtkImageCast::New();
80 cast->SetInput(imageData);
81 cast->SetOutputScalarTypeToInt();
85 buildHistogram(imageData);
88 pHistogram::~pHistogram()
90 if(points!=NULL)points->Delete();
92 //----------------------------------------------------------------------------
94 //----------------------------------------------------------------------------
96 void pHistogram::setImagePath(std::string filePath)
101 void pHistogram::buildHistogram()
104 reader: is the reader of the image
105 histogramVector:Is the image data of the original image
106 imageData: vtkImageData of the image that we are reading
107 range :it has the (min/max) range of the levels of grey
109 points= vtkImageData::New();
110 vtkMetaImageReader *reader = vtkMetaImageReader::New();
111 vtkImageData* imageData=NULL;
115 reader->SetFileName(path.c_str());
120 imageData=reader->GetOutput();
121 imageData->GetScalarRange(range);
122 initializePoints(size);
123 setPoints(imageData);
128 Calculate the histogram and save it in the attribute points
129 it is used if the user had given the imageData
130 Pre: Size had to be setted if the user wants
131 another value that 100
133 void pHistogram::buildHistogram(vtkImageData* imageData)
135 initializePoints(size);
136 setPoints(imageData);
140 getting ready the points
142 void pHistogram::initializePoints(int xDimension)
144 //setting image data of the points
145 points->SetDimensions(xDimension,1,1);
146 //EED points->SetScalarTypeToUnsignedShort();
147 points->SetScalarTypeToShort();
148 points->AllocateScalars();
153 constructing the histogram
155 void pHistogram::setPoints(vtkImageData* imageData)
158 printf("EED pHistogram::setPoints Trinng new histogram ..\n");
160 printf("EED pHistogram::setPoints Test: vtkPComputeHistogram2DOutliers\n");
161 vtkComputeHistogram2DOutliers *vtkhist = vtkComputeHistogram2DOutliers::New();
162 vtkhist->SetPreferredNumberOfOutliers(10);
163 vtkhist->SetInput(imageData);
164 vtkTable *resulttable = vtkhist->GetOutputTable();
166 printf("EED pHistogram::setPoints rows:%d\n", (int)(resulttable->GetNumberOfRows()) );
167 printf("EED pHistogram::setPoints colums:%d\n", (int)(resulttable->GetNumberOfColumns()) );
173 unsigned short* dataImagePointerUS = NULL;
174 short* dataImagePointerS = NULL;
175 double* dataImagePointerD = NULL;
177 short* dataHistogramPointer = NULL;
179 dataImagePointerUS = (unsigned short*)imageData->GetScalarPointer(0,0,0);
180 dataImagePointerS = (short*)imageData->GetScalarPointer(0,0,0);
181 dataImagePointerD = (double*)imageData->GetScalarPointer(0,0,0);
183 dataHistogramPointer = (short*)points->GetScalarPointer(0,0,0);
192 imageData->GetScalarRange(range);
194 Setting the minimun and maximum levels of grey
196 maxLevelOfGrey=range[1];
197 minLevelOfGrey=range[0];
198 //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
203 imageData->GetExtent(ext);
213 getting ready the histogram
217 dataHistogramPointer[i]=0;
221 Constructing the Histogram
223 //int k=size/(maxLevelOfGrey-minLevelOfGrey);
225 for(i=0;i<sizeImage;i++)
228 hashing the histogram
230 //double p=((float)*dataImagePointer-minLevelOfGrey);
232 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
234 j=getIndex(*dataImagePointerUS);
235 dataImagePointerUS++;
237 if (imageData->GetScalarType()==VTK_SHORT)
239 j=getIndex(*dataImagePointerS);
242 if (imageData->GetScalarType()==VTK_DOUBLE)
244 j=getIndex(*dataImagePointerD);
247 //EED j=getIndex(*dataImagePointer);
249 //std::cout<<j<<std::endl;
250 dataHistogramPointer[j]++;
259 k=dataHistogramPointer[i];
264 Returns the poins of the histograms
266 vtkImageData* pHistogram::getHistogram()
272 hash por getting the index for the histogram vector of the original
274 @gValue: Level of grey for which wants the index in the histogrram
276 int pHistogram::getIndex(double gValue)
279 double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
281 //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
287 void pHistogram::setSize(int nSize)
294 int pHistogram::getImageSize()
299 Get Size of the histogram
301 int pHistogram::getSize()
306 Get the maximum value of grey of the histogram
308 int pHistogram::getMaximumLevelOfGrey()
310 return maxLevelOfGrey;
313 Get the minimum value of grey of the histogram
315 int pHistogram::getMinimumLevelOfGrey()
317 return minLevelOfGrey;
320 get a point of the Histogram
323 int pHistogram::getHistogramPoint(int gValue)
325 //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
328 unsigned short* dataHistogramPointer=NULL;
329 dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
331 return dataHistogramPointer[gValue];