As titled. I am having a problem of converting System.Drawing.Image to Emgu.CV.Mat I tried to covert it from Drawing.Image to CV.Image but keep having Exceptions.
Is there any other solution available? Any help render is kindly appreciated.
A OpenCV or EmguCV IMage has a Mat header in it. The trick I found was to get the System.Drawing.Image into a Image<>. If you are having trouble with exceptions be sure you are compiling for x64 if you are using EmguCV 3.1.
Here is a simple method to get a Mat object:
private Mat GetMatFromSDImage(System.Drawing.Image image)
{
int stride = 0;
Bitmap bmp = new Bitmap(image);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat);
System.Drawing.Imaging.PixelFormat pf = bmp.PixelFormat;
if (pf == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
{
stride = bmp.Width * 4;
}
else
{
stride = bmp.Width * 3;
}
Image<Bgra, byte> cvImage = new Image<Bgra, byte>(bmp.Width, bmp.Height, stride, (IntPtr)bmpData.Scan0);
bmp.UnlockBits(bmpData);
return cvImage.Mat;
}
Hope this helps! Doug
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With