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

风流意气都作罢 提交于 2019-11-29 14:48:58

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!