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.
Convert System.Drawing.Image to Emgu.CV.Image<Gray,byte>
Asked Answered
// 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);
I have seen this answer everywhere, but it does not work. I'm using Emgu.CV 4.6. Image<Gray, byte> will not accept a bitmap. –
Phlegmy
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
Remember to check bitmap locks if you are trying to display the images as well. weblogs.asp.net/anasghanem/… –
Irretrievable
Why 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. –
Calaboose @Thomas Weller Edited my answer to reflect true value of 'strid'. thanks. –
Tiler
© 2022 - 2024 — McMap. All rights reserved.