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