17

I am using the OpenCV library for image processing.

I want to convert a System.Drawing.Bitmap to an Image<Bgr, Byte>. How can I do this?

Michael Liu
  • 52,147
  • 13
  • 117
  • 150

6 Answers6

20

The Image constructor has a Bitmap overload (assuming you're using the Emgu CV wrapper since you've marked it .NET).

Image<Bgr, Byte> myImage = new Image<Bgr, Byte>(myBitmap); 
keyboardP
  • 68,824
  • 13
  • 156
  • 205
17

In .NET Emgu.CV 4.4.0.4099 I had to install Emgu.CV.Bitmap 4.4.0.4099 and Emgu.CV.runtime.windows in order to use the bitmap.ToImage<Bgr, byte>() extension method.

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
  • 1
    Thanks, just what I was looking for. Especially since their documentation pages give an exception when I browse to them... – Carra Feb 04 '21 at 22:23
16

The constructor for Image<Bgr, byte> no longer accepts Bitmap as parameter. I had to use the following code for Emgu version 4.3:

Image<Bgr, byte> emguImage = bitmap.ToImage<Bgr, byte>();

I found it on github and in patch notes. The official documentation tutorials were not properly updated.

miran80
  • 945
  • 7
  • 22
3

I am using Emgu.CV 4.5.1.4349 on NetStandard 2.1 Need to install Emgu.CV.Bitmap and Emgu.CV.runtime.windows in order to convert your Bitmap to Image<Bgr, byte>

var myImage = myBitmap.ToImage<Bgr, byte>()

0

for whom to be concern: you should add dependency dll into project and change 'copy to output directory' property to 'copy always' (add -> existing item) from Emgu\emgucv-windows-universal-cuda 2.9.0.1922\bin\x86

in my project add : opencv_core290.dll , opencv_highgui290.dll , opencv_ffmpeg290.dll , opencv_imageproc290.dll and cudart32_55.dll

from: The type initializer for 'Emgu.CV.CvInvoke' threw an exception

0

I'm using Emgu 4.3.0.3890 and I had the same problem I integrated my code with my team. The line that gave me trouble was:

Image<Bgr, byte> img = bitmap.ToImage<Bgr, byte>();

I didn't see that the build created two new directories named x86 and x64. When I discovered it, I added these two directories to the location of my binary files and every thing worked. I didn't have the time to to elimination to see which one of the files in these directories is responsible for it but hey, it worked. :-)