Discussion:
[vtkusers] Display OpenCV Image (IplImage) in VTK
Samunda Perera
2010-05-04 07:02:31 UTC
Permalink
Dear all,

I have a pointer to a image stored in memory (example void* frame or similar data type obtained from opencv) .
How to display this 2D image data as a image in VTK window?

Thanks & Regards,
Samu
Anant Vemuri
2010-05-07 09:12:11 UTC
Permalink
I have also been trying to interface both Iplimage and vtkimagedata. Here is
what I did

void fromIpl2Vtk( IplImage* _src, vtkImageData* _dest )
{
vtkImageImport *importer = vtkImageImport::New();
if ( _dest )
{
importer->SetOutput( _dest );
}
importer->SetDataSpacing( 1, 1, 1 );
importer->SetDataOrigin( 0, 0, 0 );
importer->SetWholeExtent( 0, _src->width-1, 0, _src->height-1, 0,
_src->nChannels-1 );
importer->SetDataExtentToWholeExtent();
importer->SetDataScalarTypeToUnsignedChar();
importer->SetNumberOfScalarComponents( _src->nChannels );
importer->SetImportVoidPointer( _src->imageData );
importer->Update();
}

This works, but somehow vtk only recognizes the first channel that is blue,
which is the first channel of IplImage (BGR...BGR...) ... I was wondering if
anyone had any suggestions to fix this.

thanks.
Post by Samunda Perera
Dear all,
I have a pointer to a image stored in memory (example void* frame or
similar data type obtained from opencv) .
How to display this 2D image data as a image in VTK window?
Thanks & Regards,
Samu
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.vtk.org/Wiki/VTK_FAQ
http://www.vtk.org/mailman/listinfo/vtkusers
Anant Vemuri
2010-06-01 04:19:36 UTC
Permalink
Okay, I finally have the solution to this. It was just on thing that needed
changing. Since OpenCV creates BGR image, I needed to convert the BGR to RGB
using cv::cvtColor(imagebgr, imageRGB, CV_BGR2RGB) and then give it to
fromIpl2Vtk(...) function below. Just one other thing. Since VTK and OpenCV
use different origins for the image, I first did
cv::flip(origImage,flipImage, -1). I am sure some of you may have figured it
out... but I just wanted to document it.

bool fromIpl2Vtk( cv::Mat _src, vtkImageData* _dest )
{
assert( _src.data != NULL );
vtkImageImport *importer = vtkImageImport::New();
if ( _dest )
{
importer->SetOutput( _dest );
}
importer->SetDataSpacing( 1, 1, 1 );
importer->SetDataOrigin( 0, 0, 0 );
importer->SetWholeExtent( 0, _src.size().width-1, 0,
_src.size().height-1, 0, 0 );
importer->SetDataExtentToWholeExtent();
importer->SetDataScalarTypeToUnsignedChar();
importer->SetNumberOfScalarComponents( _src.channels() );
importer->SetImportVoidPointer( _src.data );
importer->Update();
return true;
}

hope it works...

Best regards,
Anant.

-----------
Anant S. Vemuri
Post by Anant Vemuri
I have also been trying to interface both Iplimage and vtkimagedata. Here
is what I did
void fromIpl2Vtk( IplImage* _src, vtkImageData* _dest )
{
vtkImageImport *importer = vtkImageImport::New();
if ( _dest )
{
importer->SetOutput( _dest );
}
importer->SetDataSpacing( 1, 1, 1 );
importer->SetDataOrigin( 0, 0, 0 );
importer->SetWholeExtent( 0, _src->width-1, 0, _src->height-1, 0,
_src->nChannels-1 );
importer->SetDataExtentToWholeExtent();
importer->SetDataScalarTypeToUnsignedChar();
importer->SetNumberOfScalarComponents( _src->nChannels );
importer->SetImportVoidPointer( _src->imageData );
importer->Update();
}
This works, but somehow vtk only recognizes the first channel that is
blue, which is the first channel of IplImage (BGR...BGR...) ... I was
wondering if anyone had any suggestions to fix this.
thanks.
Post by Samunda Perera
Dear all,
I have a pointer to a image stored in memory (example void* frame or
similar data type obtained from opencv) .
How to display this 2D image data as a image in VTK window?
Thanks & Regards,
Samu
_______________________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
http://www.vtk.org/Wiki/VTK_FAQ
http://www.vtk.org/mailman/listinfo/vtkusers
Samunda Perera
2010-05-09 09:07:41 UTC
Permalink
Hi Anant,

Thanks for the code, yes seems like a bug.

Regards,
samu




________________________________
From: Samunda Perera <***@yahoo.com>
To: ***@vtk.org
Sent: Tue, May 4, 2010 12:32:31 PM
Subject: Display OpenCV Image (IplImage) in VTK


Dear all,

I have a pointer to a image stored in memory (example void* frame or similar data type obtained from opencv) .
How to display this 2D image data as a image in VTK window?

Thanks & Regards,
Samu
Samunda Perera
2010-05-09 09:30:22 UTC
Permalink
Hi Anant,

I think i just found the problem, (not a bug).

Since opencv defaults to BGR memory style vtk reads these data as R<--B, G<--G, B<--R.

so displayed image does not appear as the real one.

however when input image is converted to RGB style (ex: cvCvtColor(img,img,CV_RGB2BGR); ) looks ok!.

Regards,
samu




________________________________
From: Samunda Perera <***@yahoo.com>
To: ***@vtk.org
Sent: Sun, May 9, 2010 2:37:41 PM
Subject: Re: Display OpenCV Image (IplImage) in VTK


Hi Anant,

Thanks for the code, yes seems like a bug.

Regards,
samu




________________________________
From: Samunda Perera <***@yahoo.com>
To: ***@vtk.org
Sent: Tue, May 4, 2010 12:32:31 PM
Subject: Display OpenCV Image (IplImage) in VTK


Dear all,

I have a pointer to a image stored in memory (example void* frame or similar data type obtained from opencv) .
How to display this 2D image data as a image in VTK window?

Thanks & Regards,
Samu
Loading...