1

だから私は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 つしかないので、例外の理由がわかりません。何か助けはありますか?

4

2 に答える 2

0

基本的に、メイン スレッドではないスレッドから UI を更新しようとしていますが、これは何らかの説明を呼び出​​さないと機能しません。

このスレッドの回答を見てください。
別のスレッドが所有しているため、呼び出しスレッドはこのオブジェクトにアクセスできません。画像を編集するにはどうすればよいですか?

これらの回答に従って、videoDisplay() のコードを次のように変更する必要があります。

...    

Action action = delegate { kinectVideo.Source = colorImageBitmap; };
kinectVideo.Dispatcher.Invoke(action);

if (colorImageBitmap == null){
...

それでもうまくいかない場合は、次のことを試してください。

...    

colorImageBitmap.Freeze();
Action action = delegate { kinectVideo.Source = colorImageBitmap; };
kinectVideo.Dispatcher.Invoke(action);

if (colorImageBitmap == null){
...

また、kinectVideo.Source = colorImageBitmap; という行も使用することを検討してください。Window_Loaded メソッドでも .Freeze() を使用する必要がある場合があります。

XAML と Freeze() にはあまり詳しくありませんが、colorImageBitmap はこれらのメソッドのいずれかの内部で初期化されていないように見えるため、他の問題が発生する可能性があります。GetAsFrozen()
を使用する方が良いオプションかもしれませんが、メモリに影響する可能性があります。

于 2013-08-16T14:34:54.450 に答える