简体   繁体   中英

Writing image using ITK

I am new to ITK and I am trying to write the image I have just read. Image is written successfully but when I am trying to open the image, it is just a black image. However, the size is exactly the same as it should be (it is the same as input image size). I don't get what the problem is and why the image is black. Here is my code:

typedef unsigned char PixelType;
const unsigned int Dimension = 2;
typedef itk::Image< PixelType, Dimension > ImageType; // ImageType is an image with 2D and unsigned number of pixels
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageFileWriter< ImageType> WriterType;

typedef itk::ImageFileReader<ImageType> FileReaderType;


using namespace std;

ReaderType::Pointer LoadImage(string);
void WriteImage(string,ImageType::Pointer);


int main()
{
    string fileName = "test.tiff";
    string outFileName = "out.tiff";
    ReaderType::Pointer reader = LoadImage(fileName);
    ImageType::Pointer image = reader->GetOutput(); 


    WriteImage(outFileName, image);
    //cout<<reader<<endl;
    //cout<<image<<endl;

  return EXIT_SUCCESS;
}


ReaderType::Pointer LoadImage(string filename){
    itk::TIFFImageIO::Pointer tiffImageIO = itk::TIFFImageIO::New();
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName("test.tiff");
    reader->SetImageIO(tiffImageIO);
    try{
        reader->Update();
    }
    catch(itk::ExceptionObject & e){
        cerr<<e.GetDescription()<<endl;
    }
    return reader;
}


void WriteImage(string filename, ImageType::Pointer image){
  itk::TIFFImageIO::Pointer tiffImageIO = itk::TIFFImageIO::New();
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(filename);
  writer->SetImageIO(tiffImageIO);
  writer->SetInput(image);
  try{
      writer->Update(); 
      cout<<"Image has been written!"<<endl;
  }
  catch(itk::ExceptionObject & e){
      std::cerr << e.GetDescription() << std::endl;
  } 
}

What program are you using to view your image? Can you adjust the window, level or contrast?

As you are judging that your image is black visually, it's possibly that the dynamic range your image viewer is trying to display is different that what is in the image.

You are reading the image into an "unsigned integer". If your image was originally in the range 0-255, and you are writing it out with a pixel type which has a range form 0-4294967296, if you viewer is trying to show that range you will see a black image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM