简体   繁体   English

如何将emgu图像转换为XNA Texture2D?

[英]How to convert an emgu image to an XNA Texture2D?

Image<Bgr, Byte> video = cap.QueryFrame();
Texture2D t = new Texture2D(GraphicsDevice, video.Width, video.Height, false, SurfaceFormat.Color);
t.SetData<byte>(video.Bytes);

ArgumentException was unhandled 未处理ArgumentException

The size of the data passed in is too large or too small for this resource. 对于此资源,传入的数据大小太大或太小。

My perfered way is to 'save' the image into memory then load it with the Texture2D.FromStream function. 我的方法是将图像“保存”到内存中,然后使用Texture2D.FromStream函数加载它。

Texture2D t;

using(MemoryStream memStream = new MemoryStream())
{
    Image<Bgr, Byte> video = cap.QueryFrame();
    cap.save(memStream, ImageFormat.PNG);
    t = Texture2D.FromStream(GraphicsDevice, memStream, video.Width, video.Height, 1f)
}

This will work with the NonPremultiplied BlendStates but if you want to use Premultiplied alpha you should run the Texture2D though the following function. 这将适用于NonPremultiplied BlendStates,但如果要使用Premultiplied alpha,则应通过以下功能运行Texture2D。 This function simply uses the GPU to quickly premultiply the texture's alpha the same as the content processor would. 此功能仅使用GPU即可快速将纹理的Alpha乘以内容处理器所用的Alpha。

    static public void PreMultiplyAlpha(this Texture2D texture) {            

        //Setup a render target to hold our final texture which will have premulitplied color values
        var result = new RenderTarget2D(texture.GraphicsDevice, texture.Width, texture.Height);

        texture.GraphicsDevice.SetRenderTarget(result);
        texture.GraphicsDevice.Clear(Color.Black);

        // Using default blending function
        // (source × Blend.SourceAlpha) + (destination × Blend.InvSourceAlpha)
        // Destination is zero so the reduces to
        // (source × Blend.SourceAlpha)
        // So this multiplies our color values by the alpha value and draws it to the RenderTarget
        var blendColor = new BlendState {
            ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue,
            AlphaDestinationBlend = Blend.Zero,
            ColorDestinationBlend = Blend.Zero,
            AlphaSourceBlend = Blend.SourceAlpha,
            ColorSourceBlend = Blend.SourceAlpha
        };

        var spriteBatch = new SpriteBatch(texture.GraphicsDevice);
        spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
        spriteBatch.Draw(texture, texture.Bounds, Color.White);
        spriteBatch.End();

        // Simply copy over the alpha channel
        var blendAlpha = new BlendState {
            ColorWriteChannels = ColorWriteChannels.Alpha,
            AlphaDestinationBlend = Blend.Zero,
            ColorDestinationBlend = Blend.Zero,
            AlphaSourceBlend = Blend.One,
            ColorSourceBlend = Blend.One
        };

        spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
        spriteBatch.Draw(texture, texture.Bounds, Color.White);
        spriteBatch.End();

        texture.GraphicsDevice.SetRenderTarget(null);

        var t = new Color[result.Width * result.Height];
        result.GetData(t);
        texture.SetData(t);
    }

the video stores images in (Blue, green, Red) format. 视频以(蓝色,绿色,红色)格式存储图像。 Whereas texture2d expects an extra alpha pixel as well. 而texture2d也需要一个额外的alpha像素。

Image<Bgr, Byte> video = cap.QueryFrame();
Image<Bgra, Byte> video2 = video.Convert<Bgra, Byte>();
Texture2D t = new Texture2D(GraphicsDevice, video.Width, video.Height, false, SurfaceFormat.Color);
t.SetData<byte>(video2.Bytes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM