1 #include "Substraction.h"
3 Substraction::Substraction(vtkImageData* imageData1,vtkImageData* imageData2, int uZLevel,int lZLevel, std::vector<double> uColor, std::vector<double> lColor, std::vector<double> mColor)
5 imageResult= vtkImageData::New();
9 if(uColor.size() != NULL)
11 upperColor[0] = uColor[0];
12 upperColor[1] = uColor[1];
13 upperColor[2] = uColor[2];
21 if(mColor.size() != NULL)
23 mediumColor[0] = mColor[0];
24 mediumColor[1] = mColor[1];
25 mediumColor[2] = mColor[2];
33 if(lColor.size() != NULL)
35 lowerColor[0] = lColor[0];
36 lowerColor[1] = lColor[1];
37 lowerColor[2] = lColor[2];
46 //Original image type this case is an unsigned char (3)
47 int t=imageData1->GetScalarType();
48 //substracting the image
49 substractImage(imageData1, imageData2);
52 Substraction::~Substraction()
54 if(imageResult!=NULL)imageResult->Delete();
57 //----------------------------------------------------------------------------
59 //----------------------------------------------------------------------------
63 Calculate the new image and save it in the attribute imageResult
64 it is used if the user had given the imageData
66 void Substraction::substractImage(vtkImageData* imageData1, vtkImageData* imageData2)
68 //dimensions of the image (extent)
70 //setting the dimensionality (1d or 2d or 3d )
75 //getting the information from the original image
76 imageData1->GetSpacing(spc);
77 imageData1->GetExtent(ext);
80 newDim[0]=ext[1]-ext[0]+1;
81 newDim[1]=ext[3]-ext[2]+1;
82 newDim[2]=1;// in general it is ext[5]-ext[4]+1
85 imageType = imageData1->GetScalarType();
86 //initializing the image that represents the substracted image
87 initialize(newDim,spc);
89 substract(imageData1, imageData2);
93 getting ready the imageResult
95 void Substraction::initialize(int dimensions[], double spacing[])
97 //setting image data of the imageResult
98 imageResult->SetScalarType(imageType);
99 imageResult->SetSpacing(spacing);
100 imageResult->SetDimensions(dimensions);
101 imageResult->AllocateScalars();
102 imageResult->Update();
106 Setting the values for the
108 void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
110 if(imageType == VTK_CHAR)
115 // pointers to get into the image
116 char* dataImagePointer1=NULL;
117 char* dataImagePointer2=NULL;
118 char* dataImageResultPointer=NULL;
120 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
122 else if(imageType == VTK_UNSIGNED_CHAR)
127 // pointers to get into the image
128 unsigned char* dataImagePointer1=NULL;
129 unsigned char* dataImagePointer2=NULL;
130 unsigned char* dataImageResultPointer=NULL;
132 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
134 else if(imageType == VTK_SIGNED_CHAR)
139 // pointers to get into the image
140 signed char* dataImagePointer1=NULL;
141 signed char* dataImagePointer2=NULL;
142 signed char* dataImageResultPointer=NULL;
144 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
146 else if(imageType == VTK_SHORT)
151 // pointers to get into the image
152 short* dataImagePointer1=NULL;
153 short* dataImagePointer2=NULL;
154 short* dataImageResultPointer=NULL;
156 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
158 else if(imageType == VTK_UNSIGNED_SHORT)
163 // pointers to get into the image
164 unsigned short* dataImagePointer1=NULL;
165 unsigned short* dataImagePointer2=NULL;
166 unsigned short* dataImageResultPointer=NULL;
168 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
170 else if(imageType == VTK_INT)
175 // pointers to get into the image
176 int* dataImagePointer1=NULL;
177 int* dataImagePointer2=NULL;
178 int* dataImageResultPointer=NULL;
180 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
182 else if(imageType == VTK_UNSIGNED_INT)
187 // pointers to get into the image
188 unsigned int* dataImagePointer1=NULL;
189 unsigned int* dataImagePointer2=NULL;
190 unsigned int* dataImageResultPointer=NULL;
192 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
194 else if(imageType == VTK_LONG)
199 // pointers to get into the image
200 long* dataImagePointer1=NULL;
201 long* dataImagePointer2=NULL;
202 long* dataImageResultPointer=NULL;
204 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
206 else if(imageType == VTK_UNSIGNED_LONG)
211 // pointers to get into the image
212 unsigned long* dataImagePointer1=NULL;
213 unsigned long* dataImagePointer2=NULL;
214 unsigned long* dataImageResultPointer=NULL;
216 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
218 else if(imageType == VTK_FLOAT)
223 // pointers to get into the image
224 float* dataImagePointer1=NULL;
225 float* dataImagePointer2=NULL;
226 float* dataImageResultPointer=NULL;
228 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
230 else if(imageType == VTK_DOUBLE)
235 // pointers to get into the image
236 double* dataImagePointer1=NULL;
237 double* dataImagePointer2=NULL;
238 double* dataImageResultPointer=NULL;
240 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
245 void Substraction::substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2)
247 // we start where the image starts
248 dataImagePointer1=(T*)imageData1->GetScalarPointer(0,0,0);
249 dataImagePointer2=(T*)imageData2->GetScalarPointer(0,0,0);
250 dataImageResultPointer=(T*)imageResult->GetScalarPointer(0,0,0);
255 imageData1->GetExtent(ext);
265 //walking in the image
266 int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
267 double sum1=0,sum2=0;
275 // this is for getting just the grey level in that position
276 //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
278 // we get the pointer to the position (i,j,k)y that way we can get the
279 //grey level and we can change it
281 dataImagePointer1=(T*)imageData1->GetScalarPointer(i,j,k);
282 dataImagePointer2=(T*)imageData2->GetScalarPointer(i,j,k);
283 dataImageResultPointer=(T*)imageResult->GetScalarPointer(i,j,k);
285 sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
287 sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
289 if((sum1 - sum2) < lZeroLevel)
291 dataImageResultPointer[0] =(T) lowerColor[0];
292 dataImageResultPointer[1] =(T) lowerColor[1];
293 dataImageResultPointer[2] =(T) lowerColor[2];
296 else if((sum1 - sum2) > uZeroLevel)
298 dataImageResultPointer[0] =(T) upperColor[0];
299 dataImageResultPointer[1] =(T) upperColor[1];
300 dataImageResultPointer[2] =(T) upperColor[2];
305 dataImageResultPointer[0] =(T) mediumColor[0];
306 dataImageResultPointer[1] =(T) mediumColor[1];
307 dataImageResultPointer[2] =(T) mediumColor[2];
317 Returns the filtered image
319 vtkImageData* Substraction::getSubstractedImage()
327 int Substraction::getImageSize()