A
A
Alexander Kosheverov2014-02-20 17:25:18
C++ / C#
Alexander Kosheverov, 2014-02-20 17:25:18

How to convert Bitmap to IplImage?

Actually, there is a certain Bitmap (with pixel format Format8bppIndexed: after conversion to Grayscale). How do I convert it to IplImage (OpenCV image format).
Tried like this:

public static Bitmap Binaryze(Bitmap inBitmap)
        {
            var g = new GrayscaleY();
            var inGray = g.Apply(inBitmap);
            var inPtr = IntPtr.Zero;
            var bd = inGray.LockBits(new Rectangle(0, 0, inGray.Width, inGray.Height), ImageLockMode.ReadWrite,
                PixelFormat.Format8bppIndexed);
            try
            {
                inPtr = bd.Scan0;
                var b1 = Sauvola.Convert(inPtr, 15, 0.05, 256);
                var b2 = Benrsen.Convert(inPtr, 3, 100);
                inGray.UnlockBits(bd);
                ...
            }
            ...
        }

Inside, for example, Sauvola:
public class Sauvola
    {
        public static Bitmap Convert(IntPtr src, int Size = 31, double K = 0.9, int R = 32)
        {
            var srcImage = new IplImage(src, true);
            var imgSaulova = new IplImage(srcImage.Size, BitDepth.U8, 1); //вот тут ошибка. Очевидно, что srcImage вышел каким-то "не таким"
            Binarizer.SauvolaFast(srcImage, imgSaulova, Size, K, R);
            return imgSaulova.ToBitmap();
        }
    }

Throws a BadImageFormatException.
I use AForge, OpenCVSharp.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DrReiz, 2014-03-26
@Iworb

bmp.ToIplImage() //требуется подключение OpenCvSharp.Extensions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question