Kinect v2 + WPF获取RGB与Depth图像
date: 2017-09-04 14:51:07
Kinect V2的Depth传感器采用的是「Time of Flight(TOF)」的方式,
通过从投射的红外线反射后返回的时间来取得Depth信息。
本文将Kinect v2 + WPF来得到Kinect所获取的RGB(1920×1080)及Depth(512×424)图像
第一步:Kinect v2开发环境(仅限于本文)
- Visual Studio 2017
- 下载Kinect for Windows SDK 2.0并安装
第二步:创建工程
- 打开Visual Studio 2017, 创建一个WPF工程,名字随意(本文中例子为KinectTest)
- 在Solution Explorer中,右键单击KinectTest,在右键菜单中选择“Add Reference…”。弹出对话框后,在程序集中选择“Microsoft.Kinect”
第三步:编写代码
MainWindow.xaml代码
<Window x:Class="KinectTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:KinectTest"
mc:Ignorable="d"
Title="KinectTest" Height="1080" Width="1600">
<Grid>
<Image Name="RGB_Viewer" Source="{Binding ColorSource}" HorizontalAlignment="Left" Width="960" Height="540" Margin="0, 0, 0, 0"></Image>
<Image Name="Depth_Viewer" Source="{Binding DepthSource}" Width="512" Height="424" HorizontalAlignment="Right"></Image>
</Grid>
</Window>
MainWindow.xaml.cs代码
```cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
namespace KinectTest
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
private KinectSensor kinect = null; //用来标记kinect摄像头的实例
private ColorFrameReader colorFrame = null; //用来处理和保存摄像头传过来的彩色影像帧数据
private WriteableBitmap colorBitmap = null; //用来向Image控件中填充彩色影像数据
private FrameDescription colorFrameDescription = null; //用来描述彩色影响帧数据形态的参数
private const int MapDepthToByte = 8000 / 256;
private DepthFrameReader depthFrame = null;
private WriteableBitmap depthBitmap = null;
private FrameDescription depthFrameDescription = null;
private byte[] depthPixels = null;
public MainWindow()
{
this.kinect = KinectSensor.GetDefault(); //获取第一个或默认的Kinect摄像头
this.colorFrame = kinect.ColorFrameSource.OpenReader(); //打开彩色影像数据的获取接口
this.colorFrame.FrameArrived += ColorFrame_FrameArrived; //建立监听事件,当有彩色影像数据帧到达时触发
this.colorFrameDescription = this.kinect.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
//按照Bgra格式设定数据帧的描述信息(B:Blue,G:Green,R:Red,A:Alpha)
this.colorBitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
//根据数据帧的宽高创建colorBitmap的实例
this.depthFrame = kinect.DepthFrameSource.OpenReader();
this.depthFrame.FrameArrived += DepthFrame_FrameArrived;
this.depthFrameDescription = kinect.DepthFrameSource.FrameDescription;
this.depthBitmap = new WriteableBitmap(depthFrameDescription.Width, depthFrameDescription.Height, 96.0, 96.0, PixelFormats.Gray8, null);
this.depthPixels = new byte[this.depthFrameDescription.Width * this.depthFrameDescription.Height];
this.kinect.Open(); //启动kinect摄像头
this.DataContext = this;
InitializeComponent();
}
private void ColorFrame_FrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
using (ColorFrame frame = e.FrameReference.AcquireFrame()) //建立一个ColorFrame的实例frame保存送过来的帧,通过using保证走完函数后及时释放相应资源
{
if (frame != null)
{
this.colorBitmap.Lock(); //锁定一下数据文件,准备进行填充
frame.CopyConvertedFrameDataToIntPtr(this.colorBitmap.BackBuffer, (uint)(this.colorFrameDescription.Width * this.colorFrameDescription.Height * 4), ColorImageFormat.Bgra);
//提供给函数一个空间接收帧数据,将数据储存进colorBitmap的后台缓存中
this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight));
//设定colorBitmap需要更改的位图区域,此处设定为整个图片
this.colorBitmap.Unlock(); //解锁位图资源
}
}
}
private void DepthFrame_FrameArrived(object sender, DepthFrameArrivedEventArgs e)
{
using (DepthFrame frame = e.FrameReference.AcquireFrame())
{
if (frame != null)
{
this.depthBitmap.Lock();
using (KinectBuffer _buffer = frame.LockImageBuffer())
{
Cvt_process(_buffer.UnderlyingBuffer, _buffer.Size, frame.DepthMinReliableDistance, ushort.MaxValue);
this.depthBitmap.WritePixels(
new Int32Rect(0, 0, this.depthBitmap.PixelWidth, this.depthBitmap.PixelHeight),
this.depthPixels,
this.depthBitmap.PixelWidth,
0);
}
this.depthBitmap.Unlock();
}
}
}
private unsafe void Cvt_process(IntPtr depthFrameDate, uint depthFrameDateSize, ushort minDepth, ushort maxDepth)
{
ushort* frameDate = (ushort*)depthFrameDate;//强制深度帧数据转为ushort型数组,frameDate指针指向它
for (int i = 0; i < (int)(depthFrameDateSize / this.depthFrameDescription.BytesPerPixel); ++i)
{
//深度帧各深度(像素)点逐个转化为灰度值
ushort depth = frameDate[i];
this.depthPixels[i] = (byte)((depth >= minDepth) && (depth <= maxDepth) ? (depth / MapDepthToByte) : 0);
}
}
public ImageSource ColorSource
{
get
{
return this.colorBitmap;
}
}
public ImageSource DepthSource
{
get
{
return this.depthBitmap;
}
}
}
}
Kinect v2 + WPF获取RGB与Depth图像的更多相关文章
- 【翻译】Kinect v2程序设计(C++) Depth编
Kinect SDK v2预览版,取得Depth数据的方法说明. 上一节,介绍了通过使用Kinect for Windows SDK v2预览版(以下简称为,Kinect SDK v2预览版)从Kin ...
- 【计算机视觉】深度相机(五)--Kinect v2.0
原文:http://blog.csdn.NET/qq1175421841/article/details/50412994 ----微软Build2012大会:Kinect for Windows P ...
- 【翻译】Kinect v1和Kinect v2的彻底比较
本连载主要是比较Kinect for Windows的现行版(v1)和次世代型的开发者预览版(v2),以C++开发者为背景介绍进化的硬件和软件.本文主要是对传感的配置和运行条件进行彻底的比较. ...
- vc/mfc获取rgb图像数据后动态显示及保存图片的方法
vc/mfc获取rgb图像数据后动态显示及保存图片的方法 该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw3 ...
- 【翻译】Kinect v2程序设计(C++) Body 篇
Kinect SDK v2预览版的主要功能的使用介绍,基本上完成了.这次,是关于取得Body(人体姿势)方法的说明. 上一节,是使用Kinect SDK v2预览版从Kinect v2预览版取得B ...
- 【翻译】Kinect v2程序设计(C++) Color篇
Kinect SDK v2预览版,获取数据的基本流程的说明.以及取得Color图像的示例程序的介绍. 上一节,是关于当前型号Kinect for Windows(后面称作Kinect v1)和次世代型 ...
- ROS indigo下Kinect v2的驱动安装与调试
ROS indigo下Kinect v2的驱动安装与调试 一.libfreenect2源码安装与测试 github地址:https://github.com/OpenKinect/libfreenec ...
- 【翻译】Kinect v2程序设计(C++-) AudioBeam篇
Kinect v2,Microphone Array可以用来对于水平面音源方向的推测(AudioBeam)和语音识别(Speech Recognition).这一节是介绍如何取得AudioBeam. ...
- 【翻译】Kinect v2程序设计(C++) BodyIndex篇
通过Kinect SDK v2预览版,取得BodyIndex(人体区域)的方法和示例代码. 上一节,介绍了从Kinect v2预览版用Kinect SDK v2预览版获取Depth数据的方法. 这 ...
随机推荐
- VMware_win10能ping通虚拟机ip,虚拟机ping不通win10ip的解决方法
一.虚拟机设置为桥接模式 二.修改虚拟机linux的ip 查看win10的ip和网关 使用ifconfig查看网卡名,并在 /etc/sysconfig/network-scripts/目录修改对应的 ...
- Linux上天之路(十三)之系统进程管理
主要内容 进程介绍 进程管理 进程优先级 1. 进程介绍 Linux系统中的几乎任何行动都会以进程的形式进行.如果你用网络浏览器查看网页,浏览器就作为进程运行.如果键入bash shell的命令行,这 ...
- 腾讯 TKE 厉害了!用 eBPF绕过 conntrack 优化K8s Service,性能提升40%
Kubernetes Service[1] 用于实现集群中业务之间的互相调用和负载均衡,目前社区的实现主要有userspace,iptables和IPVS三种模式.IPVS模式的性能最好,但依然有优化 ...
- Vulnhub系列:Os-hackNos
0x01环境搭建 靶机链接: https://www.vulnhub.com/entry/hacknos-os-hacknos,401/发布日期: 2019.11.27靶机描述: 描述 难度:容易中级 ...
- LabVIEW生成.NET的DLL——C#下调用NI数据采集设备功能的一种方法 [原创www.cnblogs.com/helesheng]
LabVIEW是NI公司的数据采集设备的标准平台,在其上调用NI-DAQmx驱动和接口函数能够高效的开发数据采集和控制程序.但作为一种图形化的开发语言,使用LabVIEW开发涉及算法和流程控制的大型应 ...
- 日K蜡烛图
股票价格涨跌趋势,常用蜡烛图技术中的K线图来表示,分为按日的日K线.按周的周K线.按月的月K线等.以日K线为例,每天股票价格从开盘到收盘走完一天,对应一根蜡烛小图,要表示四个价格:开盘价格Open(早 ...
- 【C primer plus】初始化链表函数的错误
C primer plus第六版 的一处错误 第五百页17.3.4 实现接口的程序清单17.5中的初始化链表函数有误 #源代码 void InitializeList(List * plist) { ...
- Android函数抽取壳的实现
0x0 前言 函数抽取壳这个词不知道从哪起源的,但我理解的函数抽取壳是那种将dex文件中的函数代码给nop,然后在运行时再把字节码给填回dex的这么一种壳. 函数抽取前: 函数抽取后: 很早之前就想写 ...
- Linux中的一些基本命令
文章目录 ls cd Linux的目录 文件的权限 1.用户,组,权限 2.文件的权限 文件的基本操作 增:创建文件 删:删除文件 改:修改文件 查:查看 vi/vim 是一个编辑工具,主要用来编辑文 ...
- java日志打印使用指南
一.简介 日志打印是java代码开发中不可缺少的重要一步. 日志可以排查问题,可以搜集数据 二.常用日志框架 比较常用的日志框架就是logback, 一些老项目会使用log4j,他们用的都是slf4j ...