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 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()) );
170 unsigned short* dataImagePointerUS = NULL;
171 short* dataImagePointerS = NULL;
172 double* dataImagePointerD = NULL;
174 short* dataHistogramPointer = NULL;
176 dataImagePointerUS = (unsigned short*)imageData->GetScalarPointer(0,0,0);
177 dataImagePointerS = (short*)imageData->GetScalarPointer(0,0,0);
178 dataImagePointerD = (double*)imageData->GetScalarPointer(0,0,0);
180 dataHistogramPointer = (short*)points->GetScalarPointer(0,0,0);
189 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);
210 getting ready the histogram
214 dataHistogramPointer[i]=0;
218 Constructing the Histogram
220 //int k=size/(maxLevelOfGrey-minLevelOfGrey);
222 for(i=0;i<sizeImage;i++)
225 hashing the histogram
227 //double p=((float)*dataImagePointer-minLevelOfGrey);
229 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
231 j=getIndex(*dataImagePointerUS);
232 dataImagePointerUS++;
234 if (imageData->GetScalarType()==VTK_SHORT)
236 j=getIndex(*dataImagePointerS);
239 if (imageData->GetScalarType()==VTK_DOUBLE)
241 j=getIndex(*dataImagePointerD);
244 //EED j=getIndex(*dataImagePointer);
246 //std::cout<<j<<std::endl;
247 dataHistogramPointer[j]++;
256 k=dataHistogramPointer[i];
261 Returns the poins of the histograms
263 vtkImageData* pHistogram::getHistogram()
269 hash por getting the index for the histogram vector of the original
271 @gValue: Level of grey for which wants the index in the histogrram
273 int pHistogram::getIndex(double gValue)
276 double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
278 //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
284 void pHistogram::setSize(int nSize)
291 int pHistogram::getImageSize()
296 Get Size of the histogram
298 int pHistogram::getSize()
303 Get the maximum value of grey of the histogram
305 int pHistogram::getMaximumLevelOfGrey()
307 return maxLevelOfGrey;
310 Get the minimum value of grey of the histogram
312 int pHistogram::getMinimumLevelOfGrey()
314 return minLevelOfGrey;
317 get a point of the Histogram
320 int pHistogram::getHistogramPoint(int gValue)
322 //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
325 unsigned short* dataHistogramPointer=NULL;
326 dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
328 return dataHistogramPointer[gValue];