问题描述
Hello亲爱的程序员,
我正在试图通过这个代码来连接我的kinect。它自己的代码不会消除任何错误,
但是当我连接kinect时它会给我"unhandeled exception"。在行
(int stride = width * format.BitsPerPixel / 8;)
我搜索了很多,但我不明白如何解决它。能帮助我吗。
谢谢,
Houssam
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;使用System.Windows
;使用System.Windows.Controls
;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;使用System.Windows.Navigation
;
使用System.Windows.Shapes;
使用Microsoft.Kinect;
使用System.IO;
名称空间WpfApp5
{
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window
{
private Array pixels;
public MainWindow()
{
InitializeComponent();
KinectSensor _sensor;
MultiSourceFrameReader _reader;
_sensor = KinectSensor.GetDefault();
if(_sensor!= null)
{
_sensor.Open();
}
_reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color |
FrameSourceTypes.Depth |
FrameSourceTypes.Infrared);
_reader.MultiSourceFrameArrived + = Reader_MultiSourceFrameArrived;
void Reader_MultiSourceFrameArrived(object sender,MultiSourceFrameArrivedEventArgs e)
{
//获取对多帧
var reference = e.FrameReference.AcquireFrame()的引用;
//使用(var frame = reference.ColorFrameReference.AcquireFrame())打开颜色框
{
if(frame!= null)
{
//用框架做一些事情......
camera.Source = ToBitmap(frame);
}
}
//使用开放深度框架
(var frame2 = reference.DepthFrameReference.AcquireFrame())
{
if(frame2!= null)
{
//对框架执行某些操作...
camera.Source = ToBitmap2(frame2);
}
}
//使用(var frame1 = reference.InfraredFrameReference.AcquireFrame())打开红外帧
{
if (frame1!= null)
{
//用框架做一些事情...
camera.Source = ToBitmap3(frame1);
}
}
}
ImageSource ToBitmap(ColorFrame帧)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
byte [] pixels = new byte [width * height *((PixelFormats.Bgr32.BitsPerPixel + 7)/ 8)];
if(frame.RawColorImageFormat == ColorImageFormat.Bgra)
{
frame.CopyRawFrameDataToArray(pixels);
}
else
{
frame.CopyConvertedFrameDataToArray(pixels,ColorImageFormat.Bgra);
}
PixelFormat format = new PixelFormat();
int stride = width * format.BitsPerPixel / 8;
返回BitmapSource.Create(宽度,高度,96,96,格式,null,像素,步幅);
}
ImageSource ToBitmap2(DepthFrame帧)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
ushort minDepth = frame.DepthMinReliableDistance;
ushort maxDepth = frame.DepthMaxReliableDistance;
ushort [] depthData = new ushort [width * height];
byte [] pixelData = new byte [width * height *(PixelFormats.Bgr32.BitsPerPixel + 7)/ 8];
frame.CopyFrameDataToArray(depthData);
int colorIndex = 0;
for(int depthIndex = 0; depthIndex< depthData.Length; ++ depthIndex)
{
ushort depth = depthData [depthIndex];
字节强度=(字节)(深度> = minDepth&& depth< = maxDepth?depth:0);
pixelData [colorIndex ++] = intensity; //蓝色
pixelData [colorIndex ++] =强度; //绿色
pixelData [colorIndex ++] =强度; //红色
++ colorIndex;
}
PixelFormat format = new PixelFormat();
int stride = width * format.BitsPerPixel / 8;
返回BitmapSource.Create(width,height,96,96,format,null,pixelData,stride);
}
}
private ImageSource ToBitmap3(InfraredFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
ushort [] infraredData = new ushort [width * height];
byte [] pixelData = new byte [width * height *(PixelFormats.Bgr32.BitsPerPixel + 7)/ 8];
frame.CopyFrameDataToArray(infraredData);
int colorIndex = 0;
for(int infraredIndex = 0; infraredIndex< infraredData.Length; ++ infraredIndex)
{
ushort ir = infraredData [infraredIndex];
字节强度=(字节)(ir>> 8);
pixelData [colorIndex ++] = intensity; //蓝色
pixelData [colorIndex ++] =强度; //绿色
pixelData [colorIndex ++] =强度; //红色
++ colorIndex;
}
PixelFormat format = new PixelFormat();
int stride = width * format.BitsPerPixel / 8;
返回BitmapSource.Create(宽度,高度,96,96,格式,null,像素,步幅);
}
private void ColorButton_Click(对象发送者,RoutedEventArgs e)
{
}
private void DepthButton_Click(object sender,RoutedEventArgs e)
{
}
private void InfraredButton_Click(object sender,RoutedEventArgs e)
{
}
}
}
Hello dear Programmers,
i am trzing to connect my kinect through out this code belwo. The code by it self doesnot dive any errors,
but when I am connecting the kinect it gives me then "unhandeled exception" on the line
(int stride = width * format.BitsPerPixel / 8;)
I have searched a lot, but I didn't understand how to fix it. can you please help me.
Thanks,
Houssam
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; using System.IO; namespace WpfApp5 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private Array pixels; public MainWindow() { InitializeComponent(); KinectSensor _sensor; MultiSourceFrameReader _reader; _sensor = KinectSensor.GetDefault(); if (_sensor != null) { _sensor.Open(); } _reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared); _reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived; void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e) { // Get a reference to the multi-frame var reference = e.FrameReference.AcquireFrame(); // Open color frame using (var frame = reference.ColorFrameReference.AcquireFrame()) { if (frame != null) { // Do something with the frame... camera.Source = ToBitmap(frame); } } // Open depth frame using (var frame2 = reference.DepthFrameReference.AcquireFrame()) { if (frame2 != null) { // Do something with the frame... camera.Source = ToBitmap2(frame2); } } // Open infrared frame using (var frame1 = reference.InfraredFrameReference.AcquireFrame()) { if (frame1 != null) { // Do something with the frame... camera.Source = ToBitmap3(frame1); } } } ImageSource ToBitmap(ColorFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; byte[] pixels = new byte[width * height * ((PixelFormats.Bgr32.BitsPerPixel + 7) / 8)]; if (frame.RawColorImageFormat == ColorImageFormat.Bgra) { frame.CopyRawFrameDataToArray(pixels); } else { frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); } PixelFormat format = new PixelFormat(); int stride = width * format.BitsPerPixel / 8; return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride); } ImageSource ToBitmap2(DepthFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; ushort minDepth = frame.DepthMinReliableDistance; ushort maxDepth = frame.DepthMaxReliableDistance; ushort[] depthData = new ushort[width * height]; byte[] pixelData = new byte[width * height * (PixelFormats.Bgr32.BitsPerPixel + 7) / 8]; frame.CopyFrameDataToArray(depthData); int colorIndex = 0; for (int depthIndex = 0; depthIndex < depthData.Length; ++depthIndex) { ushort depth = depthData[depthIndex]; byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0); pixelData[colorIndex++] = intensity; // Blue pixelData[colorIndex++] = intensity; // Green pixelData[colorIndex++] = intensity; // Red ++colorIndex; } PixelFormat format = new PixelFormat(); int stride = width * format.BitsPerPixel / 8; return BitmapSource.Create(width, height, 96, 96, format, null, pixelData, stride); } } private ImageSource ToBitmap3(InfraredFrame frame) { int width = frame.FrameDescription.Width; int height = frame.FrameDescription.Height; ushort[] infraredData = new ushort[width * height]; byte[] pixelData = new byte[width * height * (PixelFormats.Bgr32.BitsPerPixel + 7) / 8]; frame.CopyFrameDataToArray(infraredData); int colorIndex = 0; for (int infraredIndex = 0; infraredIndex < infraredData.Length; ++infraredIndex) { ushort ir = infraredData[infraredIndex]; byte intensity = (byte)(ir >> 8); pixelData[colorIndex++] = intensity; // Blue pixelData[colorIndex++] = intensity; // Green pixelData[colorIndex++] = intensity; // Red ++colorIndex; } PixelFormat format = new PixelFormat(); int stride = width * format.BitsPerPixel / 8; return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride); } private void ColorButton_Click(object sender, RoutedEventArgs e) { } private void DepthButton_Click(object sender, RoutedEventArgs e) { } private void InfraredButton_Click(object sender, RoutedEventArgs e) { } } }
这篇关于未曝光的例外:未找到有关此像素格式的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!