如何将RGB24像素格式的WriteableBitmap转换为EmguCV图像< Bgr,Byte>格式? [英] How to convert WriteableBitmap in RGB24 pixel format into a EmguCV image<Bgr, Byte> format?

查看:775
本文介绍了如何将RGB24像素格式的WriteableBitmap转换为EmguCV图像< Bgr,Byte>格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将具有Rgb24的WriteableBitmap转换为pixelFormat。我想将相同的图像存储到具有Bgr格式的EmguCV图像中。我写了下面的代码,但它没有给出适当的结果。

I am trying to convert a WriteableBitmap which is having Rgb24 as a pixelFormat. I want to store the same image into a EmguCV Image having Bgr format. I have written a following code but it is not giving appropriate results.

public unsafe void Convert(WriteableBitmap bitmap)
    {
        byte[] retVal = new byte[bitmap.PixelWidth * bitmap.PixelHeight * 4];
        bitmap.CopyPixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), retVal, bitmap.PixelWidth * 4, 0);

        Bitmap b = new Bitmap(bitmap.PixelWidth, bitmap.PixelHeight);
        int k = 0;
        byte red, green, blue, alpha;
        for (int i = 0; i < bitmap.PixelWidth; i++)
        {                
            for (int j = 0; j < bitmap.PixelHeight && k<retVal.Length; j++)
            {
                alpha = retVal[k++];
                blue = retVal[k++];
                green = retVal[k++];
                red = retVal[k++];

                System.Drawing.Color c = new System.Drawing.Color();
                c = System.Drawing.Color.FromArgb(alpha, red, green, blue);

                b.SetPixel(i, j, c);   
            }
        }
        currentFrame = new Image<Bgr, byte>(b);

        currentFrame.Save("Converted.jpg");
}

提前致谢。

推荐答案

您是否仍然收到此错误?我终于通过将WriteableBitmap变量中的数据转储到MemoryStream中然后从那里转换为Bitmap变量来实现这一点。

Are you still getting this error? I finally got this working by dumping the data from the WriteableBitmap variable into a MemoryStream and then from there into a Bitmap variable.

下面是一个例子:
位图是WriteableBitmap变量

Below is an example: bitmap is the WriteableBitmap variable

BitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap);
MemoryStream ms = new MemoryStream();

encoder.Save(ms);
Bitmap b=new Bitmap(ms);
Image<Bgr, Byte> image = new Image<Bgr, Byte>(b);

我认为这种方式更好,因为你没有经历嵌套的for循环,这可能会慢得多。无论如何希望这对你有用

I think this way is a better approach because you don't have go through the nested for loops which could be far slower. Anyway hope this works for you

这篇关于如何将RGB24像素格式的WriteableBitmap转换为EmguCV图像&lt; Bgr,Byte&gt;格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆