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

2007-09-17 Thread John Steinbis
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] Convertion to osg::Image from IplImage (OpenCV) for use as texture

2007-09-16 Thread John Steinbis
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


Re: [osg-users] Background image texture rescaling problem

2007-09-15 Thread John Steinbis
Thanks Robert,
I have this working now without rescaling the image. My solution was
to pad the image with zeros making it a power of two and then specify
the correct percentage of the image to use as 2D texture. Not sure if
this is the best solution but it works and is fast enough for me.

-- John

On 9/13/07, Robert Osfield [EMAIL PROTECTED] wrote:
 Hi John,

 The rescaling is done on non power of two textures by default,
 rescaling is slow op so best avoided.  You have two choices to disable
 the resize of the non power of two texture as you have down to move to
 using TextureRectangle instead of Texture2D.  If moving to the
 TextureRectangle you'll need to move you pixel coords rather than
 normal non dimensions texture coords - see osgtexturerectangle
 example.

 Robert.

 On 9/12/07, John Steinbis [EMAIL PROTECTED] wrote:
  For an AR project, I need to render a model with video image as
  background. My program is based around osgViewer using osg v2.0. I
  found an example for a HUD and with a few changes made it work as
  background. The code can be found below.
 
  The code runs ok but there is a problem occurring that I cannot figure
  out. Any ideas … ?
 
  When new images are loaded, they are rescaled for some reason. This
  adds a noticeable delay, ~250 ms, and distorts the images, which is
  not acceptable. Output looks like Scaling image 'x' from (720,480) to
  (512,512).
 
  To prevent resizing I use BGTexture-setResizeNonPowerOfTwoHint(false).
  To update an image I use BGTexture-setImage(newImg).
 
  Thanks,
  John
 
 
 
  root-addChild(BGProjectionMatrix);
 
  BGProjectionMatrix-setMatrix(osg::Matrix::ortho2D(0,width,0,height));
  BGProjectionMatrix-addChild(BGModelViewMatrix);
 
  BGModelViewMatrix-setMatrix(osg::Matrix::identity());
  BGModelViewMatrix-setReferenceFrame(osg::Transform::ABSOLUTE_RF);
  BGModelViewMatrix-addChild( BGGeode );
 
  BGTexture-setDataVariance(osg::Object::DYNAMIC);
  BGTexture-setResizeNonPowerOfTwoHint(false); // --
  BGTexture-setImage(BGImage);
 
  BGGeode-addDrawable(BGBackgroundGeometry);
  BGGeode-setStateSet(BGStateSet);
 
  BGBackgroundVertices-push_back( osg::Vec3( 0, 0, 0) );
  BGBackgroundVertices-push_back( osg::Vec3(width, 0, 0) );
  BGBackgroundVertices-push_back( osg::Vec3(width, height, 0) );
  BGBackgroundVertices-push_back( osg::Vec3( 0, height, 0) );
 
  (*texcoords)[0].set(0.0f,0.0f);
  (*texcoords)[1].set(1.0f,0.0f);
  (*texcoords)[2].set(1.0f,1.0f);
  (*texcoords)[3].set(0.0f,1.0f);
 
  BGcolors-push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
  BGnormals-push_back(osg::Vec3(1.0f,1.0f,1.0f));
 
  BGBackgroundGeometry-setTexCoordArray(0,texcoords);
  BGBackgroundGeometry-setColorArray(BGcolors);
  BGBackgroundGeometry-setColorBinding(osg::Geometry::BIND_OVERALL);
  BGBackgroundGeometry-setNormalArray(BGnormals);
  BGBackgroundGeometry-setNormalBinding(osg::Geometry::BIND_OVERALL);
  BGBackgroundGeometry-addPrimitiveSet(BGBackgroundIndices);
  BGBackgroundGeometry-setVertexArray(BGBackgroundVertices);
 
  BGBackgroundIndices-push_back(0);
  BGBackgroundIndices-push_back(1);
  BGBackgroundIndices-push_back(2);
  BGBackgroundIndices-push_back(3);
 
  BGStateSet-setTextureAttributeAndModes(0,BGTexture,osg::StateAttribute::ON);
  BGStateSet-setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
  BGStateSet-setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
  BGStateSet-setRenderBinDetails( 1, RenderBin);
  ___
  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


Re: [osg-users] Background image texture rescaling problem

2007-09-15 Thread John Steinbis
I tryed disabling the resize of non power of two textures by using
setResizeNonPowerOfTwoHint but it did not seem to have any effect.
Do you think there is a bug or I just wasnt doing something right? You
can find the line where I tried this in my original post.

I havent been able to figure out how to set the position of a window.
How can I do this? Also, if I try to drag the window to a new
location, it freezes.

-- js


On 9/15/07, Robert Osfield [EMAIL PROTECTED] wrote:
 Hi John,

 On 9/15/07, John Steinbis [EMAIL PROTECTED] wrote:
  Thanks Robert,
  I have this working now without rescaling the image. My solution was
  to pad the image with zeros making it a power of two and then specify
  the correct percentage of the image to use as 2D texture. Not sure if
  this is the best solution but it works and is fast enough for me.

 This is not an elegant way to tackle the problem, workable yes, but
 I'd recommend spending a little time - readying up about
 TextureRectangle or just disabling the resize of non power of two
 textures, you'll end up with a more efficient and elegant solution.

 Robert.
 ___
 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] Background image texture rescaling problem

2007-09-12 Thread John Steinbis
For an AR project, I need to render a model with video image as
background. My program is based around osgViewer using osg v2.0. I
found an example for a HUD and with a few changes made it work as
background. The code can be found below.

The code runs ok but there is a problem occurring that I cannot figure
out. Any ideas … ?

When new images are loaded, they are rescaled for some reason. This
adds a noticeable delay, ~250 ms, and distorts the images, which is
not acceptable. Output looks like Scaling image 'x' from (720,480) to
(512,512).

To prevent resizing I use BGTexture-setResizeNonPowerOfTwoHint(false).
To update an image I use BGTexture-setImage(newImg).

Thanks,
John



root-addChild(BGProjectionMatrix);

BGProjectionMatrix-setMatrix(osg::Matrix::ortho2D(0,width,0,height));
BGProjectionMatrix-addChild(BGModelViewMatrix);

BGModelViewMatrix-setMatrix(osg::Matrix::identity());
BGModelViewMatrix-setReferenceFrame(osg::Transform::ABSOLUTE_RF);
BGModelViewMatrix-addChild( BGGeode );

BGTexture-setDataVariance(osg::Object::DYNAMIC);
BGTexture-setResizeNonPowerOfTwoHint(false); // --
BGTexture-setImage(BGImage);

BGGeode-addDrawable(BGBackgroundGeometry);
BGGeode-setStateSet(BGStateSet);

BGBackgroundVertices-push_back( osg::Vec3( 0, 0, 0) );
BGBackgroundVertices-push_back( osg::Vec3(width, 0, 0) );
BGBackgroundVertices-push_back( osg::Vec3(width, height, 0) );
BGBackgroundVertices-push_back( osg::Vec3( 0, height, 0) );

(*texcoords)[0].set(0.0f,0.0f);
(*texcoords)[1].set(1.0f,0.0f);
(*texcoords)[2].set(1.0f,1.0f);
(*texcoords)[3].set(0.0f,1.0f);

BGcolors-push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
BGnormals-push_back(osg::Vec3(1.0f,1.0f,1.0f));

BGBackgroundGeometry-setTexCoordArray(0,texcoords);
BGBackgroundGeometry-setColorArray(BGcolors);
BGBackgroundGeometry-setColorBinding(osg::Geometry::BIND_OVERALL);
BGBackgroundGeometry-setNormalArray(BGnormals);
BGBackgroundGeometry-setNormalBinding(osg::Geometry::BIND_OVERALL);
BGBackgroundGeometry-addPrimitiveSet(BGBackgroundIndices);
BGBackgroundGeometry-setVertexArray(BGBackgroundVertices);

BGBackgroundIndices-push_back(0);
BGBackgroundIndices-push_back(1);
BGBackgroundIndices-push_back(2);
BGBackgroundIndices-push_back(3);

BGStateSet-setTextureAttributeAndModes(0,BGTexture,osg::StateAttribute::ON);
BGStateSet-setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF);
BGStateSet-setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
BGStateSet-setRenderBinDetails( 1, RenderBin);
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org