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();
138 points->SetScalarTypeToDouble();
139 points->AllocateScalars();
144 constructing the histogram
146 void pHistogram::setPoints(vtkImageData* imageData)
149 printf("EED pHistogram::setPoints Trinng new histogram ..\n");
151 printf("EED pHistogram::setPoints Test: vtkPComputeHistogram2DOutliers\n");
152 vtkComputeHistogram2DOutliers *vtkhist = vtkComputeHistogram2DOutliers::New();
153 vtkhist->SetPreferredNumberOfOutliers(10);
154 vtkhist->SetInput(imageData);
155 vtkTable *resulttable = vtkhist->GetOutputTable();
157 printf("EED pHistogram::setPoints rows:%d\n", (int)(resulttable->GetNumberOfRows()) );
158 printf("EED pHistogram::setPoints colums:%d\n", (int)(resulttable->GetNumberOfColumns()) );
165 char* dataImagePointerC = (char*)imageData->GetScalarPointer(0,0,0);
166 unsigned char* dataImagePointerUC = (unsigned char*)imageData->GetScalarPointer(0,0,0);
167 short* dataImagePointerS = (short*)imageData->GetScalarPointer(0,0,0);
168 unsigned short* dataImagePointerUS = (unsigned short*)imageData->GetScalarPointer(0,0,0);
169 float* dataImagePointerF = (float*)imageData->GetScalarPointer(0,0,0);
170 double* dataImagePointerD = (double*)imageData->GetScalarPointer(0,0,0);
171 double* dataHistogramPointer = (double*)points->GetScalarPointer(0,0,0);
181 imageData->GetScalarRange(range);
185 Setting the minimun and maximum levels of grey
187 maxLevelOfGrey = range[1];
188 minLevelOfGrey = range[0];
189 //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
194 imageData->GetExtent(ext);
196 sx = ext[1]-ext[0]+1;
197 sy = ext[3]-ext[2]+1;
198 sz = ext[5]-ext[4]+1;
204 getting ready the histogram
208 dataHistogramPointer[i]=0;
214 if (imageData->GetScalarType()==VTK_CHAR)
216 for(i=0;i<sizeImage;i++)
218 j=getIndex(*dataImagePointerC);
220 dataHistogramPointer[j]++;
224 if (imageData->GetScalarType()==VTK_UNSIGNED_CHAR)
226 for(i=0;i<sizeImage;i++)
228 j=getIndex(*dataImagePointerUC);
229 dataImagePointerUC++;
230 dataHistogramPointer[j]++;
235 if (imageData->GetScalarType()==VTK_SHORT)
237 for(i=0;i<sizeImage;i++)
239 j=getIndex(*dataImagePointerS);
241 dataHistogramPointer[j]++;
245 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
247 for(i=0;i<sizeImage;i++)
249 j=getIndex(*dataImagePointerUS);
250 dataImagePointerUS++;
251 dataHistogramPointer[j]++;
256 if (imageData->GetScalarType()==VTK_FLOAT)
258 for(i=0;i<sizeImage;i++)
260 j=getIndex(*dataImagePointerF);
262 dataHistogramPointer[j]++;
267 if (imageData->GetScalarType()==VTK_DOUBLE)
269 for(i=0;i<sizeImage;i++)
271 j=getIndex(*dataImagePointerD);
273 dataHistogramPointer[j]++;
281 Returns the poins of the histograms
283 vtkImageData* pHistogram::getHistogram()
289 hash por getting the index for the histogram vector of the original
291 @gValue: Level of grey for which wants the index in the histogrram
293 int pHistogram::getIndex(double gValue)
296 double diff=maxLevelOfGrey-minLevelOfGrey;
299 p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
304 //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
310 void pHistogram::setSize(int nSize)
317 int pHistogram::getImageSize()
322 Get Size of the histogram
324 int pHistogram::getSize()
329 Get the maximum value of grey of the histogram
331 int pHistogram::getMaximumLevelOfGrey()
333 return maxLevelOfGrey;
336 Get the minimum value of grey of the histogram
338 int pHistogram::getMinimumLevelOfGrey()
340 return minLevelOfGrey;
343 get a point of the Histogram
346 int pHistogram::getHistogramPoint(int gValue)
348 //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
351 double* dataHistogramPointer = dataHistogramPointer=(double*)points->GetScalarPointer(0,0,0);
352 return dataHistogramPointer[gValue];