Problem with the stochastic fractal filter

Hello, everyone! I’m trying to make use of the Stochastic Fractal Dimension Image Filter, but I’ve been unable to get any results in 2D or 3D images. All the outputs are blank (actually, black).

Could anyone please point out where I’m getting the filter usage wrong?

Attached is the image I’m using as input and a .zip of the code itself with a CMakeLists file ready to run it.

Thanks in advance!

stochastic_fractal_code.zip (151.7 KB)

#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkStochasticFractalDimensionImageFilter.h>


void showUsage(std::string str)
{
	std::cout << "Usage:\n"
	<< "\t" << str << "\n"
	<< "\tinputImage\n"
	<< "\toutputImage\n"
	<< "\t[radius, default = 2]\n";
}



int main(int argc, char **argv)
{
	if(argc < 3)
	{
		showUsage(argv[0]);
		return -1;
	}

	// Get inputs from command line
	std::string inputFileName, outputFileName;
	inputFileName = argv[1];
	outputFileName = argv[2];
	int radius = argc > 3 ? atoi(argv[3]) : 2;

	// Dimension and pixel type
	const int Dimension = 2;
	typedef unsigned char PixelType;

	// Image and reader/writer definitions
	typedef itk::Image< PixelType, Dimension > ImageType;
	typedef itk::ImageFileReader< ImageType > ReaderType;
	typedef itk::ImageFileWriter< ImageType > WriterType;

	// Read input
	ReaderType::Pointer reader = ReaderType::New();
	reader->SetFileName(inputFileName);
	reader->Update();

	// Filter definition
	typedef itk::StochasticFractalDimensionImageFilter< ImageType > StochasticFractalDimensionImageFilterType;
	StochasticFractalDimensionImageFilterType::Pointer stochastic_filter = StochasticFractalDimensionImageFilterType::New();

	// Set filter input
	stochastic_filter->SetInput(reader->GetOutput());

	// Set radius
	ImageType::SizeType r;
	r.Fill(radius);
	stochastic_filter->SetNeighborhoodRadius(r);
	
	// Do it
	stochastic_filter->Update();

	// Write output to file
	WriterType::Pointer writer = WriterType::New();
	writer->SetFileName(outputFileName);
	writer->SetInput(stochastic_filter->GetOutput());
	writer->Update();

	return 0;
}

Have you tried using float for the output pixel type? ( Just a guess )

Thanks for your reply! Yes, when dealing with 3D images I use float as the output pixel type. However, since my 2D images are in .png format, I must use either unsigned char or unsigned int.

I am just writing up a wrapper for this class for SimpleITK. The results with integer pixel type for output were very poor, mostly 0,1,2 for the “cthead1.png” test image. The output of this filter should be a real type. If you have a png image of {0,1,2} and it’s loaded into a standard view you will see just a black image. You should be using a view that can support window and leveling, and file format that write float/double types. ImageJ comes to mind for basic 2D images, while 3D Slicer or medical images.

If you must write as uchar , you can use a filter to change the intensity of the image such as RescaleIntensityImageFilter.

1 Like

Of course, I should be using the rescale intensity filter! I just plugged it into my pipeline and now everything works fine. Thank you for your support, Bradley!

1 Like

@pfreire There is no need to change the original post’s title to include the word [Solved]. There is an option for that in discourse and we have intentionally not enabled it. It deters from further discussion.

Sure thing @dzenanz. Thank you for letting me know :slight_smile:

Getting back to this topic in order to understand how the mask usage works in this particular filter. It has the option to provide a mask to constrain the ROI and thus decrease computation time. However, I’ve given a binary mask (unsigned char) as input but the output still encompasses the whole image. No decrease in computation time is observed. I’ve used binary masks with 0 as background and 1 as foreground and also 0 as background and 255 (unsigned char max) as foreground, but to no avail. Could someone please tell me what to do in this case? Thank you very much!