[vtkusers] Display OpenCV Image (IplImage) in VTK

Anant Vemuri ajar108 at gmail.com
Tue Jun 1 00:19:36 EDT 2010


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
email: ajar108 at gmail.com



On Fri, May 7, 2010 at 5:12 PM, Anant Vemuri <ajar108 at gmail.com> wrote:

> 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.
>
>
> On Tue, May 4, 2010 at 3:02 PM, Samunda Perera <samunda_n at yahoo.com>wrote:
>
>> 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
>>
>> 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
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100601/3f655700/attachment.htm>


More information about the vtkusers mailing list