Is there any way to convert an Image of type System.Drawing.Image in to Image of Emgu.CV.Image type or vice versa on EmguCV using C#? I will explain per your request if additional explanation is needed about the purpose of doing this.
Asked
Active
Viewed 1.4k times
2 Answers
8
// Converting the master image to a bitmap
Bitmap masterImage = (Bitmap) pbxMaster.Image;
// Normalizing it to grayscale
Image<Gray, Byte> normalizedMasterImage = new Image<Gray, Byte>(masterImage);

Waleed
- 3,105
- 2
- 24
- 31
-
I have seen this answer everywhere, but it does not work. I'm using Emgu.CV 4.6. Image
will not accept a bitmap. – Rafael Ventura Nov 30 '22 at 12:59
5
EmguCV version 4.2.0.3636 [and forward] works with below code:
using System.Drawing;
using System.Drawing.Imaging;
using Emgu.CV;
using Emgu.CV.Structure;
//inputImage type is System.Drawing.Image
Bitmap bitmapImage = new Bitmap(pictureBox1.Image);
Rectangle rectangle = new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height);//System.Drawing
BitmapData bmpData = bitmapImage.LockBits(rectangle, ImageLockMode.ReadWrite, bitmapImage.PixelFormat);//System.Drawing.Imaging
Image<Bgr, byte> outputImage = new Image<Bgr, byte>(bitmapImage.Width, bitmapImage.Height, bmpData.Stride, bmpData.Scan0);//(IntPtr)
//outputImage type is Emgu.CV.Image

Sina Hoseinkhani
- 318
- 4
- 15
-
Remember to check bitmap locks if you are trying to display the images as well. https://weblogs.asp.net/anasghanem/solving-quot-a-generic-error-occurred-in-gdi-quot-exception – thusith.92 Mar 24 '20 at 01:09
-
1Why not use `bmpdata.Stride`? The calculation of Width*4 or Width*3 may be off by some pixels, because internally, data must be aligned to a multiple of 8 or so. – Thomas Weller Jul 10 '20 at 08:24
-
@Thomas Weller Edited my answer to reflect true value of 'strid'. thanks. – Sina Hoseinkhani Jul 11 '20 at 18:14