[osg-users] Convertion to osg::Image from IplImage (OpenCV) for useas texture

Poirier, Guillaume Guillaume.Poirier at nrc-cnrc.gc.ca
Mon Sep 17 06:50:06 PDT 2007


Hi John,

I wrote a osgImage2IplImage, not sure if it could help you, but here it is:

/*! Image conversion functions
    Utility functions to convert between different image representations */
IplImage* osgImage2IplImage(const osg::Image& p_osgImage)
{
  const unsigned char *buffer = p_osgImage.data();
  
  IplImage* pImg = cvCreateImage(cvSize(p_osgImage.s(), p_osgImage.t()), IPL_DEPTH_8U, p_osgImage.r() );
  
  if(p_osgImage.r() == 3)
  {
    struct pixelStruct { unsigned char r, g, b; };
    struct pixelStruct* pPixel = ( struct pixelStruct * ) ( buffer );
    struct pixelStruct* pCurrentPixel = NULL;
    for(int x = 0; x < p_osgImage.s(); x++ ) {
      for(int y = 0; y < p_osgImage.t(); y++ ) { 
        pCurrentPixel = &pPixel[ y * p_osgImage.s() + x ]; 
        int invertedY = p_osgImage.t()-1-y;
        // blue
        ((uchar*)(pImg->imageData + pImg->widthStep*invertedY))[x*3] = pCurrentPixel->b;
        // green
        ((uchar*)(pImg->imageData + pImg->widthStep*invertedY))[x*3+1] = pCurrentPixel->g;
        // red
        ((uchar*)(pImg->imageData + pImg->widthStep*invertedY))[x*3+2] = pCurrentPixel->r;
      }
    }
  }
  else if(p_osgImage.r() == 4)
  {
    struct pixelStruct { unsigned char r, g, b, a; };
    struct pixelStruct* pPixel = ( struct pixelStruct * ) ( buffer );
    struct pixelStruct* pCurrentPixel = NULL;
    for(int x = 0; x < p_osgImage.s(); x++ ) {
      for(int y = 0; y < p_osgImage.t(); y++ ) { 
        pCurrentPixel = &pPixel[ y * p_osgImage.s() + x ]; 
        int invertedY = p_osgImage.t()-1-y;
        // alpha
        ((uchar*)(pImg->imageData + pImg->widthStep*invertedY))[x*3] = pCurrentPixel->a;
        // blue
        ((uchar*)(pImg->imageData + pImg->widthStep*invertedY))[x*3+1] = pCurrentPixel->b;
        // green
        ((uchar*)(pImg->imageData + pImg->widthStep*invertedY))[x*3+2] = pCurrentPixel->g;
        // red
        ((uchar*)(pImg->imageData + pImg->widthStep*invertedY))[x*3+3] = pCurrentPixel->r;
      }
    } 
  }
  else
  {
    osg::notify(osg::NOTICE)<<"osgImage2IplImage: Error, unrecognized image depth."<<std::endl;
  }
  
  return pImg;
}



bill


-----Original Message-----
From: osg-users-bounces at lists.openscenegraph.org on behalf of John Steinbis
Sent: Sun 9/16/2007 9:23 PM
To: OpenSceneGraph Users
Subject: [osg-users] Convertion to osg::Image from IplImage (OpenCV) for useas texture
 
Hi,
I'm trying to convert an IplImage (OpenCV) type to osg::Image type for
use as a texture within OSG. My conversion routine attempts to utilize
setImage(...) of image class. It crashes and I havent been able to
track down the problem.

Does anyone have experience with a conversion like this or advise
about building osg images from other types? Thank you!

-- John
_______________________________________________
osg-users mailing list
osg-users at lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 3958 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20070917/598a7b7f/attachment-0003.bin>


More information about the osg-users mailing list