]> Creatis software - creaRigidRegistration.git/blob - lib/Substraction.cxx
b6f50690dc612ca1ad2482e2c39673e0cfe174a7
[creaRigidRegistration.git] / lib / Substraction.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------      */                                                                    
26
27 #include "Substraction.h"
28
29 Substraction::Substraction(vtkImageData* imageData1,vtkImageData* imageData2, int uZLevel,int lZLevel, std::vector<double> uColor, std::vector<double> lColor, std::vector<double> mColor)
30 {
31         imageResult= vtkImageData::New();
32         sizeImage=0;
33         uZeroLevel=uZLevel;
34         lZeroLevel=lZLevel;
35         if(uColor.size() != 0)
36         {
37                 upperColor[0] = uColor[0];
38                 upperColor[1] = uColor[1];
39                 upperColor[2] = uColor[2];
40         }
41         else
42         {
43                 upperColor[0] = 255;
44                 upperColor[1] = 255;
45                 upperColor[2] = 255;
46         }       
47         if(mColor.size() != 0)
48         {
49                 mediumColor[0] = mColor[0];
50                 mediumColor[1] = mColor[1];
51                 mediumColor[2] = mColor[2];
52         }
53         else
54         {
55                 mediumColor[0] = 125;
56                 mediumColor[1] = 125;
57                 mediumColor[2] = 125;
58         }       
59         if(lColor.size() != 0)
60         {
61                 lowerColor[0] = lColor[0];
62                 lowerColor[1] = lColor[1];
63                 lowerColor[2] = lColor[2];
64         }
65         else
66         {
67                 lowerColor[0] = 0;
68                 lowerColor[1] = 0;
69                 lowerColor[2] = 0;
70         }
71         
72         //Original image type this case is an unsigned char (3)
73         //int t=imageData1->GetScalarType(); // JPR : unused
74         //substracting the image
75         substractImage(imageData1, imageData2);
76 }
77
78 Substraction::~Substraction()
79 {
80         if(imageResult!=NULL)imageResult->Delete();
81 }
82
83 //----------------------------------------------------------------------------
84 // Methods
85 //----------------------------------------------------------------------------
86
87 /*
88 Calculate the new image and save it in the attribute imageResult
89 it is used if the user had given the imageData
90 */
91 void Substraction::substractImage(vtkImageData* imageData1, vtkImageData* imageData2)
92 {
93         //dimensions of the image (extent)
94     int ext[6];
95         //setting the dimensionality (1d or 2d or 3d )
96     int newDim[3];
97         //image spacing
98     double spc[3];
99   
100         //getting the information from the original image
101     imageData1->GetSpacing(spc);
102     imageData1->GetExtent(ext);
103         
104         //this a 2d image
105     newDim[0]=ext[1]-ext[0]+1;
106     newDim[1]=ext[3]-ext[2]+1;
107     newDim[2]=1;// in general it is ext[5]-ext[4]+1
108
109
110     imageType = imageData1->GetScalarType();
111         //initializing the image that represents the substracted image
112     initialize(newDim,spc);
113         //Time to substract
114     substract(imageData1, imageData2);  
115 }
116
117 /*
118   getting ready the imageResult
119 */
120 void Substraction::initialize(int dimensions[], double spacing[])
121 {
122         //setting image data of the imageResult
123         imageResult->SetScalarType(imageType);
124         imageResult->SetSpacing(spacing);
125         imageResult->SetDimensions(dimensions);
126         imageResult->AllocateScalars();
127         imageResult->Update();
128 }
129
130 /*
131          Setting the values for the
132 */
133 void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
134 {
135         if(imageType == VTK_CHAR)
136         {
137                 /*
138                 images pointers
139                 */
140                 // pointers to get into the image
141                 char* dataImagePointer1=NULL;
142                 char* dataImagePointer2=NULL;
143                 char* dataImageResultPointer=NULL;
144                 
145                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
146         }
147         else if(imageType == VTK_UNSIGNED_CHAR)
148         {
149                 /*
150                 images pointers
151                 */
152                 // pointers to get into the image
153                 unsigned char* dataImagePointer1=NULL;
154                 unsigned char* dataImagePointer2=NULL;
155                 unsigned char* dataImageResultPointer=NULL;
156                 
157                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
158         }
159         else if(imageType == VTK_SIGNED_CHAR)
160         {
161                 /*
162                 images pointers
163                 */
164                 // pointers to get into the image
165                 signed char* dataImagePointer1=NULL;
166                 signed char* dataImagePointer2=NULL;
167                 signed char* dataImageResultPointer=NULL;
168                 
169                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
170         }
171         else if(imageType == VTK_SHORT)
172         {
173                 /*
174                 images pointers
175                 */
176                 // pointers to get into the image
177                 short* dataImagePointer1=NULL;
178                 short* dataImagePointer2=NULL;
179                 short* dataImageResultPointer=NULL;
180
181                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);          
182         }
183         else if(imageType == VTK_UNSIGNED_SHORT)
184         {
185                 /*
186                 images pointers
187                 */
188                 // pointers to get into the image
189                 unsigned short* dataImagePointer1=NULL;
190                 unsigned short* dataImagePointer2=NULL;
191                 unsigned short* dataImageResultPointer=NULL;
192                 
193                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
194         }
195         else if(imageType == VTK_INT)
196         {
197                 /*
198                 images pointers
199                 */
200                 // pointers to get into the image
201                 int* dataImagePointer1=NULL;
202                 int* dataImagePointer2=NULL;
203                 int* dataImageResultPointer=NULL;
204                 
205                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
206         }
207         else if(imageType == VTK_UNSIGNED_INT)
208         {
209                 /*
210                 images pointers
211                 */
212                 // pointers to get into the image
213                 unsigned int* dataImagePointer1=NULL;
214                 unsigned int* dataImagePointer2=NULL;
215                 unsigned int* dataImageResultPointer=NULL;
216                 
217                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
218         }
219         else if(imageType == VTK_LONG)
220         {
221                 /*
222                 images pointers
223                 */
224                 // pointers to get into the image
225                 long* dataImagePointer1=NULL;
226                 long* dataImagePointer2=NULL;
227                 long* dataImageResultPointer=NULL;
228
229                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
230         }
231         else if(imageType == VTK_UNSIGNED_LONG)
232         {
233                 /*
234                 images pointers
235                 */
236                 // pointers to get into the image
237                 unsigned long* dataImagePointer1=NULL;
238                 unsigned long* dataImagePointer2=NULL;
239                 unsigned long* dataImageResultPointer=NULL;
240                 
241                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
242         }
243         else if(imageType == VTK_FLOAT)
244         {
245                 /*
246                 images pointers
247                 */
248                 // pointers to get into the image
249                 float* dataImagePointer1=NULL;
250                 float* dataImagePointer2=NULL;
251                 float* dataImageResultPointer=NULL;
252                 
253                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
254         }
255         else if(imageType == VTK_DOUBLE)
256         {
257                 /*
258                 images pointers
259                 */
260                 // pointers to get into the image
261                 double* dataImagePointer1=NULL;
262                 double* dataImagePointer2=NULL;
263                 double* dataImageResultPointer=NULL;
264                 
265                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
266         }
267 }
268
269 template <class T> 
270 void Substraction::substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2)
271 {
272         // we start where the  image starts
273         dataImagePointer1=(T*)imageData1->GetScalarPointer(0,0,0);
274         dataImagePointer2=(T*)imageData2->GetScalarPointer(0,0,0);
275         dataImageResultPointer=(T*)imageResult->GetScalarPointer(0,0,0);
276         /*
277         Image Size
278         */
279         int ext[6];
280         imageData1->GetExtent(ext);
281         int sx,sy,sz;
282         sx=ext[1]-ext[0]+1;
283         sy=ext[3]-ext[2]+1;
284         sz=ext[5]-ext[4]+1;
285
286         sizeImage=sx*sy*sz;
287         //-----------------
288         //A3    
289         //-----------------
290         //walking in the image
291         int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
292         double sum1=0,sum2=0;
293         for(i=0;i<sx;i++)
294         {
295                 for(j=0;j<sy;j++)
296                 {
297                         for(k=0;k<sz;k++)
298                         {
299                                         
300                                 // this is for getting just the grey level in that position
301                                 //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
302                 
303                                 // we get the pointer to the position (i,j,k)y that way we can get the 
304                                 //grey level and we can change it
305
306                                 dataImagePointer1=(T*)imageData1->GetScalarPointer(i,j,k);
307                                 dataImagePointer2=(T*)imageData2->GetScalarPointer(i,j,k);
308                                 dataImageResultPointer=(T*)imageResult->GetScalarPointer(i,j,k);
309
310                                 sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
311                                 sum1=sum1/3;
312                                 sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
313                                 sum2=sum2/3;                            
314                                 if((sum1 - sum2) < lZeroLevel)
315                                 {
316                                         dataImageResultPointer[0] =(T) lowerColor[0];
317                                         dataImageResultPointer[1] =(T) lowerColor[1];
318                                         dataImageResultPointer[2] =(T) lowerColor[2];
319                                         nL++;
320                                 }
321                                 else if((sum1 - sum2) > uZeroLevel)
322                                 {
323                                         dataImageResultPointer[0] =(T) upperColor[0];
324                                         dataImageResultPointer[1] =(T) upperColor[1];
325                                         dataImageResultPointer[2] =(T) upperColor[2];
326                                         nU++;
327                                 }
328                                 else
329                                 {
330                                         dataImageResultPointer[0] =(T) mediumColor[0];
331                                         dataImageResultPointer[1] =(T) mediumColor[1];
332                                         dataImageResultPointer[2] =(T) mediumColor[2];
333                                         nZ++;
334                                 }                               
335                                 counter++;
336                         }
337                 }
338         }
339 }
340
341 /*
342 Returns the filtered image
343 */
344 vtkImageData* Substraction::getSubstractedImage()
345 {
346         return imageResult;
347 }
348
349 /*
350         Get Image Size
351 */
352 int Substraction::getImageSize()
353
354         return sizeImage;
355 }