为什么WriteableBitmap BackBufferStride与EmguCV Mat Step不同

问题描述 投票:0回答:1

使用netcoreapp3.1和EmguCV 3.4.1,我一方面创建一个WriteableBitmap,另一方面创建一个EmguCV Mat。两者的大小相同,为2793 x 2585

var wb = new WriteableBitmap(2793, 2585, 96, 96, PixelFormats.Bgr24, null);
int wbStride = wb.BackBufferStride; //8380 

var m = new Mat(2585, 2793, DepthType.Cv8U, 3);
int matStride = m.Step; //8379

对于WriteableBitmap BackBufferStride = 8380,但对于Mat,我得到Step =8379。我发现存在两个经常用于计算步幅的公式:

a)步幅=((宽度* bitsPerPixel + 31)&〜31)>> 3;

b)步幅=(宽度* bitsPerPixel + 7)/ 8

公式a)产生的结果是EmguCV Mat的值中的WriteableBitmap BackBufferStride和公式b)的值。

为什么宽度和高度相同的步幅不同?哪个公式是正确的?

c# wpf emgucv
1个回答
0
投票

不确定为什么BackBufferStride确切返回该值。

但是,对于该位图的跨度,任何大于或等于8379的值都有效。只需尝试以下代码,然后任意添加即可,例如stride += 100;。只要确保跨步也用于计算像素缓冲区的大小即可。

var width = 2793;
var height = 2585;
var stride = (width * PixelFormats.Bgr24.BitsPerPixel + 7) / 8; // 8379

stride += 100;

var buffer = new byte[stride * height];

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x += 10)
    {
        buffer[stride * y + 3 * x + 0] = 0xFF;
        buffer[stride * y + 3 * x + 1] = 0xFF;
        buffer[stride * y + 3 * x + 2] = 0xFF;
    }
}

var bitmap = BitmapSource.Create(
    width, height, 96, 96, PixelFormats.Bgr24, null, buffer, stride);
© www.soinside.com 2019 - 2024. All rights reserved.