]> Creatis software - creaRigidRegistration.git/blob - lib/CheckBoard.cpp
*** empty log message ***
[creaRigidRegistration.git] / lib / CheckBoard.cpp
1 #include "CheckBoard.h"
2
3
4 /*
5 * Constructor
6 */
7 //------------------------------------------------------------
8 CheckBoard::CheckBoard()
9 {
10         image1=NULL;
11         image2=NULL;
12         squaresX=0;
13         squaresY=0;
14         newImage = NULL;
15         processed=false;
16         newImage = NULL;
17         resample = NULL;
18         result = NULL;
19 }
20
21 /*
22 * Destructor
23 */
24 //------------------------------------------------------------
25 CheckBoard::~CheckBoard()
26 {
27         //if (image1 != NULL ) { image1->Delete(); }
28         //if (image2 != NULL ) { image2->Delete(); }
29         if (newImage != NULL ) { newImage->Delete(); }
30         if (resample != NULL ) { resample->Delete(); }
31         if (result != NULL ) { result->Delete(); }
32 }
33
34 //------------------------------------------------------------
35 void CheckBoard::calculateImage()
36 {
37
38         if(image1!=NULL && image2!=NULL)
39         {
40
41                 // IMAGE 1
42                         
43                 // Information from image1
44                 image1->GetSpacing(spcImg1);
45                 image1->GetExtent(extImg1);
46
47                 type = image1->GetScalarType();
48                 
49                 // The Z dimension is 1 for a 2D image
50                 dimImg1[0] = extImg1[1] - extImg1[0]+1;
51                 dimImg1[1] = extImg1[3] - extImg1[2]+1;
52                 dimImg1[2] = 1;
53
54                 //IMAGE 2
55
56                 // Information from image2
57                 image2->GetSpacing(spcImg2);
58                 image2->GetExtent(extImg2);
59                 
60                 // The Z dimension is 1 for a 2D image
61                 dimImg2[0] = extImg2[1] - extImg2[0]+1;
62                 dimImg2[1] = extImg2[3] - extImg2[2]+1;
63                 dimImg2[2] = 1;
64
65                 long numPixelsImg1 = dimImg1[0]*dimImg1[1];
66                 long numPixelsImg2 = dimImg2[0]*dimImg2[1];
67                 
68                 double factorX = 1;
69                 double factorY = 1;
70                 resample = vtkImageResample::New();
71                 result = vtkImageData::New();
72                 if(numPixelsImg1<numPixelsImg2)
73                 {
74                         resample->SetInput(image1);
75                         factorX = (double)extImg2[1]/extImg1[1];
76                         factorY = (double)extImg2[3]/extImg1[3];
77                         resample->SetAxisMagnificationFactor(0,factorX);
78                         resample->SetAxisMagnificationFactor(1,factorY);
79                         resample->SetInformationInput(image2);
80
81                         initialize(dimImg2, spcImg2);
82                         result = resample->GetOutput();
83                         result->Update();
84                         createImage(result,image2,dimImg2[0],dimImg2[1]);               
85                         
86                 } //if
87                 else if (numPixelsImg1>numPixelsImg2)
88                 {
89                         resample->SetInput(image2);
90                         factorX = (double)extImg1[1]/extImg2[1];
91                         factorY = (double)extImg1[3]/extImg2[3];
92                         resample->SetAxisMagnificationFactor(0,factorX);
93                         resample->SetAxisMagnificationFactor(1,factorY);
94                         resample->SetInformationInput(image1);
95
96                         initialize(dimImg1, spcImg1);
97                         result = resample->GetOutput();
98                         
99                         result->Update();
100
101                         createImage(image1,result,dimImg1[0],dimImg1[1]);
102                 } // else if
103                 else
104                 {
105                         //If both images have the same number of pixels, the resultant image will have the 
106                         //properties of the first image.
107                         resample->SetInput(image2);
108                         factorX = (double)extImg1[1]/extImg2[1];
109                         factorY = (double)extImg1[3]/extImg2[3];
110                         resample->SetAxisMagnificationFactor(0,factorX);
111                         resample->SetAxisMagnificationFactor(1,factorY);
112                         resample->SetInformationInput(image1);
113
114                         initialize(dimImg1, spcImg1);
115
116                         result = resample->GetOutput();
117                         result->Update();
118                         
119                         createImage(image1,result,dimImg1[0],dimImg1[1]);
120
121                 } //else
122                 
123                 resample->Delete();             
124                 processed=true;
125         }
126 }
127
128 //------------------------------------------------------------
129 void CheckBoard::initialize(int dimensions[], double spacing[])
130 {
131         // Setting the new image
132         if(newImage != NULL){
133                         newImage->Delete();
134         }
135         newImage = vtkImageData::New();
136         newImage->SetScalarType(type);
137         newImage->SetSpacing(spacing);
138         newImage->SetDimensions(dimensions);
139         newImage->AllocateScalars();
140         newImage->Update();
141         
142
143 }
144
145 //------------------------------------------------------------
146 void CheckBoard::createImage(vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
147 {
148         int imageType=img1->GetScalarType();
149
150         if(imageType == VTK_CHAR)
151         {
152                 //POINTERS: 
153                 char* dataImagePointer1 = NULL;
154                 char* dataImagePointer2 = NULL;
155                 char* dataImageResultPointer = NULL;
156
157                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
158         }
159         else if(imageType == VTK_SIGNED_CHAR)
160         {
161                 //POINTERS: 
162                 signed char* dataImagePointer1 = NULL;
163                 signed char* dataImagePointer2 = NULL;
164                 signed char* dataImageResultPointer = NULL;
165
166                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
167         }
168         else if(imageType == VTK_UNSIGNED_CHAR)
169         {
170                 //POINTERS: 
171                 unsigned char* dataImagePointer1 = NULL;
172                 unsigned char* dataImagePointer2 = NULL;
173                 unsigned char* dataImageResultPointer = NULL;
174
175                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
176         }
177         else if(imageType == VTK_SHORT)
178         {
179         //POINTERS: 
180                 short* dataImagePointer1 = NULL;
181                 short* dataImagePointer2 = NULL;
182                 short* dataImageResultPointer = NULL;
183
184                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
185         }
186         else if(imageType == VTK_UNSIGNED_SHORT)
187         {
188                 //POINTERS: 
189                 unsigned short* dataImagePointer1 = NULL;
190                 unsigned short* dataImagePointer2 = NULL;
191                 unsigned short* dataImageResultPointer = NULL;
192
193                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
194         }
195
196         else if(imageType == VTK_INT)
197         {
198                 //POINTERS: 
199                 int* dataImagePointer1 = NULL;
200                 int* dataImagePointer2 = NULL;
201                 int* dataImageResultPointer = NULL;
202
203                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
204         }
205         else if(imageType == VTK_UNSIGNED_INT)
206         {
207                 //POINTERS: 
208                 unsigned int* dataImagePointer1 = NULL;
209                 unsigned int* dataImagePointer2 = NULL;
210                 unsigned int* dataImageResultPointer = NULL;
211
212                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
213         }
214         else if(imageType == VTK_LONG)
215         {
216                 //POINTERS: 
217                 long* dataImagePointer1 = NULL;
218                 long* dataImagePointer2 = NULL;
219                 long* dataImageResultPointer = NULL;
220
221                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
222         }
223         else if(imageType == VTK_UNSIGNED_LONG)
224         {
225                 //POINTERS: 
226                 unsigned long* dataImagePointer1 = NULL;
227                 unsigned long* dataImagePointer2 = NULL;
228                 unsigned long* dataImageResultPointer = NULL;
229
230                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
231         }
232         else if(imageType == VTK_FLOAT)
233         {
234                 //POINTERS: 
235                 float* dataImagePointer1 = NULL;
236                 float* dataImagePointer2 = NULL;
237                 float* dataImageResultPointer = NULL;
238
239                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
240         }
241         else if(imageType == VTK_DOUBLE)
242         {
243                 //POINTERS: 
244                 double* dataImagePointer1 = NULL;
245                 double* dataImagePointer2 = NULL;
246                 double* dataImageResultPointer = NULL;
247
248                 createImageByType(dataImagePointer1,dataImagePointer2,dataImageResultPointer,img1,img2,sizeX,sizeY);
249         }
250
251                 
252         //std::cout << "The image has been checkboardized!" << std::endl;
253    newImage->Update();
254 //   newImage->Modified();
255 }
256 /**Template used for the different image types
257 */
258 template <class T>
259 void CheckBoard::createImageByType(T* dataImagePointer1, T* dataImagePointer2, T* dataImageResultPointer, vtkImageData *img1, vtkImageData *img2, int sizeX, int sizeY)
260 {
261         dataImagePointer1 = (T*) img1->GetScalarPointer(0,0,0);
262         dataImagePointer2 = (T*) img2->GetScalarPointer(0,0,0);
263         dataImageResultPointer = (T*) newImage->GetScalarPointer(0,0,0);
264         
265         if(squaresX == 0)
266         {
267                 squaresX = 1;
268         }
269         if(squaresY == 0)
270         {
271                 squaresY = 1;
272         }    
273
274         int divX = sizeX/squaresX;
275         int divY = sizeY/squaresY;
276         int i, j, counterX, counterY;
277         for(i = 0; i < sizeX; i++)
278         {
279                 counterY = 0; 
280                 for(j = 0; j < sizeY; j++)
281                 {
282                         dataImagePointer1 = (T*)img1->GetScalarPointer(i,j,0);
283                         dataImagePointer2 = (T*)img2->GetScalarPointer(i,j,0);
284                         dataImageResultPointer = (T*)newImage->GetScalarPointer(i,j,0);
285                         if( (counterX <= divX && counterY <= divY) || ((counterX >= divX && counterX <= 2*divX) && (counterY >= divY && counterY <= 2*divY)) )
286                         {
287                                 *dataImageResultPointer = (T) *dataImagePointer1;
288                         }
289                         else //if( ((counterX >= divX && counterX <= 2*divX) && counterY <= divY) || (counterX <= divX && (counterY >= divY && counterY <= 2*divY)) )
290                         {
291                                 *dataImageResultPointer = (T) *dataImagePointer2;
292                         }
293
294                         if(counterX >= 2*divX)
295                         {
296                                 counterX = 0;
297                         }
298                         else if(counterY >= 2*divY)
299                         {
300                                 counterY = 0;
301                         }                       
302                         counterY++;
303                 }
304                 counterX++;
305         }
306 }
307
308 /*
309 * Get new image
310 */
311 //------------------------------------------------------------
312 vtkImageData* CheckBoard::getFilteredImage()
313 {
314         if(processed){
315                 return newImage;
316         }
317         else{
318                 return NULL;
319         }
320 }
321
322 //------------------------------------------------------------
323 void CheckBoard::setInputImage1(vtkImageData *_image)
324 {
325         image1=_image;
326 }
327
328 //------------------------------------------------------------
329 void CheckBoard::setInputImage2(vtkImageData *_image)
330 {
331         image2=_image;
332 }
333
334 //------------------------------------------------------------
335 void CheckBoard::setCols(int cols)
336 {
337         squaresX=cols;
338 }
339
340 //------------------------------------------------------------
341 void CheckBoard::setRows(int rows)
342 {
343         squaresY=rows;
344 }
345