]> Creatis software - creaRigidRegistration.git/blob - lib/Substraction.cxx
*** empty log message ***
[creaRigidRegistration.git] / lib / Substraction.cxx
1 #include "Substraction.h"
2
3 Substraction::Substraction(vtkImageData* imageData1,vtkImageData* imageData2, int uZLevel,int lZLevel, std::vector<double> uColor, std::vector<double> lColor, std::vector<double> mColor)
4 {
5         imageResult= vtkImageData::New();
6         sizeImage=0;
7         uZeroLevel=uZLevel;
8         lZeroLevel=lZLevel;
9         if(uColor.size() != NULL)
10         {
11                 upperColor[0] = uColor[0];
12                 upperColor[1] = uColor[1];
13                 upperColor[2] = uColor[2];
14         }
15         else
16         {
17                 upperColor[0] = 255;
18                 upperColor[1] = 255;
19                 upperColor[2] = 255;
20         }       
21         if(mColor.size() != NULL)
22         {
23                 mediumColor[0] = mColor[0];
24                 mediumColor[1] = mColor[1];
25                 mediumColor[2] = mColor[2];
26         }
27         else
28         {
29                 mediumColor[0] = 125;
30                 mediumColor[1] = 125;
31                 mediumColor[2] = 125;
32         }       
33         if(lColor.size() != NULL)
34         {
35                 lowerColor[0] = lColor[0];
36                 lowerColor[1] = lColor[1];
37                 lowerColor[2] = lColor[2];
38         }
39         else
40         {
41                 lowerColor[0] = 0;
42                 lowerColor[1] = 0;
43                 lowerColor[2] = 0;
44         }
45         
46         //Original image type this case is an unsigned char (3)
47         int t=imageData1->GetScalarType();
48         //substracting the image
49         substractImage(imageData1, imageData2);
50 }
51
52 Substraction::~Substraction()
53 {
54         if(imageResult!=NULL)imageResult->Delete();
55 }
56
57 //----------------------------------------------------------------------------
58 // Methods
59 //----------------------------------------------------------------------------
60
61
62 /*
63 Calculate the new image and save it in the attribute imageResult
64 it is used if the user had given the imageData
65 */
66 void Substraction::substractImage(vtkImageData* imageData1, vtkImageData* imageData2)
67 {
68         //dimensions of the image (extent)
69         int ext[6];
70         //setting the dimensionality (1d or 2d or 3d )
71     int newDim[3];
72         //image spacing
73     double spc[3];
74   
75         //getting the information from the original image
76         imageData1->GetSpacing(spc);
77         imageData1->GetExtent(ext);
78         
79         //this a 2d image
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
83
84
85         imageType = imageData1->GetScalarType();
86         //initializing the image that represents the substracted image
87         initialize(newDim,spc);
88         //Time to substract
89         substract(imageData1, imageData2);      
90 }
91
92 /*
93   getting ready the imageResult
94 */
95 void Substraction::initialize(int dimensions[], double spacing[])
96 {
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();
103 }
104
105 /*
106          Setting the values for the
107 */
108 void Substraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
109 {
110         if(imageType == VTK_CHAR)
111         {
112                 /*
113                 images pointers
114                 */
115                 // pointers to get into the image
116                 char* dataImagePointer1=NULL;
117                 char* dataImagePointer2=NULL;
118                 char* dataImageResultPointer=NULL;
119                 
120                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
121         }
122         else if(imageType == VTK_UNSIGNED_CHAR)
123         {
124                 /*
125                 images pointers
126                 */
127                 // pointers to get into the image
128                 unsigned char* dataImagePointer1=NULL;
129                 unsigned char* dataImagePointer2=NULL;
130                 unsigned char* dataImageResultPointer=NULL;
131                 
132                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
133         }
134         else if(imageType == VTK_SIGNED_CHAR)
135         {
136                 /*
137                 images pointers
138                 */
139                 // pointers to get into the image
140                 signed char* dataImagePointer1=NULL;
141                 signed char* dataImagePointer2=NULL;
142                 signed char* dataImageResultPointer=NULL;
143                 
144                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
145         }
146         else if(imageType == VTK_SHORT)
147         {
148                 /*
149                 images pointers
150                 */
151                 // pointers to get into the image
152                 short* dataImagePointer1=NULL;
153                 short* dataImagePointer2=NULL;
154                 short* dataImageResultPointer=NULL;
155
156                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);          
157         }
158         else if(imageType == VTK_UNSIGNED_SHORT)
159         {
160                 /*
161                 images pointers
162                 */
163                 // pointers to get into the image
164                 unsigned short* dataImagePointer1=NULL;
165                 unsigned short* dataImagePointer2=NULL;
166                 unsigned short* dataImageResultPointer=NULL;
167                 
168                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
169         }
170         else if(imageType == VTK_INT)
171         {
172                 /*
173                 images pointers
174                 */
175                 // pointers to get into the image
176                 int* dataImagePointer1=NULL;
177                 int* dataImagePointer2=NULL;
178                 int* dataImageResultPointer=NULL;
179                 
180                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
181         }
182         else if(imageType == VTK_UNSIGNED_INT)
183         {
184                 /*
185                 images pointers
186                 */
187                 // pointers to get into the image
188                 unsigned int* dataImagePointer1=NULL;
189                 unsigned int* dataImagePointer2=NULL;
190                 unsigned int* dataImageResultPointer=NULL;
191                 
192                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
193         }
194         else if(imageType == VTK_LONG)
195         {
196                 /*
197                 images pointers
198                 */
199                 // pointers to get into the image
200                 long* dataImagePointer1=NULL;
201                 long* dataImagePointer2=NULL;
202                 long* dataImageResultPointer=NULL;
203
204                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
205         }
206         else if(imageType == VTK_UNSIGNED_LONG)
207         {
208                 /*
209                 images pointers
210                 */
211                 // pointers to get into the image
212                 unsigned long* dataImagePointer1=NULL;
213                 unsigned long* dataImagePointer2=NULL;
214                 unsigned long* dataImageResultPointer=NULL;
215                 
216                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
217         }
218         else if(imageType == VTK_FLOAT)
219         {
220                 /*
221                 images pointers
222                 */
223                 // pointers to get into the image
224                 float* dataImagePointer1=NULL;
225                 float* dataImagePointer2=NULL;
226                 float* dataImageResultPointer=NULL;
227                 
228                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
229         }
230         else if(imageType == VTK_DOUBLE)
231         {
232                 /*
233                 images pointers
234                 */
235                 // pointers to get into the image
236                 double* dataImagePointer1=NULL;
237                 double* dataImagePointer2=NULL;
238                 double* dataImageResultPointer=NULL;
239                 
240                 substractByType(dataImagePointer1, dataImagePointer2, dataImageResultPointer, imageData1, imageData2);
241         }
242 }
243
244 template <class T> 
245 void Substraction::substractByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *imageData1, vtkImageData *imageData2)
246 {
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);
251         /*
252         Image Size
253         */
254         int ext[6];
255         imageData1->GetExtent(ext);
256         int sx,sy,sz;
257         sx=ext[1]-ext[0]+1;
258         sy=ext[3]-ext[2]+1;
259         sz=ext[5]-ext[4]+1;
260
261         sizeImage=sx*sy*sz;
262         //-----------------
263         //A3    
264         //-----------------
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;
268         for(i=0;i<sx;i++)
269         {
270                 for(j=0;j<sy;j++)
271                 {
272                         for(k=0;k<sz;k++)
273                         {
274                                         
275                                 // this is for getting just the grey level in that position
276                                 //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
277                 
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
280
281                                 dataImagePointer1=(T*)imageData1->GetScalarPointer(i,j,k);
282                                 dataImagePointer2=(T*)imageData2->GetScalarPointer(i,j,k);
283                                 dataImageResultPointer=(T*)imageResult->GetScalarPointer(i,j,k);
284
285                                 sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
286                                 sum1=sum1/3;
287                                 sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
288                                 sum2=sum2/3;                            
289                                 if((sum1 - sum2) < lZeroLevel)
290                                 {
291                                         dataImageResultPointer[0] =(T) lowerColor[0];
292                                         dataImageResultPointer[1] =(T) lowerColor[1];
293                                         dataImageResultPointer[2] =(T) lowerColor[2];
294                                         nL++;
295                                 }
296                                 else if((sum1 - sum2) > uZeroLevel)
297                                 {
298                                         dataImageResultPointer[0] =(T) upperColor[0];
299                                         dataImageResultPointer[1] =(T) upperColor[1];
300                                         dataImageResultPointer[2] =(T) upperColor[2];
301                                         nU++;
302                                 }
303                                 else
304                                 {
305                                         dataImageResultPointer[0] =(T) mediumColor[0];
306                                         dataImageResultPointer[1] =(T) mediumColor[1];
307                                         dataImageResultPointer[2] =(T) mediumColor[2];
308                                         nZ++;
309                                 }                               
310                                 counter++;
311                         }
312                 }
313         }
314 }
315
316 /*
317 Returns the filtered image
318 */
319 vtkImageData* Substraction::getSubstractedImage()
320 {
321         return imageResult;
322 }
323
324 /*
325         Get Image Size
326 */
327 int Substraction::getImageSize()
328
329         return sizeImage;
330 }