Thank you everyone for replies to my problem...
Bill's code was instructive by showing that the basic idea behind what
I was trying to do was correct. My old method (and Bill's) would
attempt to perform a deep copy. In this method, I'd simply forgotten
to initialize something before using it. Sorry! My new method performs
a shallow copy and is working now, with very little code required.
I've posted it below.

-- John


osg::Image* Convert_OpenCV_TO_OSG_IMAGE(IplImage* cvImg)
{

        if(cvImg->nChannels == 3)
        {
                // Flip image from top-left to bottom-left origin
                if(cvImg->origin == 0) {
                        cvConvertImage(cvImg , cvImg, CV_CVTIMG_FLIP);
                        cvImg->origin = 1;
                }

                // Convert from BGR to RGB color format
                //printf("Color format %s\n",cvImg->colorModel);
                cvCvtColor( cvImg, cvImg, CV_BGR2RGB );

                osg::Image* osgImg = new osg::Image();

                osgImg->setImage(
                        cvImg->width, //s
                        cvImg->height, //t
                        3, //r
                        3, //GLint internalTextureformat, (GL_LINE_STRIP, 
0x0003)
                        6407, // GLenum pixelFormat, (GL_RGB, 0x1907)
                        5121, // GLenum type, (GL_UNSIGNED_BYTE, 0x1401)
                        (BYTE*)(cvImg->imageData), // unsigned char* data
                        osg::Image::AllocationMode::NO_DELETE // AllocationMode 
mode (shallow copy)
                        );//int packing=1); (???)

                //printf("Conversion completed\n");
                return osgImg;
        }
        else {
                printf("Unrecognized image type");
                return 0;
        }

}




On 9/17/07, Poirier, Guillaume <[EMAIL PROTECTED]> wrote:
>
> 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: [EMAIL PROTECTED] 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@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
>
>
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to