#include "bbUtilitiesMetzRegionGrowing.h" #include "bbUtilitiesPackage.h" namespace bbUtilities { BBTK_ADD_BLACK_BOX_TO_PACKAGE(Utilities,MetzRegionGrowing) BBTK_USER_BLACK_BOX_IMPLEMENTATION(MetzRegionGrowing,bbtk::AtomicBlackBox); void MetzRegionGrowing::Process() { //-------------------------- // Getting the inputs //-------------------------- initialImage=bbGetInputInImage(); upper=bbGetInputUpper(); lower =bbGetInputLower(); std::vector inputPoint = bbGetInputSeedPoint(); if(inputPoint.size()==3) seedPoint=inputPoint; //-------------------------- //Initializing the outputs //-------------------------- if(filteredImage==NULL) filteredImage = vtkImageData::New(); //TYPE filteredImage->SetScalarTypeToUnsignedShort(); filteredImage->SetNumberOfScalarComponents(1); //SPACING double* spc = initialImage->GetSpacing(); filteredImage->SetSpacing(spc[0], spc[1], spc[2]); filteredImage->SetOrigin(0, 0, 0); //SIZE initialImage->GetExtent(ext); filteredImage->SetExtent(ext); //GETTING MEMORY filteredImage->AllocateScalars(); //PUTTING THE IMAGE IN BLACK unsigned short* imgptr= (unsigned short*)filteredImage->GetScalarPointer(0,0,0); int sx=ext[1]-ext[0]+1; int sy=ext[3]-ext[2]+1; int sz=ext[5]-ext[4]+1; for(int i = 0; i < sx*sy*sz; i++){ ptr[i] = 0; } //-------------------------- //Doing the connectivity //-------------------------- int seedXpoint = seedPoint[0]; int seedYpoint = seedPoint[1]; int seedZpoint = seedPoint[2]; connectivity(seedXpoint, seedYpoint, seedZpoint, 1); //------------------------------------ // Analizing the connectivity results //------------------------------------ for(int i=ext[0]; i<=ext[1]; i++) { for(int j=ext[1]; j<=ext[2]; j++) { for(int k=ext[3]; k<=ext[4]; k++) { unsigned short * imagptr= (unsigned short*)filteredImage->GetScalarPointer(i,j,k); int grey=*imagptr; if(grey == pow(2,16)) *imagptr=(unsigned short)0; else *imagptr =(unsigned short)255; } } } //----------------------- // Setting the outputs //----------------------- bbSetOutputOutImage(filteredImage); } //------------------------------------------------ void MetzRegionGrowing::bbUserConstructor() { //Default values upper=1900; lower=1174; bbSetInputInImage(NULL); bbSetOutputOutImage(NULL); bbSetInputUpper(upper); bbSetInputLower(lower); //seed seedPoint.push_back(0); seedPoint.push_back(0); seedPoint.push_back(0); bbSetInputSeedPoint(seedPoint); //initial image initialImage=NULL; //filtered image filteredImage=NULL; //actual iteration firstIteration = new std::vector(); //valid Neighbors secondIteration = new std::vector(); //valid neighbors of the neighbors thirdIteration = new std::vector(); } /* * Does the metz connectivity algorithm */ void MetzRegionGrowing:: connectivity (int x, int y, int z, int iteration) { //mark the point with the iteration number mark(x,y,z, iteration); //Verifying iterations MetzData* metzData= new MetzData(x,y,z); //we are in the seed point if(iteration==1) { firstIteration->push_back(metzData); } //if are the neighbors of the seed else if(iteration==2) { secondIteration->push_back(metzData); } // neighbors of the neigbors else if(iteration==3) { thirdIteration->push_back(metzData); } else { //Verifying voxels conditions int thirdIterationSize= thirdIteration->size(); if(thirdIterationSize >= 3000) { //we mark all the points in the firstIteration as not valid for(int i= 0 ; i < firstIteration->size(); i++) { MetzData* point = firstIteration->at(i); int pointx=point->getX(); int pointy=point->getY(); int pointz=point->getZ(); mark(pointx,pointy,pointz,-1); } //cleaning the firsIteration data cleanVector(firstIteration) ; firstIteration= secondIteration; //cleaning the secondIteration data cleanVector(secondIteration); secondIteration= thirdIteration; cleanVector(thirdIteration); } else if(thirdIterationSize==0) thirdIteration->push_back(metzData); } //we are looking for the (x,y,z) neighbors for(int i=x-1; i<=x+1; i++) { for(int j=y-1; j<=y+1; j++) { for(int k=z-1; k<=z+1; k++) { if(i >=ext[0] && i <= ext[1] && y >=ext[2] && y <= ext[3] && z >=ext[4] && z <= ext[5]) { //verifying if the level of grey is in the threshold unsigned short * imagptr= (unsigned short*)initialImage->GetScalarPointer(i,j,k); int greyLevel =(int)*imagptr; if(greyLevel >= lower && greyLevel<= upper && !isMarked(i,j,k) && i != x && j != y && k != z) { iteration++; connectivity(i,j,k,iteration); } } } } } } /* * Cleaning the vector given */ void MetzRegionGrowing::cleanVector(std::vector* v) { for(int i= 0 ; i < v->size(); i++) { MetzData* point = v->at(i); delete point; } v->clear(); } /* * Mark the point (x,y,z) with the mark given */ void MetzRegionGrowing::mark(int x,int y,int z, int mark) { unsigned short * imagptr= (unsigned short*) filteredImage->GetScalarPointer(x,y,z); *imagptr = mark; imagptr = NULL; } /* * Verify if the point is already marked with some mark and return the mark value */ bool MetzRegionGrowing::isMarked(int x,int y,int z) { int * imagptr= (int*)filteredImage->GetScalarPointer(x,y,z); int greyLevel =(int)*imagptr; imagptr = NULL; if(greyLevel!=0) return true; return false; } void MetzRegionGrowing::bbUserCopyConstructor() { } void MetzRegionGrowing::bbUserDestructor() { } } // EO namespace bbUtilities