C# Can';在EMGU CV中绘制SharpGL(openGL包装器)对象
我一直在使用emgu cv(opencv包装器)从网络摄像头捕获图像,并使用其功能处理这些图像。 我还检测手并跟踪手的运动 现在,我需要根据手的位置画一种地球或一个物体,sharpGL非常适合透视变换等等。我的问题是我无法做到这一点 我不知道怎么对sharpGL说“你们这些家伙,在这个手跟踪窗口内画那个物体” 我想做的是不可能的吗?我很绝望…任何帮助都会很好。提前谢谢 如果你还不明白我的意思,请看这段视频(http://www.youtube.com/watch?v=ccL4t36sVvg) 到目前为止,我刚刚将这段代码翻译成C# 下面是代码片段C# Can';在EMGU CV中绘制SharpGL(openGL包装器)对象,c#,opengl,emgucv,C#,Opengl,Emgucv,我一直在使用emgu cv(opencv包装器)从网络摄像头捕获图像,并使用其功能处理这些图像。 我还检测手并跟踪手的运动 现在,我需要根据手的位置画一种地球或一个物体,sharpGL非常适合透视变换等等。我的问题是我无法做到这一点 我不知道怎么对sharpGL说“你们这些家伙,在这个手跟踪窗口内画那个物体” 我想做的是不可能的吗?我很绝望…任何帮助都会很好。提前谢谢 如果你还不明白我的意思,请看这段视频(http://www.youtube.com/watch?v=ccL4t36sVvg) 到
private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
{
// TODO: Initialise OpenGL here.
// The texture identifier.
uint[] textures = new uint[1];
// Get the OpenGL object.
OpenGL gl = openGLControl1.OpenGL;
//texture.Create(gl);
// Get one texture id, and stick it into the textures array.
gl.GenTextures(1, textures);
// Bind the texture.
gl.BindTexture(OpenGL.GL_TEXTURE_2D, textures[0]);
// A bit of extra initialisation here, we have to enable textures.
gl.Enable(OpenGL.GL_TEXTURE_2D);
// Specify linear filtering.
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_NEAREST);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_NEAREST);
gl.PixelStore(OpenGL.GL_UNPACK_ALIGNMENT, 1);
// Set the clear color.
gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
private void openGLControl_Resized(object sender, EventArgs e)
{
// TODO: Set the projection matrix here.
// Get the OpenGL object.
OpenGL gl = openGLControl1.OpenGL;
// Set the projection matrix.
gl.MatrixMode(OpenGL.GL_PROJECTION);
// Load the identity.
gl.LoadIdentity();
// Create a perspective transformation.
gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 100.0);
// Use the 'look at' helper function to position and aim the camera.
gl.LookAt(-5, 5, -5, 0, 0, 0, 0, 1, 0);
// Set the modelview matrix.
gl.MatrixMode(OpenGL.GL_MODELVIEW);
}
最后画一个三维物体
private void openGLControl_OpenGLDraw(object sender, PaintEventArgs e)
{
// Get the OpenGL object.
OpenGL gl = openGLControl1.OpenGL;
if (capture == null)
{
this.start_capture();
}
if (capture != null)
{
Image<Bgr, Byte> ImageFrame = capture.QueryFrame();
//I'm trying to use some algorithm using the code from sample (sharpGLTextureExample)
//first, I make an Bitmap object that I take from queryframe(convert it to bitmap first)
Bitmap image = new Bitmap(ImageFrame.ToBitmap());
// Clear the color and depth buffer.
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
//ImageFrame.Draw(new Rectangle(2, 2, 2, 2), new Bgr(Color.Aqua), 2);
// Load the identity matrix.
gl.LoadIdentity();
//then, Lock the image bits (so that we can pass them to OGL).
BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, textures[0]);
//gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, ImageFrame.Width, ImageFrame.Height, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, ImageFrame);
gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, (int)OpenGL.GL_RGBA, ImageFrame.Width, ImageFrame.Height, 0, OpenGL.GL_RGBA, OpenGL.GL_UNSIGNED_BYTE, bitmapData.Scan0);
//gl.Begin(OpenGL.GL_QUADS);
//gl.TexCoord(0, 0); gl.Vertex(-1, -1, 0);
//gl.TexCoord(1, 0); gl.Vertex(1, -1, 0);
//gl.TexCoord(1, 5); gl.Vertex(1, 1, 0);
//gl.TexCoord(0, 1); gl.Vertex(-1, 1, 0);
//gl.End();
//gl.Flush();
//texture.Bind(gl);
//
//CamImageBox.Image = ImageFrame;
}
}
private void openGLControl\u OpenGLDraw(对象发送方,PaintEventArgs e)
{
//获取OpenGL对象。
OpenGL=openGLControl1.OpenGL;
如果(捕获==null)
{
这个。开始捕获();
}
如果(捕获!=null)
{
Image ImageFrame=capture.QueryFrame();
//我正在尝试使用示例代码(sharpGLTextureExample)中的一些算法
//首先,我从queryframe获取一个位图对象(首先将其转换为位图)
位图图像=新位图(ImageFrame.ToBitmap());
//清除颜色和深度缓冲区。
gl.Clear(OpenGL.gl_COLOR_BUFFER_BIT | OpenGL.gl_DEPTH_BUFFER_BIT);
//ImageFrame.Draw(新的矩形(2,2,2,2),新的Bgr(Color.Aqua),2);
//加载标识矩阵。
gl.LoadIdentity();
//然后,锁定图像位(以便我们可以将它们传递给OGL)。
BitmapData BitmapData=image.LockBits(新矩形(0,0,image.Width,image.Height),
ImageLockMode.ReadOnly,PixelFormat.Format32bppArgb);
BindTexture(OpenGL.gl_纹理_2D,纹理[0]);
//gl.TexImage2D(OpenGL.gl_纹理_2D,0,(int)OpenGL.gl_RGBA,ImageFrame.Width,ImageFrame.Height,0,OpenGL.gl_RGBA,OpenGL.gl_无符号字节,ImageFrame);
TexImage2D(OpenGL.gl_纹理_2D,0,(int)OpenGL.gl_RGBA,ImageFrame.Width,ImageFrame.Height,0,OpenGL.gl_RGBA,OpenGL.gl_无符号_字节,位图数据.Scan0);
//开始(OpenGL.gl\u QUADS);
//gl.TexCoord(0,0);gl.Vertex(-1,-1,0);
//gl.TexCoord(1,0);gl.Vertex(1,-1,0);
//高斯坐标(1,5);高斯顶点(1,1,0);
//gl.TexCoord(0,1);gl.Vertex(-1,1,0);
//gl.End();
//gl.Flush();
//纹理绑定(gl);
//
//CamImageBox.Image=ImageFrame;
}
}
但是输出总是返回白色,没有纹理
我也考虑过使用纹理类,但没有用..因为没有输入参数为帧的方法…从您的代码和帮助中,我得到了SharpGL显示来自EmguCV的视频
public partial class FormSharpGLTexturesSample : Form
{
Capture capture;
public FormSharpGLTexturesSample()
{
InitializeComponent();
// Get the OpenGL object, for quick access.
SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
// A bit of extra initialisation here, we have to enable textures.
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.Disable(OpenGL.GL_DEPTH_TEST);
// Create our texture object from a file. This creates the texture for OpenGL.
capture = new Capture(@"Video file here");
}
private void openGLControl1_OpenGLDraw(object sender, RenderEventArgs e)
{
// Get the OpenGL object, for quick access.
SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
int Width = openGLControl1.Width;
int Height = openGLControl1.Height;
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
gl.LoadIdentity();
var frame = capture.QueryFrame();
texture.Destroy(gl);
texture.Create(gl, frame.Bitmap);
// Bind the texture.
texture.Bind(gl);
gl.Begin(OpenGL.GL_QUADS);
gl.TexCoord(0.0f, 0.0f); gl.Vertex(0, 0, 0);
gl.TexCoord(1.0f, 0.0f); gl.Vertex(Width, 0, 0);
gl.TexCoord(1.0f, 1.0f); gl.Vertex(Width, Height, 0);
gl.TexCoord(0.0f, 1.0f); gl.Vertex(0, Height, 0);
gl.End();
gl.Flush();
}
// The texture identifier.
Texture texture = new Texture();
private void openGLControl1_Resized(object sender, EventArgs e)
{
SharpGL.OpenGL gl = this.openGLControl1.OpenGL;
// Create an orthographic projection.
gl.MatrixMode(MatrixMode.Projection);
gl.LoadIdentity();
// NOTE: Basically no matter what I do, the only points I see are those at
// the "near" surface (with z = -zNear)--in this case, I only see green points
gl.Ortho(0, openGLControl1.Width, openGLControl1.Height, 0, 0, 1);
// Back to the modelview.
gl.MatrixMode(MatrixMode.Modelview);
}
}
我希望有帮助
经过一些实验后,我可以使用TexImage2D,但只能使用宽度和高度为二次方的图像
而不是:
var frame = capture.QueryFrame();
texture.Destroy(gl);
texture.Create(gl, frame.Bitmap);
它可以替换为下面的块来更新图片的数据。我想知道如何消除调用Resize的需要
var frame = capture.QueryFrame();
frame = frame.Resize(256, 256, Emgu.CV.CvEnum.INTER.CV_INTER_NN);
Bitmap Bitmap = frame.Bitmap;
BitmapData bitmapData = Bitmap.LockBits(new Rectangle(0, 0, Bitmap.Width, Bitmap.Height),
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, textures[0]);
gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, OpenGL.GL_RGBA, Bitmap.Width, Bitmap.Height, 0, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE, bitmapData.Scan0);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); // Required for TexImage2D
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); // Required for TexImage2D