未曝光的例外:未找到有关此像素格式的信息 [英] Exception unhandeled:No information was found about this pixel format

查看:97
本文介绍了未曝光的例外:未找到有关此像素格式的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

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)
{
}

}
}



解决方案

你好weeshoussam,


根据你的描述,你有这个问题在  int stride = width * format.BitsPerPixel / 8 ;但我没有从MSDN文章中获得PixelFormat.BitsPerPixel,你可以看看:


https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.imaging.pixelformat?redirectedfrom = MSDN& view = netframework-4.7.2


获取此PixelFormat的每像素位数(bpp),您可以使用以下代码:

 //创建一个PixelFormat对象。 
PixelFormat myPixelFormat = new PixelFormat();

//将此PixelFormat设为Gray32Float像素格式。
myPixelFormat = PixelFormats.Gray32Float;

//获取此格式的每像素位数。因为
//格式为"Gray32Float",返回的浮点值将为32.
int bpp = myPixelFormat.BitsPerPixel;


 

关于PixelFormat.BitsPerPixel,请查看以下msdn文章:


https://docs.microsoft.com/en-us/dotnet/api/system.windows .media.pixelformat.bitsperpixel?redirectedfrom = MSDN& view = netframework-4.7.2#System_Windows_Media_PixelFormat_BitsPerPixel



最好的问候,


Cherry


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) { } } }


解决方案

Hi weeshoussam,

According to your description, you have this issue at int stride = width * format.BitsPerPixel / 8; but I don't get PixelFormat.BitsPerPixel from MSDN article, you can take a look:

https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.imaging.pixelformat?redirectedfrom=MSDN&view=netframework-4.7.2

And getting the number of bits-per-pixel (bpp) for this PixelFormat, you can use the following code:

// Create a PixelFormat object.
    PixelFormat myPixelFormat = new PixelFormat();
    
    // Make this PixelFormat a Gray32Float pixel format.
    myPixelFormat = PixelFormats.Gray32Float;

    // Get the number of bits-per-pixel for this format. Because
    // the format is "Gray32Float", the float value returned will be 32.
    int bpp = myPixelFormat.BitsPerPixel;

 

About PixelFormat.BitsPerPixel, please take a look the following msdn article:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.media.pixelformat.bitsperpixel?redirectedfrom=MSDN&view=netframework-4.7.2#System_Windows_Media_PixelFormat_BitsPerPixel

Best Regards,

Cherry


这篇关于未曝光的例外:未找到有关此像素格式的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆