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

相关文章

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

Python WebSockets 库从基础到实战使用举例

《PythonWebSockets库从基础到实战使用举例》WebSocket是一种全双工、持久化的网络通信协议,适用于需要低延迟的应用,如实时聊天、股票行情推送、在线协作、多人游戏等,本文给大家介... 目录1. 引言2. 为什么使用 WebSocket?3. 安装 WebSockets 库4. 使用 We

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十

Java使用正则提取字符串中的内容的详细步骤

《Java使用正则提取字符串中的内容的详细步骤》:本文主要介绍Java中使用正则表达式提取字符串内容的方法,通过Pattern和Matcher类实现,涵盖编译正则、查找匹配、分组捕获、数字与邮箱提... 目录1. 基础流程2. 关键方法说明3. 常见场景示例场景1:提取所有数字场景2:提取邮箱地址4. 高级

使用SpringBoot+InfluxDB实现高效数据存储与查询

《使用SpringBoot+InfluxDB实现高效数据存储与查询》InfluxDB是一个开源的时间序列数据库,特别适合处理带有时间戳的监控数据、指标数据等,下面详细介绍如何在SpringBoot项目... 目录1、项目介绍2、 InfluxDB 介绍3、Spring Boot 配置 InfluxDB4、I

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

使用Python实现一个简易计算器的新手指南

《使用Python实现一个简易计算器的新手指南》计算器是编程入门的经典项目,它涵盖了变量、输入输出、条件判断等核心编程概念,通过这个小项目,可以快速掌握Python的基础语法,并为后续更复杂的项目打下... 目录准备工作基础概念解析分步实现计算器第一步:获取用户输入第二步:实现基本运算第三步:显示计算结果进