[Insight-users] Number and size of regions?

Christian Marshall Rieck rieck at stud.ntnu.no
Tue Mar 27 09:47:03 EST 2007


Is there any way of getting the number and sizes of different regions in 
an image? I have thresholded it and have a binary image with black 
background with 1..N white (val=255) blobs in 3D. 

I tried to iterate through it and look for values of 255, and if found 
deliver this to a neighborhoodfilter to set it to a unique value. (then 
value++). 
Then the final value of value would give me the number of segments and a 
histogram of the resulting image would give me the size of the regions. 
However, this does not work. I pass the image::pointer to the 
markRegions-function but the changes made there is not detected back in 
the iteration. MarkRegion is called the number of white voxels in the 
original image. 

Code:


template<class ImageType>
int markDifferentSegments(typename ImageType::Pointer image){

	std::cout << "called markDifferent" << std::endl;
	typedef itk::ImageRegionIterator<ImageType> itType;
	ImageType::IndexType index;
	int color = 1;
	itType it(image, image->GetRequestedRegion());
	it.GoToBegin();
	while(!(it.IsAtEnd())) {
		if(it.Get() == 255) {
			markRegion<ImageType>(image, color, 
it.GetIndex());
			color++;
		}
		++it;
	}

	std::cout << (color-1) << " regioner funnet. " << std::endl;
	WriteImageFile<ImageType>(image, "markTest.mhd");
	return color;
}


template<class ImageType>
typename ImageType::Pointer markRegion(typename ImageType::Pointer image, 
int color, typename ImageType::IndexType index) {

	ImageType::Pointer ret;
	typedef itk::NeighborhoodConnectedImageFilter<ImageType, 
ImageType> neighType;
	neighType::Pointer neigh = neighType::New();
	neigh->SetInput(image);
	neigh->SetSeed(index);
	neigh->SetReplaceValue(color);

	ImageType::SizeType radius;
	radius[0] = 0;
	radius[1] = 0;
	radius[2] = 0;
	neigh->SetRadius(radius);

	ImageType::Pointer ret = neigh->GetOutput();
	try{
		ret->Update();
	}
	catch(itk::ExceptionObject &exep) {
		std::cerr << exep;
	}


	std::cout << "returning " << color << std::endl; 
	return ret;
}



best regards,
Christian Marshall Rieck
93 49 54 75
----------------------------------------
All the good things in life are
fattening, cancer-causing or NP-complete.



More information about the Insight-users mailing list