]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/src/interface/wxWindows/widgets/pPlotter/pHistogram.cxx
2415 creaMaracasVisu Bug New Normal box Histogram is not working in differents formats
[creaMaracasVisu.git] / lib / maracasVisuLib / src / interface / wxWindows / widgets / pPlotter / pHistogram.cxx
1 /*# ---------------------------------------------------------------------
2 #
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
4 #                        pour la Sant�)
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
8 #
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.
15 #
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
20 #  liability.
21 #
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 # ------------------------------------------------------------------------ */
25
26
27 //----------------------------------------------------------------------------
28 // Class definition include
29 //----------------------------------------------------------------------------
30
31 #include "pHistogram.h"
32 //#include "vtkJPEGReader.h"
33 #include "vtkImageData.h"
34 #include "vtkImageCast.h"
35 #include "vtkComputeHistogram2DOutliers.h"
36 #include "vtkTable.h"
37
38
39 #include <iostream>
40 #include <fstream>
41 #include <string>
42 #include <vector>
43
44 // ----------------------------------------------------------------------------
45 // WX headers inclusion.
46 // For compilers that support precompilation, includes <wx/wx.h>.
47 // ----------------------------------------------------------------------------
48
49 /*
50 #ifndef WX_PRECOMP
51 #include <wx/wx.h>
52 #endif
53 */
54 //----------------------------------------------------------------------------
55 // Class implementation
56 //----------------------------------------------------------------------------
57
58 //IMPLEMENT_CLASS(pHistogram, wxObject)
59
60 //----------------------------------------------------------------------------
61 // Constructors
62 //----------------------------------------------------------------------------
63 pHistogram::pHistogram(std::string filePath)
64 {
65         path=filePath;
66         points= vtkImageData::New();
67         size=100;
68         sizeImage=0;
69         buildHistogram();
70 }
71
72 pHistogram::pHistogram(vtkImageData* imageData)
73 {
74         points= vtkImageData::New();
75         size=100;
76         sizeImage=0;
77         //cast
78         /*
79         vtkImageCast* cast= vtkImageCast::New();
80         cast->SetInput(imageData);
81         cast->SetOutputScalarTypeToInt();
82         cast->Update();
83         */
84         //build the histogram
85         buildHistogram(imageData);
86 }
87
88 pHistogram::~pHistogram()
89 {
90         if(points!=NULL)points->Delete();
91 }
92 //----------------------------------------------------------------------------
93 // Methods
94 //----------------------------------------------------------------------------
95
96 void pHistogram::setImagePath(std::string filePath)
97 {
98         path=filePath;
99 }
100
101 void pHistogram::buildHistogram()
102 {
103         /*
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
108         */
109         points= vtkImageData::New();
110         vtkMetaImageReader *reader = vtkMetaImageReader::New();
111         vtkImageData* imageData=NULL;
112         double range[2];
113         
114         //reading
115         reader->SetFileName(path.c_str());
116         reader->Update();
117
118         
119         // getting the data 
120         imageData=reader->GetOutput();
121         imageData->GetScalarRange(range);
122         initializePoints(size);
123         setPoints(imageData);
124         
125 }
126
127 /*
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
132 */
133 void pHistogram::buildHistogram(vtkImageData* imageData)
134 {
135         initializePoints(size);
136         setPoints(imageData);   
137 }
138
139 /*
140   getting ready the points
141 */
142 void pHistogram::initializePoints(int xDimension)
143 {
144         //setting image data of the points
145         points->SetDimensions(xDimension,1,1);
146 //EED   points->SetScalarTypeToUnsignedShort();
147         points->SetScalarTypeToShort();
148         points->AllocateScalars();
149         points->Update();
150 }
151
152 /*
153          constructing the histogram
154 */
155 void pHistogram::setPoints(vtkImageData* imageData)
156 {
157
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();
163
164 printf("EED pHistogram::setPoints rows:%d\n", (int)(resulttable->GetNumberOfRows()) );
165 printf("EED pHistogram::setPoints colums:%d\n", (int)(resulttable->GetNumberOfColumns()) );
166
167         /*
168                 Pointers
169         */
170         unsigned short* dataImagePointerUS      = NULL;
171         short* dataImagePointerS                = NULL;
172         double* dataImagePointerD               = NULL;
173
174         short* dataHistogramPointer             = NULL;
175
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);
179
180         dataHistogramPointer    = (short*)points->GetScalarPointer(0,0,0);
181         
182         /*
183          Range of greys
184         */
185         double range[2];
186         if(imageData==NULL)
187                 range[1]=1;
188         else
189                 imageData->GetScalarRange(range);
190         /*
191          Setting the  minimun and maximum levels of grey
192         */
193         maxLevelOfGrey=range[1];
194         minLevelOfGrey=range[0];
195         //std::cout<<"maxLevelOfGrey "<<maxLevelOfGrey<<" minLevelOfGrey "<<minLevelOfGrey<<std::endl;
196         /*
197          Image Size
198         */
199         int ext[6];
200         imageData->GetExtent(ext);
201         int sx,sy,sz;
202         sx=ext[1]+1;
203         sy=ext[3]+1;
204         sz=ext[5]+1;
205
206         sizeImage=sx*sy*sz;
207
208         int i;
209         /*
210           getting ready the histogram
211         */
212         for(i=0;i<size;i++)
213         {
214                 dataHistogramPointer[i]=0;
215         }
216         
217         /*
218         Constructing the Histogram
219         */
220         //int k=size/(maxLevelOfGrey-minLevelOfGrey);
221         int j=0;
222         for(i=0;i<sizeImage;i++)
223         {
224                 /*
225                  hashing the histogram
226                 */
227                 //double p=((float)*dataImagePointer-minLevelOfGrey);
228                 //j=p*k;
229                 if (imageData->GetScalarType()==VTK_UNSIGNED_SHORT)
230                 { 
231                         j=getIndex(*dataImagePointerUS);
232                         dataImagePointerUS++;
233                 }
234                 if (imageData->GetScalarType()==VTK_SHORT) 
235                 {
236                         j=getIndex(*dataImagePointerS);
237                         dataImagePointerS++;
238                 }
239                 if (imageData->GetScalarType()==VTK_DOUBLE) 
240                 {
241                         j=getIndex(*dataImagePointerD);
242                         dataImagePointerD++;
243                 }
244 //EED           j=getIndex(*dataImagePointer);
245
246                 //std::cout<<j<<std::endl;
247                 dataHistogramPointer[j]++;
248         }
249         /*
250         BORRAME
251         */
252         /*
253         k=0;
254         for(i=0;i<size;i++)
255         {
256                 k=dataHistogramPointer[i];
257         }
258         */
259 }
260 /*
261 Returns the poins of the histograms
262 */
263 vtkImageData* pHistogram::getHistogram()
264 {
265         return points;
266 }
267
268 /*
269 hash por getting the index for the histogram vector of the original
270 image
271 @gValue: Level of grey for which wants the index in the histogrram
272 */
273 int pHistogram::getIndex(double gValue)
274 {
275
276         double p=((double)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey);
277         double k=p*(size-1);
278         //std::cout<<"gValue "<<gValue<<" k "<<k<<std::endl;
279         return (int)k;
280 }
281 /*
282 Setting the size
283 */
284 void pHistogram::setSize(int nSize)
285 {
286         size=nSize;
287 }
288 /*
289         Get Image Size
290 */
291 int pHistogram::getImageSize()
292
293         return sizeImage;
294 }
295 /*
296         Get Size of the histogram
297 */
298 int pHistogram::getSize()
299 {
300         return size;
301 }
302 /*
303         Get the maximum value of grey of the histogram
304 */
305 int pHistogram::getMaximumLevelOfGrey()
306 {
307         return maxLevelOfGrey;
308 }
309 /*
310         Get the minimum value of grey of the histogram
311 */
312 int pHistogram::getMinimumLevelOfGrey()
313 {
314         return minLevelOfGrey;
315 }
316 /*
317                 get a point of the Histogram
318                 given the grey value
319 */
320 int pHistogram::getHistogramPoint(int gValue)
321 {
322         //double p=((float)gValue-minLevelOfGrey)/(maxLevelOfGrey-minLevelOfGrey); // JPRx
323         //double k=p*size;
324
325         unsigned short* dataHistogramPointer=NULL;
326         dataHistogramPointer=(unsigned short*)points->GetScalarPointer(0,0,0);
327
328         return dataHistogramPointer[gValue];
329 }
330
331