[vtkusers] Saving result of volume rendering in a .pgm 2D image

Miro Drahos mdrahos at robodoc.com
Mon Nov 18 14:35:23 EST 2013


Hi Rane,
glad it was of help.
Firstly, please always write into the vtkusers maillist. You can cc me 
if I had responded before, but make sure the email gets public, so that 
people who might encounter a similar issue will be able to find the answer.

On to your question: the output of vtkWindowToImageFilter is 
vtkImageData, as you can tell from the name. So you can access the pixel 
values by means of GetScalarPointer() method of vtkImageData. You want 
to cast it (it is void*) to whatever type of scalars is the vtkImageData.
Here is a sample code snippet.

   vtkImageData * screen = w2i->GetOutput();
   cout << " scalar type of w2i output: " << 
screen->GetScalarTypeAsString() << endl;  // this is what I want to cast 
the pointer to
   cout << "  num of components: "  << 
screen->GetNumberOfScalarComponents () << endl; // == 4, i.e. RGBA

   cout << " extent: " << screen->GetExtent()[0] << " " << 
screen->GetExtent()[1] << " " << screen->GetExtent()[2] << " " << 
screen->GetExtent()[3] << " " << screen->GetExtent()[4] << " " << 
screen->GetExtent()[5] << endl;

   int i = 230, j= 230;
   unsigned char * ptr = static_cast<unsigned char 
*>(screen->GetScalarPointer(i, j, 0));
   cout << " value of pixel (" << i << ", " << j << "): R="<< (int)*ptr 
<< " G=" << (int)*(ptr+1) << " B=" << (int)*(ptr+2) << " "A=" << 
(int)*(ptr+3) << endl;

HTH,
Miro


On 11/18/2013 10:18 AM, R R wrote:
> Hi Miro,
>
> Thanks so much for your great information and sample code.It solved my 
> problem!
>
> I have another question. Would you please let me know is there any way 
> that instead of writing image (rendered 2D image) on disk, we can save 
> it to an array (m*m) and have access to each pixel of the image? What 
> I'm trying to say is that, I need to have access to pixels of the 
> rendered image. What I do now, is that I save it on disk and then read 
> it to a int array and work with the pixel values. I want to skip 
> writing on disk and directly have access to the pixels of the rendered 
> image.
>
> I really appreciate your help.
>
> Thanks,
> Rane
>
>
> On Friday, November 8, 2013 12:56 PM, Miro Drahos 
> <mdrahos at robodoc.com> wrote:
> I don't know what is .pgm format, but for any image you could just use 
> vtkWindowToImageFilter, pass your renderwindow to it as input, and 
> then use the writer with it, e.g.:
>
> void savePNG(const char * filename, vtkRenderWindow * rw)
> {
>   vtkWindowToImageFilter * w2i = vtkWindowToImageFilter::New();
>   w2i->SetInput(rw);   // rw is vtkRenderWindow that you are painting into
>   w2i->SetInputBufferTypeToRGBA();
>   w2i->Update();
>
>   vtkPNGWriter *writer = vtkPNGWriter::New();
> writer->SetInputConnection(w2i->GetOutputPort());
>   writer->SetFileName(filename);
>
>   rw->SetSize(800, 800);
>   rw->Render();
>   writer->Write();
>
>   w2i->Delete();
>   writer->Delete();
> }
>
> Cheers,
> Miro
>
>
> On 11/05/2013 09:31 PM, R R wrote:
>> Hello all,
>>
>> I have a question about how we can save the result of volume 
>> rendering in .pgm format. I mean I have a volume from which a 2D 
>> image is created using raycasting and is shown on the screen. I want 
>> to save this 2D image in a .pgm file. Could any body please give me 
>> some hint how I can do that.
>>
>> Thanks so much.
>>
>> Regards,
>> Rane
>
>
>

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


More information about the vtkusers mailing list