[vtkusers] VTKPNGWriter printing out black images?

Bill Lorensen bill.lorensen at gmail.com
Wed Mar 27 16:20:43 EDT 2013


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/f09b9f19/attachment.htm>


More information about the vtkusers mailing list