[Insight-users] Reading and writing PNG

Brecht Heyde Brecht.Heyde at med.kuleuven.be
Mon Jan 10 17:37:35 EST 2011


Hi everyone,

I'm trying to read the following png file: ITK-src/Examples/Data/BrainProtonDensitySlice.png and apply a simple BinaryTresholdFilter.
However, the output of this filter (BrainProtonDensitySliceFiltered.png) seems to be black. 
I call the executable like this: 
C:\ITK-code\BinaryTresholdImageFilter\bin\exe\debug\BinaryThresholdImageFilterExe ..\..\..\src\BrainProtonDensitySlice.png BrainProtonDensitySliceFiltered.png 2 200 2 50

Writing the image without applying the filter (see comments in code) also results in a black image.

Does anyone have an idea what I'm doing wrong?

Thanks in advance,
Brecht Heyde

This is my code:

***************CMakeList.txt**************

# Template CMakeLists.txt to include ITK and elastix libraries

#---------------------------------------------------------------------------
# Parameter list

SET( PROJECT_NAME BinaryThresholdImageFilterProject )
SET( EXECUTABLE_NAME BinaryThresholdImageFilterExe )
SET( SOURCE_LIST 
  BinaryThresholdImageFilter.cxx ) #Source list from which to built an executable
SET( LIB_LIST
  ITKCommon
  ITKIO) # library list to link to the executable

#---------------------------------------------------------------------------

PROJECT( ${PROJECT_NAME} )

CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

# Find ITK
FIND_PACKAGE( ITK REQUIRED )
IF( ITK_FOUND )
  INCLUDE( ${ITK_USE_FILE} )
ENDIF( ITK_FOUND )
MESSAGE( STATUS "ITK binary directory: ${ITK_DIR}")

# Build executable
IF( NOT EXECUTABLE_OUTPUT_PATH )
  SET( EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/exe CACHE PATH "Output directory for building all executables." )
ENDIF()

ADD_EXECUTABLE( ${EXECUTABLE_NAME} ${SOURCE_LIST})

# Link to some libraries
TARGET_LINK_LIBRARIES( ${EXECUTABLE_NAME} ${LIB_LIST})

***************BinaryThresholdImageFilter.cpp**************

#include "itkBinaryThresholdImageFilter.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

int main(int argc, char* argv[]){
	if(argc<7){
		std::cerr << "Usage: " << argv[0];
		std::cerr << " inputImageFile outputImageFile ";  
		std::cerr << " lowerThreshold upperThreshold ";  
		std::cerr << " outsideValue insideValue   "  << std::endl;  
		return EXIT_FAILURE;
	}
	
	typedef unsigned short InputPixelType;
	typedef unsigned short OutputPixelType; 
	
	typedef itk::Image< InputPixelType, 2 > InputImageType;
	typedef itk::Image< OutputPixelType, 2 > OutputImageType;

	typedef itk::BinaryThresholdImageFilter< InputImageType, OutputImageType > FilterType;

	typedef itk::ImageFileReader< InputImageType > ReaderType;
	typedef itk::ImageFileWriter< OutputImageType > WriterType;

	ReaderType::Pointer reader = ReaderType::New();
	FilterType::Pointer filter = FilterType::New();
	WriterType::Pointer writer = WriterType::New();

	//filter settings
	const InputPixelType lowTHold = atoi(argv[3]); //string to integer
	const InputPixelType upTHold = atoi(argv[4]);
	filter->SetLowerThreshold(lowTHold);
	filter->SetUpperThreshold(upTHold);

	const OutputPixelType inVal = atoi(argv[5]);
	const OutputPixelType outVal = atoi(argv[6]);
	filter->SetInsideValue(inVal);
	filter->SetOutsideValue(outVal);

	//pipeline
	try{
		reader->SetFileName(argv[1]);
		reader->Update();
	}
	catch(itk::ExceptionObject & excp){
		std::cerr << excp << std::endl;
		return 1;
     }

	filter->SetInput(reader->GetOutput());
	writer->SetInput(filter->GetOutput()); //Write the filtered image
	//writer->SetInput(reader->GetOutput()); //Write the input image
	writer->SetFileName(argv[2]);
	try{
		writer->Update();
	}
	catch ( itk::ExceptionObject & excp ){
		std::cerr << excp << std::endl;
		return 1;
	}

	return EXIT_SUCCESS;
}













More information about the Insight-users mailing list