Skip to content

Memory Access Violation After Converting a Bitmap to a Mat #1601

@gdelacruz5

Description

@gdelacruz5

Summary of your issue

After creating a Mat from a Bitmap image I get a "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" error when trying to access its properties.

The strange thing is, the code works when running through my IDE but when I create a build and run an installed version I get the error.

Another strange thing is this was working when I was using OpenCvSharp3 but upgrading to OpenCvSharp4 produces this error. For reasons in other parts of the code, I cannot downgrade to OpenCvSharp3. The bitmap is in PixelFormat=Format32bppArgb

Environment

Microsoft Visual Studio Professional 2022
OpenCvSharp4 version=4.8.0.20230708
OpenCvSharp4.Extensions version=4.8.0.20230708
OpenCvSharp4.runtime.win version=4.8.0.20230708
OpenCvSharp4.Windows version=4.8.0.20230708
OpenCvSharp4.WpfExtensions version=4.8.0.20230708
Net framework = 48

What did you do when you faced the problem?

I've tried to copy the bitmap data out to a byte array and create a Mat from that. I still get the same memory access violations.

Example code:

This was the original code and it was working with OpenCvSharp3. Using OpenCvSharp4 produces the memory access violation:

        private static Mat ToMat(Bitmap src)
        {
            if (src == null)
            {
                throw new ArgumentNullException("src");
            }

            try
            {
                return src.ToMat().CvtColor(ColorConversionCodes.BGRA2GRAY);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to convert bitmap source to Mat type", ex);
            }
        }

This is what I am trying now:

        private static Mat ToMat(Bitmap src, out BitmapData data, byte[] byteBuffer)
        {
            if (src == null)
            {
                throw new ArgumentNullException("src");
            }

            data = new BitmapData();
            try
            {
                data = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadWrite, src.PixelFormat);
                IntPtr ptr = data.Scan0;
                int bytes = Math.Abs(data.Stride) * data.Height;
                System.Runtime.InteropServices.Marshal.Copy(ptr, byteBuffer, 0, bytes);

                switch (src.PixelFormat)
                {
                    case PixelFormat.Format8bppIndexed:
                        return new Mat(data.Height, data.Width, MatType.CV_8UC1, byteBuffer, data.Stride);
                    case PixelFormat.Format16bppArgb1555:
                        return new Mat(data.Height, data.Width, MatType.CV_16UC1, byteBuffer, data.Stride);
                    case PixelFormat.Format32bppArgb:
                        return new Mat(data.Height, data.Width, MatType.CV_32SC1, byteBuffer, data.Stride);
                    default:
                        throw new Exception("Cannot handle pixel format: " + src.PixelFormat);
                }
            }
            catch (Exception ex)
            {
                src.UnlockBits(data);
                throw new SierraException("Failed to convert bitmap source to Mat type", ex);
            }
            finally
            {
                src.UnlockBits(data);
            }
        }

Output:

When attempting to access the Mat here I get the following error:

using (Mat originalImageFloat = ToMat(originalImage, out BitmapData origImageData, origImageByteBuffer))
using (Mat patternImageFloat = ToMat(originalPattern.TemplateImage, out BitmapData origPatternData, templateImageByteBuffer))
{
            Mat destOrigImageFloat = new Mat(originalImageFloat.Height, originalImageFloat.Width, MatType.CV_32FC1);

Error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions