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->SetScalarType(imageType);
124 imageResult->SetSpacing(spacing);
125 imageResult->SetDimensions(dimensions);
126 imageResult->AllocateScalars();
127 imageResult->Update();
131 Setting the values for the
133 void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
135 if(imageType == VTK_CHAR)
140 // pointers to get into the image
141 char* dataImagePointer1=NULL;
142 char* dataImagePointer2=NULL;
143 char* dataImageResultPointer=NULL;
145 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
147 else if(imageType == VTK_UNSIGNED_CHAR)
152 // pointers to get into the image
153 unsigned char* dataImagePointer1=NULL;
154 unsigned char* dataImagePointer2=NULL;
155 unsigned char* dataImageResultPointer=NULL;
157 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
159 else if(imageType == VTK_SIGNED_CHAR)
164 // pointers to get into the image
165 signed char* dataImagePointer1=NULL;
166 signed char* dataImagePointer2=NULL;
167 signed char* dataImageResultPointer=NULL;
169 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
171 else if(imageType == VTK_SHORT)
176 // pointers to get into the image
177 short* dataImagePointer1=NULL;
178 short* dataImagePointer2=NULL;
179 short* dataImageResultPointer=NULL;
181 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
183 else if(imageType == VTK_UNSIGNED_SHORT)
188 // pointers to get into the image
189 unsigned short* dataImagePointer1=NULL;
190 unsigned short* dataImagePointer2=NULL;
191 unsigned short* dataImageResultPointer=NULL;
193 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
195 else if(imageType == VTK_INT)
200 // pointers to get into the image
201 int* dataImagePointer1=NULL;
202 int* dataImagePointer2=NULL;
203 int* dataImageResultPointer=NULL;
205 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
207 else if(imageType == VTK_UNSIGNED_INT)
212 // pointers to get into the image
213 unsigned int* dataImagePointer1=NULL;
214 unsigned int* dataImagePointer2=NULL;
215 unsigned int* dataImageResultPointer=NULL;
217 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
219 else if(imageType == VTK_LONG)
224 // pointers to get into the image
225 long* dataImagePointer1=NULL;
226 long* dataImagePointer2=NULL;
227 long* dataImageResultPointer=NULL;
229 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
231 else if(imageType == VTK_UNSIGNED_LONG)
236 // pointers to get into the image
237 unsigned long* dataImagePointer1=NULL;
238 unsigned long* dataImagePointer2=NULL;
239 unsigned long* dataImageResultPointer=NULL;
241 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
243 else if(imageType == VTK_FLOAT)
248 // pointers to get into the image
249 float* dataImagePointer1=NULL;
250 float* dataImagePointer2=NULL;
251 float* dataImageResultPointer=NULL;
253 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
255 else if(imageType == VTK_DOUBLE)
260 // pointers to get into the image
261 double* dataImagePointer1=NULL;
262 double* dataImagePointer2=NULL;
263 double* dataImageResultPointer=NULL;
265 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
270 void Substraction::substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2)
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);
280 imageData1->GetExtent(ext);
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;
300 // this is for getting just the grey level in that position
301 //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
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
306 dataImagePointer1=(T*)imageData1->GetScalarPointer(i,j,k);
307 dataImagePointer2=(T*)imageData2->GetScalarPointer(i,j,k);
308 dataImageResultPointer=(T*)imageResult->GetScalarPointer(i,j,k);
310 sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
312 sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
314 if((sum1 - sum2) < lZeroLevel)
316 dataImageResultPointer[0] =(T) lowerColor[0];
317 dataImageResultPointer[1] =(T) lowerColor[1];
318 dataImageResultPointer[2] =(T) lowerColor[2];
321 else if((sum1 - sum2) > uZeroLevel)
323 dataImageResultPointer[0] =(T) upperColor[0];
324 dataImageResultPointer[1] =(T) upperColor[1];
325 dataImageResultPointer[2] =(T) upperColor[2];
330 dataImageResultPointer[0] =(T) mediumColor[0];
331 dataImageResultPointer[1] =(T) mediumColor[1];
332 dataImageResultPointer[2] =(T) mediumColor[2];
342 Returns the filtered image
344 vtkImageData* Substraction::getSubstractedImage()
352 int Substraction::getImageSize()