2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
27 #include "Substraction.h"
29 Substraction::Substraction(vtkImageData* imageData1,vtkImageData* imageData2, int uZLevel,int lZLevel, std::vector<double> uColor, std::vector<double> lColor, std::vector<double> mColor)
31 imageResult= vtkImageData::New();
35 if(uColor.size() != 0)
37 upperColor[0] = uColor[0];
38 upperColor[1] = uColor[1];
39 upperColor[2] = uColor[2];
47 if(mColor.size() != 0)
49 mediumColor[0] = mColor[0];
50 mediumColor[1] = mColor[1];
51 mediumColor[2] = mColor[2];
59 if(lColor.size() != 0)
61 lowerColor[0] = lColor[0];
62 lowerColor[1] = lColor[1];
63 lowerColor[2] = lColor[2];
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);
78 Substraction::~Substraction()
80 if(imageResult!=NULL)imageResult->Delete();
83 //----------------------------------------------------------------------------
85 //----------------------------------------------------------------------------
88 Calculate the new image and save it in the attribute imageResult
89 it is used if the user had given the imageData
91 void Substraction::substractImage(vtkImageData* imageData1, vtkImageData* imageData2)
93 //dimensions of the image (extent)
95 //setting the dimensionality (1d or 2d or 3d )
100 //getting the information from the original image
101 imageData1->GetSpacing(spc);
102 imageData1->GetExtent(ext);
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
110 imageType = imageData1->GetScalarType();
111 //initializing the image that represents the substracted image
112 initialize(newDim,spc);
114 substract(imageData1, imageData2);
118 getting ready the imageResult
120 void Substraction::initialize(int dimensions[], double spacing[])
122 //setting image data of the imageResult
123 imageResult->SetSpacing(spacing);
124 imageResult->SetDimensions(dimensions);
125 //EED 2017-01-01 Migration VTK7
126 #if VTK_MAJOR_VERSION <= 5
127 imageResult->SetScalarType(imageType);
128 imageResult->AllocateScalars();
129 imageResult->Update();
131 imageResult->AllocateScalars(imageType,1);
137 Setting the values for the
139 void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
141 if(imageType == VTK_CHAR)
146 // pointers to get into the image
147 char* dataImagePointer1=NULL;
148 char* dataImagePointer2=NULL;
149 char* dataImageResultPointer=NULL;
151 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
153 else if(imageType == VTK_UNSIGNED_CHAR)
158 // pointers to get into the image
159 unsigned char* dataImagePointer1=NULL;
160 unsigned char* dataImagePointer2=NULL;
161 unsigned char* dataImageResultPointer=NULL;
163 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
165 else if(imageType == VTK_SIGNED_CHAR)
170 // pointers to get into the image
171 signed char* dataImagePointer1=NULL;
172 signed char* dataImagePointer2=NULL;
173 signed char* dataImageResultPointer=NULL;
175 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
177 else if(imageType == VTK_SHORT)
182 // pointers to get into the image
183 short* dataImagePointer1=NULL;
184 short* dataImagePointer2=NULL;
185 short* dataImageResultPointer=NULL;
187 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
189 else if(imageType == VTK_UNSIGNED_SHORT)
194 // pointers to get into the image
195 unsigned short* dataImagePointer1=NULL;
196 unsigned short* dataImagePointer2=NULL;
197 unsigned short* dataImageResultPointer=NULL;
199 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
201 else if(imageType == VTK_INT)
206 // pointers to get into the image
207 int* dataImagePointer1=NULL;
208 int* dataImagePointer2=NULL;
209 int* dataImageResultPointer=NULL;
211 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
213 else if(imageType == VTK_UNSIGNED_INT)
218 // pointers to get into the image
219 unsigned int* dataImagePointer1=NULL;
220 unsigned int* dataImagePointer2=NULL;
221 unsigned int* dataImageResultPointer=NULL;
223 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
225 else if(imageType == VTK_LONG)
230 // pointers to get into the image
231 long* dataImagePointer1=NULL;
232 long* dataImagePointer2=NULL;
233 long* dataImageResultPointer=NULL;
235 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
237 else if(imageType == VTK_UNSIGNED_LONG)
242 // pointers to get into the image
243 unsigned long* dataImagePointer1=NULL;
244 unsigned long* dataImagePointer2=NULL;
245 unsigned long* dataImageResultPointer=NULL;
247 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
249 else if(imageType == VTK_FLOAT)
254 // pointers to get into the image
255 float* dataImagePointer1=NULL;
256 float* dataImagePointer2=NULL;
257 float* dataImageResultPointer=NULL;
259 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
261 else if(imageType == VTK_DOUBLE)
266 // pointers to get into the image
267 double* dataImagePointer1=NULL;
268 double* dataImagePointer2=NULL;
269 double* dataImageResultPointer=NULL;
271 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
276 void Substraction::substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2)
278 // we start where the image starts
279 dataImagePointer1=(T*)imageData1->GetScalarPointer(0,0,0);
280 dataImagePointer2=(T*)imageData2->GetScalarPointer(0,0,0);
281 dataImageResultPointer=(T*)imageResult->GetScalarPointer(0,0,0);
286 imageData1->GetExtent(ext);
296 //walking in the image
297 int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
298 double sum1=0,sum2=0;
306 // this is for getting just the grey level in that position
307 //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
309 // we get the pointer to the position (i,j,k)y that way we can get the
310 //grey level and we can change it
312 dataImagePointer1=(T*)imageData1->GetScalarPointer(i,j,k);
313 dataImagePointer2=(T*)imageData2->GetScalarPointer(i,j,k);
314 dataImageResultPointer=(T*)imageResult->GetScalarPointer(i,j,k);
316 sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
318 sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
320 if((sum1 - sum2) < lZeroLevel)
322 dataImageResultPointer[0] =(T) lowerColor[0];
323 dataImageResultPointer[1] =(T) lowerColor[1];
324 dataImageResultPointer[2] =(T) lowerColor[2];
327 else if((sum1 - sum2) > uZeroLevel)
329 dataImageResultPointer[0] =(T) upperColor[0];
330 dataImageResultPointer[1] =(T) upperColor[1];
331 dataImageResultPointer[2] =(T) upperColor[2];
336 dataImageResultPointer[0] =(T) mediumColor[0];
337 dataImageResultPointer[1] =(T) mediumColor[1];
338 dataImageResultPointer[2] =(T) mediumColor[2];
348 Returns the filtered image
350 vtkImageData* Substraction::getSubstractedImage()
358 int Substraction::getImageSize()