Getting distorted images after sending them from C# to OpenCV in C++?

前端 未结 1 1990
执笔经年
执笔经年 2020-12-19 18:13

I created a C DLL out of my C++ class which uses OpenCV for image manipulations and want to use this DLL in my C# application. Currently, this is how I have imp

相关标签:
1条回答
  • 2020-12-19 18:28

    Your initial issue is to do with what is called stride or pitch of image buffers. Basically for performance reasons pixel row values can be memory aligned, here we see that in your case it's causing the pixel rows to not align because the row size is not equal to the pixel row width.

    The general case is:

    resolution width * bit-depth (in bytes) * num of channels + padding
    

    in your case the bitmap class state:

    The stride is the width of a single row of pixels (a scan line), rounded up to a four-byte boundary

    So if we look at the problematic image, it has a resolution of 1414 pixel width, this is a 8-bit RGB bitmap so if we do the maths:

    1414 * 1 * 3 (we have RGB so 3 channels) = 4242 bytes
    

    So now divide by 4-bytes:

    4242 / 4 = 1060.5
    

    So we are left with 0.5 * 4 bytes = 2 bytes padding

    So the stride is in fact 4244 bytes.

    So this needs to be passed through so that the stride is correct.

    Looking at what you're doing, I'd pass the file as memory to your openCV dll, this should be able to call imdecode which will sniff the file type, additionally you can pass the flag cv::IMREAD_GRAYSCALE which will load the image and convert the grayscale on the fly.

    0 讨论(0)
提交回复
热议问题