Kinect for windows 开发入门 五:彩色数据获取和使用

2024-06-12 18:32

本文主要是介绍Kinect for windows 开发入门 五:彩色数据获取和使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

背景知识

1.      WriteableBitmap之于Bitmap,就好像StringBuilder之于String。可以减少内存消耗。WriteableBitmap在初始化时需要指定高度,宽度和格式。上一节示例中,获取每帧图像都需要创建并初始化一个新的Bitmap,对GPU来说是一个极大的负担。用WriteableBitmap替代可以很大程度上提高性能。

2.      色彩数据流的解析式根据格式而定的。ColorImageFormat枚举用来指定格式。下表列出了支持的格式。简单的说支持四种颜色格式RGBYUVBayerInfrared;两种分辨率640*4801280*960;最快支持30帧每秒;

ColorImageFormat

Description

含义

InfraredResolution640x480Fps30

16 bits, using the top 10 bits from a PixelFormats.Gray16  format (with the 6 least significant bits always set to 0) whose resolution is  640 x 480 and frame rate is 30 frames per second. Introduced in 1.6.

格式:红外(有亮度无彩色)

分辨率:640*480

最高频:30/

RawBayerResolution1280x960Fps12

Bayer data (8 bits per pixel, layout in alternating  pixels of red, green and blue) whose resolution is 1280 x 960 and frame rate  is 12 frames per second. Introduced in 1.6.

格式:Bayer

分辨率:1280*960

最高频:12/

RawBayerResolution640x480Fps30

Bayer data (8 bits per pixel, layout in alternating  pixels of red, green and blue) whose resolution is 640 x 480 and frame rate  is 30 frames per second. Introduced in 1.6.

格式:Bayer

分辨率:640*480

最高频:30/

RawYuvResolution640x480Fps15

Raw YUV data whose resolution is 640 x 480 and frame  rate is 15 frames per second.

格式:YUV

分辨率:640*480

最高频:15/

RgbResolution1280x960Fps12

RBG data whose resolution is 1280 x 960 and frame rate  is 12 frames per second.

格式:RGB

分辨率:1280*960

最高频:12/

RgbResolution640x480Fps30

RBG data whose resolution is 640 x 480 and frame rate is  30 frames per second.

格式:RGB

分辨率:640*480

最高频:30/

YuvResolution640x480Fps15

YUV data whose resolution is 640 x 480 and frame rate is  15 frames per second.

格式:YUV

分辨率:640*480

最高频:15/

Undefined

The format is not defined.

图像格式未定义

3.      示例中使用的颜色格式是Bgr32,每个像素占4个字节,每个字节8个位。第一个字节是蓝色通道,第二个是绿色,第三个是红色,第四个待用(表示像素的Alpha?或者透明度)。

4.      Stride是指图像中一行像素所占的字节(RGBwidth*height*BytesPerPixel=640*480*4).

5.      Stream会为每一帧图像加一个编号(不一定连续)和Timestamp

6.      获取ColorStream有两种方式,主动和被动,示例中采用被动模式(多用),即被动的持续接受sensor传过来的信息;主动则是,根据需要定时地去获取,可参考“拉”的模式

7.      本示例展示了一些简单的图像处理。下面代码中以EditColor_开头的方法就是各种处理方法。

 

示例代码

        privateKinectSensor kinect;

        privateWriteableBitmapcolorImageBitmap;

        privateInt32RectcolorImageBitmapRect;

        privateint colorImageStride;

        privatebyte[] colorImagePixelData;

 

 

        // Get and set connected Kinect Sensor;

        publicKinectSensor Kinect

        {

            get

            {

                returnthis.kinect;

            }

 

            set

            {

                if (this.kinect != value)

                {

                    if (null != this.kinect)

                   {

                       UninitializeKinectSensor(this.kinect);

                       this.kinect= null;

                   }

                }

 

                if (null != value && KinectStatus.Connected== value.Status)

                {

                    this.kinect = value;

                   InitializeKinectSensor(this.kinect);

                }

            }

        }

 

        // Bind color stream handler

        privatevoid InitializeKinectSensor(KinectSensorkinectSensor)

        {

            if (null != kinectSensor)

            {

                ColorImageStreamcolorStream = kinectSensor.ColorStream;

                colorStream.Enable();

 

                this.colorImageBitmap = newWriteableBitmap(colorStream.FrameWidth,colorStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);

                this.colorImageBitmapRect = newInt32Rect(0, 0,colorStream.FrameWidth, colorStream.FrameHeight);

                this.colorImageStride =colorStream.FrameWidth * colorStream.FrameBytesPerPixel;

               ColorImageElement.Source = this.colorImageBitmap;

 

               kinectSensor.ColorFrameReady += kinectSensor_ColorFrameReady;

               kinectSensor.Start();

            }

        }

 

        privatevoid UninitializeKinectSensor(KinectSensorkinectSensor)

        {

            if (kinectSensor != null)

            {

               kinectSensor.Stop();

               kinectSensor.ColorFrameReady -= newEventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);

            }

        }

 

        privatevoidkinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)

        {

            // Use 'using' to dispose the frame after using.

            // 30 frame per second

            using (ColorImageFrame frame =e.OpenColorImageFrame())

            {

                if (null != frame)

                {

                    byte[] pixelData = newbyte[frame.PixelDataLength];

                   frame.CopyPixelDataTo(pixelData);

                   EditColor_HighSaturation(pixelData, frame.BytesPerPixel);

                    this.colorImageBitmap.WritePixels(this.colorImageBitmapRect,pixelData, this.colorImageStride,0);

                }

            }

 

        }

 

        privatevoid EditColor_Inverted(byte[] pixelData, int bytesPerPixel)

        {

            for (int i = 0; i <pixelData.Length; i += bytesPerPixel)

            {

               pixelData[i] = 0x00;//Blue

               pixelData[i + 1] = 0x00;//Green

            }

        }

 

        privatevoid EditColor_Gray(byte[] pixelData, int bytesPerPixel)

        {

            for (int i = 0; i <pixelData.Length; i += bytesPerPixel)

            {

                byte gray = Math.Max(pixelData[i],pixelData[i + 1]);

                gray= Math.Max(gray,pixelData[i + 2]);

               pixelData[i] = gray;

               pixelData[i + 1] = gray;

               pixelData[i + 2] = gray;

            }

        }

 

        privatevoid EditColor_BlackWhite(byte[] pixelData, int bytesPerPixel)

        {

            for (int i = 0; i <pixelData.Length; i += bytesPerPixel)

            {

                byte gray = Math.Min(pixelData[i], pixelData[i + 1]);

                gray= Math.Min(gray,pixelData[i + 2]);

               pixelData[i] = gray;

               pixelData[i + 1] = gray;

               pixelData[i + 2] = gray;

            }

        }

 

        privatevoid EditColor_Apocalyptic(byte[] pixelData, int bytesPerPixel)

        {

            for (int i = 0; i <pixelData.Length; i += bytesPerPixel)

            {

               pixelData[i] = pixelData[i + 1];

               pixelData[i + 2] = (byte)~pixelData[i + 2];

            }

        }

 

        privatevoid EditColor_WashedOut(byte[] pixelData, int bytesPerPixel)

        {

            for (int i = 0; i <pixelData.Length; i += bytesPerPixel)

            {

                double gray =(pixelData[i] * 0.11) + (pixelData[i + 1] * 0.59) + (pixelData[i + 2] * 0.3);

                double desaturation =0.75;

               pixelData[i] = (byte)(pixelData[i] + desaturation * (gray - pixelData[i]));

                pixelData[i + 1] = (byte)(pixelData[i + 1] +desaturation * (gray - pixelData[i + 1]));

               pixelData[i + 2] = (byte)(pixelData[i + 2] + desaturation * (gray - pixelData[i +2]));

            }

        }

 

        privatevoid EditColor_HighSaturation(byte[] pixelData, int bytesPerPixel)

        {

            for (int i = 0; i <pixelData.Length; i += bytesPerPixel)

            {

                if (pixelData[i] < 0x33 ||pixelData[i] > 0xE5)

                {

                   pixelData[i] = 0x00;

                }

                else

                {

                   pixelData[i] = 0Xff;

                }

 

                if (pixelData[i + 1] < 0x33|| pixelData[i + 1] > 0xE5)

                {

                   pixelData[i + 1] = 0x00;

                }

                else

                {

                   pixelData[i + 1] = 0Xff;

                }

 

                if (pixelData[i + 2] < 0x33|| pixelData[i + 2] > 0xE5)

                {

                   pixelData[i + 2] = 0x00;

                }

                else

                {

                   pixelData[i + 1] = 0Xff;

                }

            }

        }

 

        public MainWindow()

        {

           InitializeComponent();

            // Action according to the app start or stop

            this.Loaded += (sender, e) =>DiscoverKinectSensor();

            this.Unloaded += (s, e) => this.kinect = null;

        }

 

        privatevoid DiscoverKinectSensor()

        {

            KinectSensor.KinectSensors.StatusChanged+= KinectSensors_StatusChanged;

            this.Kinect = KinectSensor.KinectSensors.FirstOrDefault(x=> x.Status == KinectStatus.Connected);

        }

 

        // Monitor Kinect Sensor status, get the first one if it'sconnected, remove if it's disconnected.

        privatevoid KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)

        {

            switch (e.Status)

            {

                caseKinectStatus.Connected:

                    if (null == this.kinect)

                   {

                       this.kinect= e.Sensor;

                   }

                    break;

                caseKinectStatus.Disconnected:

                    if (this.kinect == e.Sensor)

                    {

                       this.kinect= null;

                       this.kinect= KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);

                       if (this.kinect == null)

                       {

                           // Message to show that all kinects weredisconnected.

                       }

                   }

                    break;

            }

        }

 

效果演示

翻转:

灰化:

黑白:

血腥:

冲洗:

饱和:

 

这篇关于Kinect for windows 开发入门 五:彩色数据获取和使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1054996

相关文章

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤