缩放后图像损坏

时间:2014-12-22 15:49:44

标签: image kinect emgucv

我正在使用Microsoft Kinect SDK v2从Kinect V2传感器读取彩色边框。我将帧数据复制到一个字节数组中,稍后将其转换为EmguCV Image。以下是代码中的代码段 -

// A pixel buffer to hold image data from the incoming color frame
private byte[] pixels = null;
private KinectSensor kinectSensor = null;
private ColorFrameReader colorFrameReader = null;
public KinectForm()
{
    this.kinectSensor = KinectSensor.GetDefault();
    this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader();
    this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived;
    // create the colorFrameDescription from the ColorFrameSource using Bgra format
    FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
    // Create a pixel buffer to hold the frame's image data as a byte array
    this.pixels = new byte[colorFrameDescription.Width * colorFrameDescription.Height * colorFrameDescription.BytesPerPixel];
    // open the sensor
    this.kinectSensor.Open();
    InitializeComponent();
}

private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
    using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
    {
        if (colorFrame != null)
        {
            FrameDescription colorFrameDescription = colorFrame.FrameDescription;
            if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra)
                colorFrame.CopyRawFrameDataToArray(pixels);
            else
                colorFrame.CopyConvertedFrameDataToArray(this.pixels, ColorImageFormat.Bgra);

            //Initialize Emgu CV image then assign byte array of pixels to it
            Image<Bgr, byte> img = new Image<Bgr, byte>(colorFrameDescription.Width, colorFrameDescription.Height);
            img.Bytes = pixels;

            imgBox.Image = img;//Show image in Emgu.CV.UI.ImageBox
        }
    }
}

转换后的图像在缩放超过25%后损坏。请参见下面的截图 -

50%放大 - 50% Zoom

25%放大 - 25% Zoom

12.5%放大 - 12.5% Zoom

1 个答案:

答案 0 :(得分:1)

&lt; TColor&GT;应该是Bgra,你可以从&#34;像素&#34; bytes array。

编辑: 由于Kinect颜色框的强度为0,图像不可见,所以我添加了这个代码来解决问题,我希望有更好(更快)的方法来解决它。 / p>

private void FixIntensity(byte[] p)
    {
        int i = 0;
        while (i < p.Length)
        {
            p[i+3] = 255;
            i += 4;
        }
    }