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();
77 buildHistogram(imageData);
80 //----------------------------------------------------------------------------
81 pHistogram::~pHistogram()
83 if(points!=NULL)points->Delete();
86 //----------------------------------------------------------------------------
87 void pHistogram::setImagePath(std::string filePath)
92 //----------------------------------------------------------------------------
93 void pHistogram::buildHistogram()
96 reader: is the reader of the image
97 histogramVector:Is the image data of the original image
98 imageData: vtkImageData of the image that we are reading
99 range :it has the (min/max) range of the levels of grey
101 points= vtkImageData::New();
102 vtkMetaImageReader *reader = vtkMetaImageReader::New();
103 vtkImageData* imageData=NULL;
107 reader->SetFileName(path.c_str());
112 imageData=reader->GetOutput();
113 imageData->GetScalarRange(range);
114 initializePoints(size);
115 setPoints(imageData);
119 Calculate the histogram and save it in the attribute points
120 it is used if the user had given the imageData
121 Pre: Size had to be setted if the user wants
122 another value that 100
124 void pHistogram::buildHistogram(vtkImageData* imageData)
126 initializePoints(size);
127 setPoints(imageData);
131 getting ready the points
133 void pHistogram::initializePoints(int xDimension)
135 //setting image data of the points
136 points->SetDimensions(xDimension,1,1);
137 //EED points->SetScalarTypeToUnsignedShort();
139 //EED 2017-01-01 Migration VTK7
140 #if VTK_MAJOR_VERSION <= 5
141 points->SetScalarTypeToDouble();
142 points->AllocateScalars();
145 points->AllocateScalars(VTK_DOUBLE,1);
151 constructing the histogram
153 void pHistogram::setPoints(vtkImageData* imageData)
156 printf("EED pHistogram::setPoints Trinng new histogram ..\n");
158 printf("EED pHistogram::setPoints Test: vtkPComputeHistogram2DOutliers\n");
159 vtkComputeHistogram2DOutliers *vtkhist = vtkComputeHistogram2DOutliers::New();
160 vtkhist->SetPreferredNumberOfOutliers(10);
161 vtkhist->SetInput(imageData);
162 vtkTable *resulttable = vtkhist->GetOutputTable();
164 printf("EED pHistogram::setPoints rows:%d\n", (int)(resulttable->GetNumberOfRows()) );
165 printf("EED pHistogram::setPoints colums:%d\n", (int)(resulttable->GetNumberOfColumns()) );
172 char* dataImagePointerC = (char*)imageData->GetScalarPointer(0,0,0);
173 unsigned char* dataImagePointerUC = (unsigned char*)imageData->GetScalarPointer(0,0,0);
174 short* dataImagePointerS = (short*)imageData->GetScalarPointer(0,0,0);
175 unsigned short* dataImagePointerUS = (unsigned short*)imageData->GetScalarPointer(0,0,0);
176 float* dataImagePointerF = (float*)imageData->GetScalarPointer(0,0,0);
177 double* dataImagePointerD = (double*)imageData->GetScalarPointer(0,0,0);
178 double* dataHistogramPointer = (double*)points->GetScalarPointer(0,0,0);
188 imageData->GetScalarRange(range);
192 Setting the minimun and maximum levels of grey
194 maxLevelOfGrey = range[1];
195 minLevelOfGrey = range[0];
196 //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
201 imageData->GetExtent(ext);
203 sx = ext[1]-ext[0]+1;
204 sy = ext[3]-ext[2]+1;
205 sz = ext[5]-ext[4]+1;
211 getting ready the histogram
215 dataHistogramPointer[i]=0;
221 if (imageData->GetScalarType()==VTK_CHAR)
223 for(i=0;i<sizeImage;i++)
225 j=getIndex(*dataImagePointerC);
227 dataHistogramPointer[j]++;
231 if (imageData->GetScalarType()==VTK_UNSIGNED_CHAR)
233 for(i=0;i<sizeImage;i++)
235 j=getIndex(*dataImagePointerUC);
236 dataImagePointerUC++;
237 dataHistogramPointer[j]++;
242 if (imageData->GetScalarType()==VTK_SHORT)
244 for(i=0;i<sizeImage;i++)
246 j=getIndex(*dataImagePointerS);
248 dataHistogramPointer[j]++;
252 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
254 for(i=0;i<sizeImage;i++)
256 j=getIndex(*dataImagePointerUS);
257 dataImagePointerUS++;
258 dataHistogramPointer[j]++;
263 if (imageData->GetScalarType()==VTK_FLOAT)
265 for(i=0;i<sizeImage;i++)
267 j=getIndex(*dataImagePointerF);
269 dataHistogramPointer[j]++;
274 if (imageData->GetScalarType()==VTK_DOUBLE)
276 for(i=0;i<sizeImage;i++)
278 j=getIndex(*dataImagePointerD);
280 dataHistogramPointer[j]++;
288 Returns the poins of the histograms
290 vtkImageData* pHistogram::getHistogram()
296 hash por getting the index for the histogram vector of the original
298 @gValue: Level of grey for which wants the index in the histogrram
300 int pHistogram::getIndex(double gValue)
303 double diff=maxLevelOfGrey-minLevelOfGrey;
306 p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
311 //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
317 void pHistogram::setSize(int nSize)
324 int pHistogram::getImageSize()
329 Get Size of the histogram
331 int pHistogram::getSize()
336 Get the maximum value of grey of the histogram
338 int pHistogram::getMaximumLevelOfGrey()
340 return maxLevelOfGrey;
343 Get the minimum value of grey of the histogram
345 int pHistogram::getMinimumLevelOfGrey()
347 return minLevelOfGrey;
350 get a point of the Histogram
353 int pHistogram::getHistogramPoint(int gValue)
355 //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
358 double* dataHistogramPointer = dataHistogramPointer=(double*)points->GetScalarPointer(0,0,0);
359 return dataHistogramPointer[gValue];