-
- MyImageSubstraction::MyImageSubstraction(vtkImageData* imageData1,vtkImageData* imageData2, int uZLevel,int lZLevel, std::vector<double> uColor, std::vector<double> lColor, std::vector<double> mColor)
-{
- imageResult= vtkImageData::New();
- sizeImage=0;
- uZeroLevel=uZLevel;
- lZeroLevel=lZLevel;
- if(uColor.size() != NULL)
- {
- upperColor[0] = uColor[0];
- upperColor[1] = uColor[1];
- upperColor[2] = uColor[2];
- }
- else
- {
- upperColor[0] = 255;
- upperColor[1] = 255;
- upperColor[2] = 255;
- }
- if(mColor.size() != NULL)
- {
- mediumColor[0] = mColor[0];
- mediumColor[1] = mColor[1];
- mediumColor[2] = mColor[2];
- }
- else
- {
- mediumColor[0] = 125;
- mediumColor[1] = 125;
- mediumColor[2] = 125;
- }
- if(lColor.size() != NULL)
- {
- lowerColor[0] = lColor[0];
- lowerColor[1] = lColor[1];
- lowerColor[2] = lColor[2];
- }
- else
- {
- lowerColor[0] = 0;
- lowerColor[1] = 0;
- lowerColor[2] = 0;
- }
-
- //Original image type this case is an unsigned char (3)
- int t=imageData1->GetScalarType();
- //substracting the image
- substractImage(imageData1, imageData2);
-}
-
-MyImageSubstraction::~MyImageSubstraction()
-{
- if(imageResult!=NULL)imageResult->Delete();
-}
-
-//----------------------------------------------------------------------------
-// Methods
-//----------------------------------------------------------------------------
-
-
-/*
-Calculate the new image and save it in the attribute imageResult
-it is used if the user had given the imageData
-*/
-void MyImageSubstraction::substractImage(vtkImageData* imageData1, vtkImageData* imageData2)
-{
- //dimensions of the image (extent)
- int ext[6];
- //setting the dimensionality (1d or 2d or 3d )
- int newDim[3];
- //image spacing
- double spc[3];
-
- //getting the information from the original image
- imageData1->GetSpacing(spc);
- imageData1->GetExtent(ext);
-
- //this a 2d image
- newDim[0]=ext[1]-ext[0]+1;
- newDim[1]=ext[3]-ext[2]+1;
- newDim[2]=1;// in general it is ext[5]-ext[4]+1
-
-
- //initializing the image that represents the substracted image
- initialize(newDim,spc);
- //Time to substract
- substract(imageData1, imageData2);
-}
-
-/*
- getting ready the imageResult
-*/
-void MyImageSubstraction::initialize(int dimensions[], double spacing[])
-{
- //setting image data of the imageResult
- imageResult->SetScalarTypeToUnsignedChar();
- imageResult->SetSpacing(spacing);
- imageResult->SetDimensions(dimensions);
- imageResult->AllocateScalars();
- imageResult->Update();
-}
-
-/*
- Setting the values for the
-*/
-void MyImageSubstraction::substract(vtkImageData* imageData1, vtkImageData* imageData2)
-{
- /*
- images pointers
- */
- //the new image type is unsigned short (5)
- int t=imageData1->GetScalarType();
-
- // pointers to get into the image
- unsigned char* dataImagePointer1=NULL;
- unsigned char* dataImagePointer2=NULL;
- unsigned char* dataImageResultPointer=NULL;
- // we start where the image starts
- dataImagePointer1=(unsigned char*)imageData1->GetScalarPointer(0,0,0);
- dataImagePointer2=(unsigned char*)imageData2->GetScalarPointer(0,0,0);
- dataImageResultPointer=(unsigned char*)imageResult->GetScalarPointer(0,0,0);
-
- /*
- Image Size
- */
- int ext[6];
- imageData1->GetExtent(ext);
- int sx,sy,sz;
- sx=ext[1]-ext[0]+1;
- sy=ext[3]-ext[2]+1;
- sz=ext[5]-ext[4]+1;
-
- sizeImage=sx*sy*sz;
-
- //-----------------
- //A3
- //-----------------
- //walking in the image
- int i=0,j=0,k=0,counter=0,nU=0,nL=0,nZ=0;
- double sum1=0,sum2=0;
- for(i=0;i<sx;i++)
- {
- for(j=0;j<sy;j++)
- {
- for(k=0;k<sz;k++)
- {
-
- // this is for getting just the grey level in that position
- //originalValue=(short)imageData->GetScalarComponentAsFloat(i,j,k,0);
-
- // we get the pointer to the position (i,j,k)y that way we can get the
- //grey level and we can change it
- dataImagePointer1=(unsigned char*)imageData1->GetScalarPointer(i,j,k);
- dataImagePointer2=(unsigned char*)imageData2->GetScalarPointer(i,j,k);
- dataImageResultPointer=(unsigned char*)imageResult->GetScalarPointer(i,j,k);
-
- sum1=(int)(dataImagePointer1[0]) + (int)(dataImagePointer1[1]) + (int)(dataImagePointer1[2]);
- sum1=sum1/3;
- sum2=(int)(dataImagePointer2[0]) + (int)(dataImagePointer2[1]) + (int)(dataImagePointer2[2]);
- sum2=sum2/3;
- if((sum1 - sum2) < lZeroLevel)
- {
- dataImageResultPointer[0] =(unsigned char) lowerColor[0];
- dataImageResultPointer[1] =(unsigned char) lowerColor[1];
- dataImageResultPointer[2] =(unsigned char) lowerColor[2];
- nL++;
- }
- else if((sum1 - sum2) > uZeroLevel)
- {
- dataImageResultPointer[0] =(unsigned char) upperColor[0];
- dataImageResultPointer[1] =(unsigned char) upperColor[1];
- dataImageResultPointer[2] =(unsigned char) upperColor[2];
- nU++;
- }
- else
- {
- dataImageResultPointer[0] =(unsigned char) mediumColor[0];
- dataImageResultPointer[1] =(unsigned char) mediumColor[1];
- dataImageResultPointer[2] =(unsigned char) mediumColor[2];
- nZ++;
- }
- counter++;
- }
- }
- }
-}
-/*
-Returns the filtered image
-*/
-vtkImageData* MyImageSubstraction::getSubstractedImage()
-{
- return imageResult;
-}
-
-/*
- Get Image Size
-*/
-int MyImageSubstraction::getImageSize()
-{
- return sizeImage;
-}
-