Exemplo n.º 1
0
//This function removes all objects that are <= minObjSize from the foreground.
//The foreground remains grayscale after this filter
void Preprocess::RemoveConnectedComponents(int minObjSize)
{
	typedef itk::Image< unsigned short, 3 > ShortImageType;
	typedef itk::ConnectedComponentImageFilter< ImageType3D, ShortImageType > CCFilterType;
	typedef itk::RelabelComponentImageFilter< ShortImageType, ShortImageType > RelabelType;

	CCFilterType::Pointer ccfilter = CCFilterType::New();
	RelabelType::Pointer relabel = RelabelType::New();
	
	ccfilter->SetInput( myImg );
	ccfilter->FullyConnectedOn();

	relabel->SetInput( ccfilter->GetOutput() );
	relabel->SetMinimumObjectSize( minObjSize );
	relabel->InPlaceOn();

	try
    {
		relabel->Update();
    }
    catch( itk::ExceptionObject & excep )
    {
		std::cerr << "Relabel: exception caught !" << std::endl;
		std::cerr << excep << std::endl;
		return;
    }
	
	unsigned short initNumObjects = ccfilter->GetObjectCount();
	unsigned short finalNumObjects = relabel->GetNumberOfObjects();
	unsigned short removedNumObjects = initNumObjects - finalNumObjects;
	std::cout << "Removed " << removedNumObjects << " of " << initNumObjects << " objects...";

	ShortImageType::Pointer ccImage = relabel->GetOutput();

	//Use connected component image as a mask:
	itk::ImageRegionIterator< ShortImageType > itr1( ccImage, ccImage->GetLargestPossibleRegion() );
	itk::ImageRegionIterator< ImageType3D > itr2( myImg, myImg->GetLargestPossibleRegion() );
	for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2)
	{
		if(itr1.Get() == 0)
		{
			itr2.Set( 0 );
		}
	}
}
//This function removes all objects that are <= minObjSize from the foreground.
//The foreground remains grayscale after this filter
bool VolumeProcess::MaskSmallConnComp(int minObjSize)
{
	typedef itk::Image< unsigned short, Dimension > ShortImageType;
	typedef itk::ConnectedComponentImageFilter< ImageType, ShortImageType > CCFilterType;
	typedef itk::RelabelComponentImageFilter< ShortImageType, ShortImageType > RelabelType;

	CCFilterType::Pointer ccfilter = CCFilterType::New();
	RelabelType::Pointer relabel = RelabelType::New();
	
	ccfilter->SetInput (m_outputImage);
	ccfilter->FullyConnectedOn();

	relabel->SetInput( ccfilter->GetOutput() );
	relabel->SetMinimumObjectSize( minObjSize );
	relabel->InPlaceOn();

	try
    {
		relabel->Update();
    }
    catch( itk::ExceptionObject & excep )
    {
		std::cerr << "Relabel: exception caught !" << std::endl;
		std::cerr << excep << std::endl;
		return false;
    }
	
	unsigned short numObjects = relabel->GetNumberOfObjects();
	if(debug)
		std::cerr << "Connected components = " << numObjects << std::endl;

	ShortImageType::Pointer ccImage = relabel->GetOutput();

	//Use connected component image as a mask:
	itk::ImageRegionIterator< ShortImageType > itr1( ccImage, ccImage->GetLargestPossibleRegion() );
	itk::ImageRegionIterator< ImageType > itr2( m_outputImage, m_outputImage->GetLargestPossibleRegion() );
	for(itr1.GoToBegin(), itr2.GoToBegin() ; !itr1.IsAtEnd(); ++itr1, ++itr2)
	{
		if(itr1.Get() == 0)
		{
			itr2.Set( 0 );
		}
	}
	return true;
}
Exemplo n.º 3
0
void Histogram::delete_secondary_blobs()
{ 
	// This will remove all parts of the histogram that are not
	// 6-connected (in 3-d) with the blob containing the mode.

	std::cout << "  Removing secondary blobs...";
	
	//Find connected component image:
	typedef itk::ConnectedComponentImageFilter< HistImageType, HistImageType > CCFilterType;
	CCFilterType::Pointer ccfilter = CCFilterType::New();
	ccfilter->SetInput(histImage);
	try
	{
		ccfilter->Update();
	}
	catch( itk::ExceptionObject & excep )
    {
		std::cerr << "    Connected Components: exception caught !" << std::endl;
		std::cerr << excep << std::endl;
		return;
    }

	HistImageType::Pointer ccImage = ccfilter->GetOutput();

	//Get ID of the object containing the mode:
	HistImageType::IndexType index = { { mode[0], mode[1], mode[2] } };
	unsigned char id = ccImage->GetPixel(index);

	//Set the histogram count to 0 for all colors not in the main object:
	typedef itk::ImageRegionIterator< HistImageType > IteratorType;
	IteratorType histIterator( histImage, histImage->GetRequestedRegion() );
	IteratorType ccIterator( ccImage, ccImage->GetRequestedRegion() );
	for( histIterator.GoToBegin(), ccIterator.GoToBegin(); !histIterator.IsAtEnd(); ++histIterator, ++ccIterator )
	{
		if(ccIterator.Get() != id)
			histIterator.Set(0);
	}

	std::cout << "...Done" << std::endl;
}