How to create Emgu Image from System.Drawing.Image

2019-02-19 05:32发布

I have a source that gives me a jpeg in memory stream (Bytes).
I can convert it to System.Drawing.Image but I don't know how
to convert it to Emgu Image.

Maybe a direct conversion to Emgu Image is possible ?
I'm working in C# under VS2010.
Thanks.
SW

3条回答
姐就是有狂的资本
2楼-- · 2019-02-19 06:06

You can first convert System.Drawing.Image object to a Bitmap and then create an Emgu.CV.Image with that bitmap. The code is as follows:

System.Drawing.Image image;
Bitmap bmpImage = new Bitmap(image);
Emgu.CV.Image = new Emgu.CV.Image<Bgr, Byte>(bmpImage);

Better, if you have a memory stream, you can get a bitmap directly from the memory stream

MemoryStream ms;
Bitmap bmpImage = new Bitmap(ms);
Emgu.CV.Image = new Emgu.CV.Image<Bgr, Byte>(bmpImage);
查看更多
该账号已被封号
3楼-- · 2019-02-19 06:17

You can convert your array bytes to Emgu Image<,> with a code like the following...

public Image<Bgr, Byte> CreateImageFromBytesArray(byte[] bytes)
{
     MemoryStream ms = new MemoryStream(bytes);
     Bitmap bmpImage = (Bitmap) Image.FromStream(ms);
     return new Image<Bgr, byte>(bmpImage);         
}
查看更多
太酷不给撩
4楼-- · 2019-02-19 06:22

Using ps2010 solution, I had wrote this to get image from a http:

try
{                
    using (WebClient client = new WebClient())
    {                    
        data = client.DownloadData("http://LINK TO PICTURE");
    }       
}
catch (Exception ex)
{
    // error treatment
}

MemoryStream ms = new MemoryStream(data);
Bitmap bmpImage = new Bitmap(Image.FromStream(ms));
Emgu.CV.Image<Bgr, Byte> currentFrame = new Emgu.CV.Image<Bgr, Byte>(bmpImage);
gray = currentFrame.Convert<Gray, Byte>();
查看更多
登录 后发表回答