在WritePixels中计算步幅和偏移量的问题 [英] Problems calculating stride and offset in WritePixels

查看:137
本文介绍了在WritePixels中计算步幅和偏移量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Kinect应用程序,在其中使用传感器中的彩色图像.我得到一个640 x 480彩色图像,使用WritePixels方法将数据从传感器复制到WriteableBitmap.当我使用整个彩色图像时,我没有任何问题.但是我只想使用图像的中间部分.但是我无法大步前进或抵消吗?

I am writing a Kinect application, where I use the color image from the sensor. I get a 640 x 480 color image, I copy the data from the sensor to a WriteableBitmap, with the WritePixels method. When I use the whole color image I have no issues. But I would like to use only the middle part of the image. But I can't get stride and or offset right?

要复制整个图像,请执行以下操作:

To copy the whole image I do the following:

_colorImageWritableBitmap.WritePixels(
                new Int32Rect(0, 0, colorImageFrame.Width, colorImageFrame.Height),
                _colorImageData,
                colorImageFrame.Width * Bgr32BytesPerPixel,
                0);

正如我提到的,我只想要图像的中间部分.我想以185px的宽度开始,然后选择下一个270px,然后在此处停止.我用整个高度.

As I mention I only want the middle part of the image. I would like to start at a width at 185px and take the next 270px, and stop there. And I use the the whole height.

我的PixelFormat是bgr32,因此要计算字节pr.我使用的像素:

My PixelFormat is bgr32, so to calculate the byte pr. pixel I use:

var bytesPrPixel = (PixelFormats.Bgr32.BitsPerPixel + 7)/8;

我的大步前进:

var stride = bytesPrPixel*width;

writepixel方法:

The writepixel method:

_colorImageWritableBitmap.WritePixels(
                new Int32Rect(0, 0, colorImageFrame.Width, colorImageFrame.Height),
                _colorImageData, stride, offset);

但是当我将宽度更改为640以外时,图像会出错(隐藏在噪点中).

But when I change the width to other than 640, the image gets wrong (hidden in noise).

有人可以帮助我,以了解我在这里做错了什么吗?

Can someone help me, to understand what I am doing wrong here?

推荐答案

您必须正确复制源位图中的像素.假设源colorImageFrame也是BitmapSource,则可以这样进行:

You have to properly copy the pixels from the source bitmap. Assuming that the source colorImageFrame is also a BitmapSource, you would do it this way:

var width = 270;
var height = 480;
var x = (colorImageFrame.PixelWidth - width) / 2;
var y = 0;
var stride = (width * colorImageFrame.Format.BitsPerPixel + 7) / 8;
var pixels = new byte[height * stride];
colorImageFrame.CopyPixels(new Int32Rect(x, y, width, height), pixels, stride, 0);

现在,您可以通过以下方式将像素缓冲区写入到WriteableBitmap中:

Now you could write the pixel buffer to your WriteableBitmap by:

colorImageWritableBitmap.WritePixels(
    new Int32Rect(0, 0, width, height), pixels, stride, 0);

或者不用创建WriteableBitmap,而只需创建一个新的BitmapSource,例如:

Or instead of using WriteableBitmap, you just create a new BitmapSource, like:

var targetBitmap = BitmapSource.Create(
    width, height, 96, 96, colorImageFrame.Format, null, pixels, stride);

但是,创建源位图的最简单方法可能是使用CroppedBitmap,如下所示:

However, the easiest way to create a crop of the source bitmap might be to used a CroppedBitmap like this:

var targetBitmap = new CroppedBitmap(
    colorImageFrame, new Int32Rect(x, y, width, height));

这篇关于在WritePixels中计算步幅和偏移量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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