だから私は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;」の行で これは、「別のスレッドが所有しているため、呼び出し元のスレッドはこのオブジェクトにアクセスできません」という例外を与えます。理由がわかりません。C# と Visual Studio でのプログラミングは初めてです。スレッドが 1 つしかないので、例外の理由がわかりません。何か助けはありますか?