Convert System.Drawing.Image to Emgu.CV.Image<Gray,byte>
Asked Answered
S

2

6

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.

Seagirt answered 6/5, 2013 at 21:54 Comment(0)
M
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);
Mcelroy answered 6/5, 2013 at 22:4 Comment(1)
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
T
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
Tiler answered 1/2, 2020 at 12:20 Comment(3)
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.