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();
11 upperColor[0] = uColor[0];
12 upperColor[1] = uColor[1];
13 upperColor[2] = uColor[2];
21 if(mColor.size() != 0)
23 mediumColor[0] = mColor[0];
24 mediumColor[1] = mColor[1];
25 mediumColor[2] = mColor[2];
33 if(lColor.size() != 0)
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(); // JPR : unused
48 //substracting the image
49 substractImage(imageData1, imageData2);
52 Substraction::~Substraction()
54 if(imageResult!=NULL)imageResult->Delete();
57 //----------------------------------------------------------------------------
59 //----------------------------------------------------------------------------
62 Calculate the new image and save it in the attribute imageResult
63 it is used if the user had given the imageData
65 void Substraction::substractImage(vtkImageData* imageData1, vtkImageData* imageData2)
67 //dimensions of the image (extent)
69 //setting the dimensionality (1d or 2d or 3d )
74 //getting the information from the original image
75 imageData1->GetSpacing(spc);
76 imageData1->GetExtent(ext);
79 newDim[0]=ext[1]-ext[0]+1;
80 newDim[1]=ext[3]-ext[2]+1;
81 newDim[2]=1;// in general it is ext[5]-ext[4]+1
84 imageType = imageData1->GetScalarType();
85 //initializing the image that represents the substracted image
86 initialize(newDim,spc);
88 substract(imageData1, imageData2);
92 getting ready the imageResult
94 void Substraction::initialize(int dimensions[], double spacing[])
96 //setting image data of the imageResult
97 imageResult->SetScalarType(imageType);
98 imageResult->SetSpacing(spacing);
99 imageResult->SetDimensions(dimensions);
100 imageResult->AllocateScalars();
101 imageResult->Update();
105 Setting the values for the
107 void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
109 if(imageType == VTK_CHAR)
114 // pointers to get into the image
115 char* dataImagePointer1=NULL;
116 char* dataImagePointer2=NULL;
117 char* dataImageResultPointer=NULL;
119 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
121 else if(imageType == VTK_UNSIGNED_CHAR)
126 // pointers to get into the image
127 unsigned char* dataImagePointer1=NULL;
128 unsigned char* dataImagePointer2=NULL;
129 unsigned char* dataImageResultPointer=NULL;
131 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
133 else if(imageType == VTK_SIGNED_CHAR)
138 // pointers to get into the image
139 signed char* dataImagePointer1=NULL;
140 signed char* dataImagePointer2=NULL;
141 signed char* dataImageResultPointer=NULL;
143 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
145 else if(imageType == VTK_SHORT)
150 // pointers to get into the image
151 short* dataImagePointer1=NULL;
152 short* dataImagePointer2=NULL;
153 short* dataImageResultPointer=NULL;
155 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
157 else if(imageType == VTK_UNSIGNED_SHORT)
162 // pointers to get into the image
163 unsigned short* dataImagePointer1=NULL;
164 unsigned short* dataImagePointer2=NULL;
165 unsigned short* dataImageResultPointer=NULL;
167 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
169 else if(imageType == VTK_INT)
174 // pointers to get into the image
175 int* dataImagePointer1=NULL;
176 int* dataImagePointer2=NULL;
177 int* dataImageResultPointer=NULL;
179 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
181 else if(imageType == VTK_UNSIGNED_INT)
186 // pointers to get into the image
187 unsigned int* dataImagePointer1=NULL;
188 unsigned int* dataImagePointer2=NULL;
189 unsigned int* dataImageResultPointer=NULL;
191 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
193 else if(imageType == VTK_LONG)
198 // pointers to get into the image
199 long* dataImagePointer1=NULL;
200 long* dataImagePointer2=NULL;
201 long* dataImageResultPointer=NULL;
203 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
205 else if(imageType == VTK_UNSIGNED_LONG)
210 // pointers to get into the image
211 unsigned long* dataImagePointer1=NULL;
212 unsigned long* dataImagePointer2=NULL;
213 unsigned long* dataImageResultPointer=NULL;
215 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
217 else if(imageType == VTK_FLOAT)
222 // pointers to get into the image
223 float* dataImagePointer1=NULL;
224 float* dataImagePointer2=NULL;
225 float* dataImageResultPointer=NULL;
227 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
229 else if(imageType == VTK_DOUBLE)
234 // pointers to get into the image
235 double* dataImagePointer1=NULL;
236 double* dataImagePointer2=NULL;
237 double* dataImageResultPointer=NULL;
239 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
244 void Substraction::substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2)
246 // we start where the image starts
247 dataImagePointer1=(T*)imageData1->GetScalarPointer(0,0,0);
248 dataImagePointer2=(T*)imageData2->GetScalarPointer(0,0,0);
249 dataImageResultPointer=(T*)imageResult->GetScalarPointer(0,0,0);
254 imageData1->GetExtent(ext);
264 //walking in the image
265 int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
266 double sum1=0,sum2=0;
274 // this is for getting just the grey level in that position
275 //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
277 // we get the pointer to the position (i,j,k)y that way we can get the
278 //grey level and we can change it
280 dataImagePointer1=(T*)imageData1->GetScalarPointer(i,j,k);
281 dataImagePointer2=(T*)imageData2->GetScalarPointer(i,j,k);
282 dataImageResultPointer=(T*)imageResult->GetScalarPointer(i,j,k);
284 sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
286 sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
288 if((sum1 - sum2) < lZeroLevel)
290 dataImageResultPointer[0] =(T) lowerColor[0];
291 dataImageResultPointer[1] =(T) lowerColor[1];
292 dataImageResultPointer[2] =(T) lowerColor[2];
295 else if((sum1 - sum2) > uZeroLevel)
297 dataImageResultPointer[0] =(T) upperColor[0];
298 dataImageResultPointer[1] =(T) upperColor[1];
299 dataImageResultPointer[2] =(T) upperColor[2];
304 dataImageResultPointer[0] =(T) mediumColor[0];
305 dataImageResultPointer[1] =(T) mediumColor[1];
306 dataImageResultPointer[2] =(T) mediumColor[2];
316 Returns the filtered image
318 vtkImageData* Substraction::getSubstractedImage()
326 int Substraction::getImageSize()