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)
157 printf("EED pHistogram::setPoints Test: vtkPComputeHistogram2DOutliers\n");
158 vtkComputeHistogram2DOutliers *vtkhist = vtkComputeHistogram2DOutliers::New();
159 vtkhist->SetPreferredNumberOfOutliers(10);
160 vtkhist->SetInput(imageData);
161 vtkTable *resulttable = vtkhist->GetOutputTable();
163 printf("EED pHistogram::setPoints rows:%d\n", (int)(resulttable->GetNumberOfRows()) );
164 printf("EED pHistogram::setPoints colums:%d\n", (int)(resulttable->GetNumberOfColumns()) );
171 char* dataImagePointerC = (char*)imageData->GetScalarPointer(0,0,0);
172 unsigned char* dataImagePointerUC = (unsigned char*)imageData->GetScalarPointer(0,0,0);
173 short* dataImagePointerS = (short*)imageData->GetScalarPointer(0,0,0);
174 unsigned short* dataImagePointerUS = (unsigned short*)imageData->GetScalarPointer(0,0,0);
175 float* dataImagePointerF = (float*)imageData->GetScalarPointer(0,0,0);
176 double* dataImagePointerD = (double*)imageData->GetScalarPointer(0,0,0);
177 double* dataHistogramPointer = (double*)points->GetScalarPointer(0,0,0);
187 imageData->GetScalarRange(range);
191 Setting the minimun and maximum levels of grey
193 maxLevelOfGrey = range[1];
194 minLevelOfGrey = range[0];
195 //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
200 imageData->GetExtent(ext);
202 sx = ext[1]-ext[0]+1;
203 sy = ext[3]-ext[2]+1;
204 sz = ext[5]-ext[4]+1;
210 getting ready the histogram
214 dataHistogramPointer[i]=0;
220 if (imageData->GetScalarType()==VTK_CHAR)
222 for(i=0;i<sizeImage;i++)
224 j=getIndex(*dataImagePointerC);
226 dataHistogramPointer[j]++;
230 if (imageData->GetScalarType()==VTK_UNSIGNED_CHAR)
232 for(i=0;i<sizeImage;i++)
234 j=getIndex(*dataImagePointerUC);
235 dataImagePointerUC++;
236 dataHistogramPointer[j]++;
241 if (imageData->GetScalarType()==VTK_SHORT)
243 for(i=0;i<sizeImage;i++)
245 j=getIndex(*dataImagePointerS);
247 dataHistogramPointer[j]++;
251 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
253 for(i=0;i<sizeImage;i++)
255 j=getIndex(*dataImagePointerUS);
256 dataImagePointerUS++;
257 dataHistogramPointer[j]++;
262 if (imageData->GetScalarType()==VTK_FLOAT)
264 for(i=0;i<sizeImage;i++)
266 j=getIndex(*dataImagePointerF);
268 dataHistogramPointer[j]++;
273 if (imageData->GetScalarType()==VTK_DOUBLE)
275 for(i=0;i<sizeImage;i++)
277 j=getIndex(*dataImagePointerD);
279 dataHistogramPointer[j]++;
287 Returns the poins of the histograms
289 vtkImageData* pHistogram::getHistogram()
295 hash por getting the index for the histogram vector of the original
297 @gValue: Level of grey for which wants the index in the histogrram
299 int pHistogram::getIndex(double gValue)
302 double diff=maxLevelOfGrey-minLevelOfGrey;
305 p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
310 //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
316 void pHistogram::setSize(int nSize)
323 int pHistogram::getImageSize()
328 Get Size of the histogram
330 int pHistogram::getSize()
335 Get the maximum value of grey of the histogram
337 int pHistogram::getMaximumLevelOfGrey()
339 return maxLevelOfGrey;
342 Get the minimum value of grey of the histogram
344 int pHistogram::getMinimumLevelOfGrey()
346 return minLevelOfGrey;
349 get a point of the Histogram
352 int pHistogram::getHistogramPoint(int gValue)
354 //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
357 double* dataHistogramPointer = dataHistogramPointer=(double*)points->GetScalarPointer(0,0,0);
358 return dataHistogramPointer[gValue];