处理图像时的步幅和缓冲区大小 [英] Stride and buffersize when processing images

查看:185
本文介绍了处理图像时的步幅和缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSDN上,我发现 (用于定义图像步幅)。简而言之,它是缓冲区中一行的宽度(以字节为单位)。

On MSDN I found this for the definition of the image stride. In short it is the width in bytes of one line in the buffer.

现在我在类似的缓冲区中有一个RGB图像,并且获得了步幅和图像宽度(以像素为单位),我想知道那里有多少填充字节已添加。

Now I have an RGB image in a similar buffer and I have been given the stride and the image width in pixels, and I want to know how much padding bytes there have been added.

这是简单的大步吗-3 *图像宽度,因为每个像素有3个字节(RGB)?

Is this simply stride - 3 * imagewidth since I have 3 bytes (RGB) per pixel?

unsafe private void setPrevFrame(IntPtr pBuffer)
    {
        prevFrame = new byte[m_videoWidth, m_videoHeight, 3];
        Byte* b = (byte*) pBuffer;
        for (int i = 0; i < m_videoWidth; i++)
        {
            for (int j = 0; j < m_videoHeight; j++)
            {
                for (int k = 0; k < 3; k++)
                {
                    prevFrame[i,j,k] = *b;
                    b++;
                }
            }
            b += (m_stride - 3 * m_videoHeight);
        }
    }

这是我不确定的最后一行代码大约

It's the last line of code I'm not sure about

推荐答案

跨度将被填充到4字节边界:

Stride will be padded to a 4 byte boundary:

因此,如果您的图像宽度为 W 并且每个像素为3个字节:

So if your image width is W and it is 3 bytes per pixel:

int strideWidth = ((W * 3) - 1) / 4 * 4 + 4; 
//or simply
int strideWidth = Math.Abs(bData.Stride);

//and so
int padding = strideWidth - W * 3;

如果您使用的是 .Stride BitmapData 对象的属性,则必须 Abs 该值,因为它可能为负。

If you are using the .Stride property of the BitmapData object, you must Abs that value because it may be negative.

对问题的编辑之后:

您无需知道填充就可以迭代图像的所有字节:

There is no need for you to know the padding in order to iterate all the bytes of the image:

unsafe private void setPrevFrame(IntPtr pBuffer)
{
    for (int j = 0; j < m_videoHeight; j++)
    {
        b = scan0 + j * stride; //the first byte of the first pixel of the row
        for (int i = 0; i < m_videoWidth; i++)
        {
           ...
        }
    }
}

如前所述,您以前的方法在自下而上的图像上也会失败在您发布的MSDN链接中。

Your previous method will also fail on bottom-up images, as described in the MSDN link you posted.

这篇关于处理图像时的步幅和缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆