Kinectの本体を上下させる

camera.ElevationAngleプロパティをいじる。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Media;
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.Research.Kinect.Nui;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        // kinect sensor
        Runtime nui;

        // for moving
        Camera camera;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            nui = new Runtime();

            try
            {
                // init runtime
                nui.Initialize(RuntimeOptions.UseColor);

                // set camera
                camera = nui.NuiCamera;
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Error");
                return;
            }

        }

        private void Window_Closed(object sender, EventArgs e)
        {
            nui.Uninitialize();
        }

        private void upButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //カメラの角度を増加
                camera.ElevationAngle += 5;
            }
            catch (InvalidOperationException) {
                return;
            }
        }

        private void downButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //カメラの角度を減少
                camera.ElevationAngle -= 5;
            }
            catch (InvalidOperationException)
            {
                return;
            }
        }

    }
}

OpenCV 2.3.1でKinectを使ってみた

インストール

CMakeするときに、WITH_OPENNIを有効にする必要があります。

手前にある物体のみ表示するサンプル

// opencvkinecttest.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"

#include <iostream>

#include <opencv2/opencv.hpp>

int _tmain(int argc, _TCHAR* argv[])
{
  try {
    char* name = "OpenCV with OpenNI Test";
    cv::namedWindow( name );
    cv::VideoCapture     capture( CV_CAP_OPENNI );
    std::vector<cv::Mat> planes; //ビデオ画像を各色に分解した色情報
    while ( 1 ) { 
      // データの更新を待つ
      capture.grab(); 

      // ビデオ画像を取得
      cv::Mat  bgrImage;
      capture.retrieve( bgrImage, CV_CAP_OPENNI_BGR_IMAGE ); 
      cv::split(bgrImage, planes);
      
      // 深度を取得
      cv::Mat depthImage;
      capture.retrieve( depthImage, CV_CAP_OPENNI_DEPTH_MAP ); 
      int rows = depthImage.rows;
      int cols = depthImage.cols;	  
      for (int y=0; y<rows; y++) {
        for (int x=0; x<cols; x++) {
	  // ビデオ画像のうち、遠い箇所は黒にする
          if (depthImage.at<ushort>(y,x) > 2200 ) {
            planes[0].at<uchar>(y,x) = 0;
            planes[1].at<uchar>(y,x) = 0;
            planes[2].at<uchar>(y,x) = 0;
          }
        }
      }
   
      // 変換したビデオ画像を表示
      cv::Mat changedBgrImage;
      cv::merge(planes, changedBgrImage);
      cv::imshow( name, changedBgrImage );

      if ( cv::waitKey( 10 ) >= 0 ) {
        break; 
      }
    }

    cv::destroyAllWindows();
  }
  catch ( ... ) {
    std::cout << "exception!!" << std::endl;
  }
}

OpenCV 2.3.1インストール

http://opencv.willowgarage.com/wiki/
のDownloadからwin用のファイル(OpenCV-2.3.1-win-superpack.exe)をダウンロード。

実行するとファイルが展開される。

c:\opencv

に配置した。

dllが入っているディレクトリにPATHを通す。
Visual Studio 2010を使っているので、以下をPATHに追加した。

 C:\opencv\build\x86\vc10\bin

Visual Studio 2010での使い方

コンソールアプリケーションでプロジェクト新規作成。
プロジェクトのプロパティを開き、以下を設定する。

構成プロパティ > VC++ディレクトリ > インクルードディレクト

 C:\opencv\build\include

構成プロパティ > VC++ディレクトリ > ライブラリディレクト

 C:\opencv\build\x86\vc10\lib

構成プロパティ > リンカ > 追加の依存ファイル

 opencv_core231d.lib
 opencv_highgui231d.lib


http://opencv.willowgarage.com/wiki/VisualC%2B%2B
Building your own projects using OpenCV 2.2 in Visual Studio
に書いてあるソースをコピーして、プロジェクト名.cppに貼り付ける。

// OpenCV_Helloworld.cpp : Defines the entry point for the console application.
// Created for build/install tutorial, Microsoft Visual Studio and OpenCV 2.2.0

#include "stdafx.h"

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])
{
        // Open the file.
        IplImage *img = cvLoadImage("photo.jpg");
        if (!img) {
                printf("Error: Couldn't open the image file.\n");
                return 1;
        }

        // Display the image.
        cvNamedWindow("Image:", CV_WINDOW_AUTOSIZE);
        cvShowImage("Image:", img);

        // Wait for the user to press a key in the GUI window.
        cvWaitKey(0);

        // Free the resources.
        cvDestroyWindow("Image:");
        cvReleaseImage(&img);
        
        return 0;
}

プロジェクトのディレクトリにphoto.jpgを用意しておく。
あとはビルドして実行する。

OpenNIインストール

OpenNI stable v1.3.2.1をインストールした.
SensorKinectをインストール後,Kinectを接続し,OpenNIのSamplesのNIViewerを実行したら
"Xiron OS failed to create a mutex"
というエラーが出た.

http://groups.google.com/group/openni-dev/browse_thread/thread/de92dcfb9fdd9133
によると,「OpenNI Unstable 1.3.2.3」では解決されているらしい.

stable v1.3.2.1をアンインストールし,Unstable v1.3.2.3をインストールしたら解決した.

POJ 1068

Parencodings

一旦括弧を復元してから,W-sequenceを求めた.
(もっと効率いい方法がありそう…)

#include <iostream>
#include <string>
using namespace std;

int main() {
	int n,m;
	cin >> n;
	for (; n>0; n--) {
		cin >> m;
		string parens = "";
		//P-sequence
		int p0=0, p1;
		for (int i=1; i<=m; i++) {
			cin >> p1;
			for (int j=0; j<p1-p0; j++) parens.append("(");
			parens.append(")");
			p0 = p1;
		}
		//cout << parens << endl;
		//W-sequence
		for (int i=0; i<parens.size(); i++) {
			if (parens[i] == ')') {
				int lparen=0, rparen=1;
				for (int j=i-1; j>=0 && lparen < rparen; j--) {
					if (parens[j] == '(') lparen++;
					if (parens[j] == ')') rparen++;
				}
				cout << rparen << " ";
			}
		}
		cout << endl;

	}
	return 0;
}