所以我正在尝试使用 SDK 对 kinect 传感器进行编程。这是代码。
public partial class MainWindow : Window
{
bool mirror=false;
bool displayActive = true;
int redOffset;
int greenOffset;
int blueOffset;
WriteableBitmap colorImageBitmap = null;
KinectSensor myKinect;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
kinectVideo.Source = colorImageBitmap;
myKinect = KinectSensor.KinectSensors[0];
if (KinectSensor.KinectSensors.Count == 0)
{
MessageBox.Show("No Kinects detected", "Camera Viewer");
Application.Current.Shutdown();
}
myKinect.ColorStream.Enable();
myKinect.Start();
Thread updateVideoThread;
updateVideoThread = new Thread(new ThreadStart(videoDisplay));
updateVideoThread.Start();
}
void videoDisplay()
{
while (displayActive)
{
using (ColorImageFrame colorFrame = myKinect.ColorStream.OpenNextFrame(10))
{
if (colorFrame == null) continue;
byte[] colorData = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(colorData);
//----------------------------Methodos 2 for image color adjustment------------------------------------------------------------------------------------
updateColor(colorData);
//----------------------------Methodos 1 for mirror------------------------------------------------------------------------------------
if (mirror) { reflectImage(colorData, colorFrame.Width, colorFrame.Height); }
//----------------------------Methodos 2 for update image------------------------------------------------------------------------------------
kinectVideo.Source = colorImageBitmap;
if (colorImageBitmap == null)
{
this.colorImageBitmap = new WriteableBitmap(colorFrame.Width,
colorFrame.Height,
96, // DpiX
96, // DpiY
PixelFormats.Bgr32,
null);
}
this.colorImageBitmap.WritePixels(new Int32Rect(0, 0, colorFrame.Width, colorFrame.Height),
colorData, // video data
colorFrame.Width * colorFrame.BytesPerPixel, // stride,
0 // offset into the array - start at 0
);
}
}
}
并在“kinectVideo.Source = colorImageBitmap;”行中 is 给了我一个例外,上面写着“调用线程无法访问此对象,因为不同的线程拥有它。”。我不明白为什么。我是使用 C# 和 Visual Studio 编程的新手。我只有一个线程,所以我不知道为什么会出现异常。有什么帮助吗?