[vtkusers] VTKPNGWriter printing out black images?

Rafay Khan rafayk_ at hotmail.com
Wed Mar 27 16:33:03 EDT 2013


Hi Bill,
I have tried viewing them with gimp Image editor and the default Image Viewer on Ubuntu 11.10. 
However, vtkPNG writer is able to print binary images in another part of my code .
As shown below the following code does produce a binary mask that I can view using my default viewers.  So I don't think the viewers are the problem. 
In the code below, whiteImage is a just a vtkImage data with all scalar values set to 255.


 // cut corresponding white image
    vtkSmartPointer<vtkImageStencil> stencil = vtkSmartPointer<vtkImageStencil>::New();
    stencil->SetInput(whiteImage);
    stencil->SetStencil(pol2stenc->GetOutput());
    stencil->ReverseStencilOff();
    stencil->SetBackgroundValue(0);
    stencil->Update();
    
    //! For debugging purposes to check output of create a mask algorithm
    vtkSmartPointer<vtkImageCast> castFilter =
        vtkSmartPointer<vtkImageCast>::New();
      castFilter->SetOutputScalarTypeToUnsignedChar ();
      castFilter->SetInputConnection(stencil->GetOutputPort());
      castFilter->Update();
    
    QString filename;
    filename = "StencilMask_" + QString("%1").arg(contourDataObject->getId());
    //filename = filename.arg(m_sliceIndex + 1);
    vtkSmartPointer<vtkPNGWriter> writer =
        vtkSmartPointer<vtkPNGWriter>::New();
      writer->SetFileName(filename.toStdString().c_str());
      writer->SetInputConnection(castFilter->GetOutputPort());
      writer->Write();
Date: Wed, 27 Mar 2013 13:20:43 -0700
Subject: Re: [vtkusers] VTKPNGWriter printing out black images?
From: bill.lorensen at gmail.com
To: rafayk_ at hotmail.com
CC: vtkusers at vtk.org

How are you viewing the png image? Many png viewers cannot view 16 bit images.

On Wed, Mar 27, 2013 at 1:02 PM, Rafay Khan <rafayk_ at hotmail.com> wrote:





I am doing some image processing using ITK and then using VTK to print the results in a .png format however, the output image is always black.
Currently, I am converting itk::Image to vtk::vtkImageData using the itk::ImagetoVTKImageFilter(typedeffed to ITKtoVTKFilterType in my code).ITKtoVTKFilterType::Pointer itk2vtkGray = ITKtoVTKFilterType::New();
itk2vtkGray->SetInput(grayBinary);  //grayBinary is of type itk::Image<unsigned short, 2>
itk2vtkGray->Update();

vtkSmartPointer<vtkImageData> grayVTK = vtkSmartPointer<vtkImageData>::New();
grayVTK->SetExtent(extent);
grayVTK->SetSpacing(m_spacing);
grayVTK->SetScalarTypeToUnsignedShort();
grayVTK->SetNumberOfScalarComponents(1);
grayVTK->AllocateScalars();
grayVTK->DeepCopy(static_cast<vtkImageData*>(itk2vtkGray->GetOutput()));
//grayVTK = itk2vtkGray->GetOutput();
I have even confirmed that my VTK ImageData contains values of either 255 or 0 using the following code.int *dims = grayVTK->GetDimensions();

std::cout << "Dims: " << " x: " << dims[0] << " y: " << dims[1] << " z: " << dims[2] << std::endl;
std::cout << "Number of points: " << grayVTK->GetNumberOfPoints() << std::endl;
std::cout << "Number of cells: " << grayVTK->GetNumberOfCells() << std::endl;

for (int y = 0; y < dims[1]; y++)
{        
     for (int x = 0; x < dims[0]; x++)
     {
         unsigned short *pixel = static_cast<unsigned short*>(grayVTK->GetScalarPointer(x,y,0));
         std::cout << "PIXEL LOC/VAL "<< y*dims[0] + x << " " << pixel[0] <<std::endl;
     }
     std::cout << std::endl;
 }
I then go on to do an ImageCast to ensure the type of the data is unsignedShort.vtkSmartPointer<vtkImageCast> cast2 = vtkSmartPointer<vtkImageCast>::New();
cast2->SetInput(grayVTK);
cast2->SetOutputScalarTypeToUnsignedShort();
cast2->ClampOverflowOn();
cast2->Update();
Then finally I use vtkPNGwriter to output the .png files. Notice that I have tried to output both the actual vtkImageData as well as output from the ImageCastFilter.
vtkSmartPointer<vtkPNGWriter> writer =
vtkSmartPointer<vtkPNGWriter>::New();
writer->SetFileName(filename.toStdString().c_str());
writer->SetInputConnection(cast2->GetOutputPort());
//writer->SetInput(grayVTK); I have tried to method as well but to no success
writer->Write();
However, the .png output is always black. Does anyone know what I am doing wrong.
 		 	   		  

_______________________________________________

Powered by www.kitware.com



Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html



Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ



Follow this link to subscribe/unsubscribe:

http://www.vtk.org/mailman/listinfo/vtkusers




-- 
Unpaid intern in BillsBasement at noware dot com

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130327/bdcd87e1/attachment.htm>


More information about the vtkusers mailing list