ラベル ARToolKit の投稿を表示しています。 すべての投稿を表示
ラベル ARToolKit の投稿を表示しています。 すべての投稿を表示

2017/02/20

WPF PrismとNyARToolKitCSで複数のマーカー位置推定

先ほど、マーカーの位置推定プログラムを書きましたが、今度は複数のマーカーの位置推定を行うプログラムです。
http://kowaimononantenai.blogspot.jp/2017/02/wpf-prismnyartoolkit.html

ファイル構成は上のサイトと同じです。

初めにViewのXamlです。これは上のサイトと全く同じです。

    
        
        

次はViewModelです。
カメラ画像を表示するために、async/awaitで非同期処理を行っています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Commands;

using System.Threading;
using System.Windows.Media.Imaging;
using System.Numerics;  //.Net Framework 4.6以上

namespace NyARCaptureTest
{
    class MainWindowViewModel: BindableBase
    {
        NyARPositionEstimation NyAr;
        bool isTask;

        /// 
        /// Notifying Property Change
        /// 
        private WriteableBitmap bmp;
        public WriteableBitmap Bmp
        {
            get { return bmp; }
            set { SetProperty(ref bmp, value); }
        }

        /// 
        /// DelegateCommand
        /// 
        private DelegateCommand startCommand;
        public DelegateCommand StartCommand
        {
            get{ return startCommand ?? (startCommand = new DelegateCommand(Start));}
        }
        private async void Start()
        {
            NyAr.Start();

            isTask = true;
            await ShowImage();
        }

        private DelegateCommand stopCommand;
        public DelegateCommand StopCommand
        {
            get { return stopCommand ?? (stopCommand = new DelegateCommand(Stop)); }
        }
        private void Stop()
        {
            NyAr.Stop();
            isTask = false;
        }


        public MainWindowViewModel()
        {
            NyAr = new NyARPositionEstimation();
        }

        /// 
        /// Task
        /// 
        /// 
        private async Task ShowImage()
        {
            while (isTask)
            {
                if (NyAr.IsCaptureOk)
                {
                    Bmp = NyAr.GetImage();

                    Matrix4x4[] matrix;
                    bool[] isEstimateOk;
                    NyAr.GetMatrix(out matrix, out isEstimateOk);

                    if (isEstimateOk[0] == true)
                    {
                        Console.WriteLine(matrix[0]);
                    }
                }
                await Task.Delay(30);
            }
        }
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Media.Imaging;
using System.Threading;
using System.Numerics;  //.Net Framework 4.6以上

using jp.nyatla.nyartoolkit.cs;
using jp.nyatla.nyartoolkit.cs.core;
using jp.nyatla.nyartoolkit.cs.detector;
using NyARToolkitCSUtils.Capture;
using NyARToolkitCSUtils.Direct3d;
using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using OpenCvSharp.Extensions;

namespace NyARCaptureTest
{
    class NyARPositionEstimation : CaptureListener
    {
        private NyARToolkitCSUtils.Capture.CaptureDevice cap;
        private String[] arCodeFile = { "patt.hiro", "patt.kanji" };
        private const String arCameraFile = "camera_para.dat";
        private NyARDetectMarker ar;
        private DsRgbRaster raster;
        private Mat img;
        private Object lockObj = new object();
        private Matrix4x4[] Matrix;

        public bool IsCaptureOk { get; private set; }


        public NyARPositionEstimation()
        {
            IsCaptureOk = false;
            Matrix = new Matrix4x4[2];
            Matrix[0] = Matrix4x4.Identity;
            Matrix[1] = Matrix4x4.Identity;

            //ARの設定
            //AR用カメラパラメタファイルをロード
            NyARParam ap = NyARParam.loadFromARParamFile(File.OpenRead(arCameraFile), 320, 240);

            //AR用のパターンコードを読み出し
            NyARCode[] code = new NyARCode[2];
            code[0] = NyARCode.loadFromARPattFile(File.OpenRead(arCodeFile[0]), 16, 16);
            code[1] = NyARCode.loadFromARPattFile(File.OpenRead(arCodeFile[1]), 16, 16);

            //計算モードの設定
            //キャプチャを作る
            /**************************************************
            このコードは、0番目(一番初めに見つかったキャプチャデバイス)
            を使用するようにされています。
            複数のキャプチャデバイスを持つシステムの場合、うまく動作しないかもしれません。
            n番目のデバイスを使いたいときには、CaptureDevice cap=cl[0];←ここの0を変えてください。
            手動で選択させる方法は、SimpleLiteDirect3Dを参考にしてください。
            **************************************************/
            CaptureDeviceList cl = new CaptureDeviceList();
            NyARToolkitCSUtils.Capture.CaptureDevice cap = cl[0];
            cap.SetCaptureListener(this);
            cap.PrepareCapture(320, 240, 30);
            this.cap = cap;

            //ラスタを作る。
            this.raster = new DsRgbRaster(cap.video_width, cap.video_height);

            this.ar = new NyARDetectMarker(ap, code, new double[] { 80.0, 80.0 }, 2);
            this.ar.setContinueMode(false);
        }


        public void Start()
        {
            this.cap.StartCapture();
        }

        public void Stop()
        {
            this.cap.StopCapture();
        }


        public WriteableBitmap GetImage()
        {
            WriteableBitmap bmp;
            lock (lockObj)
            {
                bmp = img.ToWriteableBitmap();
            }
            return bmp;
        }
        
        public void GetMatrix(out Matrix4x4[] matrix, out bool[] isEstimateOk)
        {
            lock (lockObj)
            {
                matrix = new Matrix4x4[2];
                matrix[0] = GetDeepCopyMatrix4x4(Matrix[0]);
                matrix[1] = GetDeepCopyMatrix4x4(Matrix[1]);

                isEstimateOk = new bool[2];
                if (matrix[0].Equals(Matrix4x4.Identity))
                {
                    isEstimateOk[0] = false;
                }
                else
                {
                    isEstimateOk[0] = true;
                }

                if (matrix[1].Equals(Matrix4x4.Identity))
                {
                    isEstimateOk[1] = false;
                }
                else
                {
                    isEstimateOk[1] = true;
                }
            }
        }
        
        
        public void OnBuffer(NyARToolkitCSUtils.Capture.CaptureDevice i_sender, double i_sample_time, IntPtr i_buffer, int i_buffer_len)
        {
            int w = i_sender.video_width;
            int h = i_sender.video_height;
            int s = w * (i_sender.video_bit_count / 8);

            Bitmap b = new Bitmap(w, h, s, PixelFormat.Format32bppRgb, i_buffer);
            b.RotateFlip(RotateFlipType.RotateNoneFlipY);
            this.raster.setBuffer(i_buffer, i_buffer_len, i_sender.video_vertical_flip);

            lock (lockObj)
            {
                img = b.ToMat();
                int detectedMarkerNum = this.ar.detectMarkerLite(this.raster, 100);
                int markerId = 0;

                for (int marker = 0; marker < detectedMarkerNum; marker++)
                {
                    markerId = ar.getARCodeIndex(marker);

                    if (markerId == 0)
                    {
                        NyARDoubleMatrix44 result_mat = new NyARDoubleMatrix44();
                        this.ar.getTransmationMatrix(markerId, result_mat);
                        Matrix[0] = ConvertNyARDoubleMatrix44ToMatrix4x4(result_mat);
                        NyARSquare square = ar.getSquare(markerId);

                        var point = new OpenCvSharp.CPlusPlus.Point[4];
                        for (int i = 0; i < 4; i++)
                        {
                            point[i] = new OpenCvSharp.CPlusPlus.Point(square.sqvertex[i].x, square.sqvertex[i].y);
                        }
                        img.FillConvexPoly(point, new Scalar(0, 0, 255));
                    }

                    if (markerId == 1)
                    {
                        NyARDoubleMatrix44 result_mat = new NyARDoubleMatrix44();
                        this.ar.getTransmationMatrix(markerId, result_mat);
                        Matrix[1] = ConvertNyARDoubleMatrix44ToMatrix4x4(result_mat);
                        NyARSquare square = ar.getSquare(markerId);

                        var point = new OpenCvSharp.CPlusPlus.Point[4];
                        for (int i = 0; i < 4; i++)
                        {
                            point[i] = new OpenCvSharp.CPlusPlus.Point(square.sqvertex[i].x, square.sqvertex[i].y);
                        }
                        img.FillConvexPoly(point, new Scalar(255, 0, 0));
                    }
                }

                if (detectedMarkerNum == 1)
                {
                    if (markerId == 0)
                    {
                        Matrix[1] = Matrix4x4.Identity;
                    }
                    else if (markerId == 1)
                    {
                        Matrix[0] = Matrix4x4.Identity;
                    }
                }
                else if (detectedMarkerNum == 0)
                {
                    Matrix[0] = Matrix4x4.Identity;
                    Matrix[1] = Matrix4x4.Identity;
                }
                IsCaptureOk = true;
            }
        }

        private Matrix4x4 ConvertNyARDoubleMatrix44ToMatrix4x4(NyARDoubleMatrix44 m)
        {
            return new Matrix4x4((float)m.m00,
                                    (float)m.m01,
                                    (float)m.m02,
                                    (float)m.m03,
                                    (float)m.m10,
                                    (float)m.m11,
                                    (float)m.m12,
                                    (float)m.m13,
                                    (float)m.m20,
                                    (float)m.m21,
                                    (float)m.m22,
                                    (float)m.m23,
                                    (float)m.m30,
                                    (float)m.m31,
                                    (float)m.m32,
                                    (float)m.m33);
        }

        private Matrix4x4 GetDeepCopyMatrix4x4(Matrix4x4 m)
        {
            return new Matrix4x4(m.M11,
                                    m.M12,
                                    m.M13,
                                    m.M14,
                                    m.M21,
                                    m.M22,
                                    m.M23,
                                    m.M24,
                                    m.M31,
                                    m.M32,
                                    m.M33,
                                    m.M34,
                                    m.M41,
                                    m.M42,
                                    m.M43,
                                    m.M44);
        }
    }
}







WPF PrismとNyARToolKitでマーカー位置推定

マーカーの位置推定を行うプログラムです。
以下のことを行います。
①カメラ座標系からマーカー座標系への同時変換行列を取得(Consoleに出力)
②マーカーにグラフィックを描画して、カメラ画像を表示

 これでは、NyARToolKitCS, OpenCVSharp,Prism.MVVMを使用しています。


初めにXamlです。


    
        
        


次にViewModelです。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Commands;

using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Windows.Threading;
using System.Threading;
using System.Windows.Media.Imaging;

namespace NyARCaptureTest
{
    class MainWindowViewModel: BindableBase
    {
        NyARPositionEstimation NyAr;
        bool isTask;

        /// 
        /// Notifying Property Change
        /// 
        private WriteableBitmap bmp;
        public WriteableBitmap Bmp
        {
            get { return bmp; }
            set { SetProperty(ref bmp, value); }
        }

        /// 
        /// DelegateCommand
        /// 
        private DelegateCommand startCommand;
        public DelegateCommand StartCommand
        {
            get{ return startCommand ?? (startCommand = new DelegateCommand(Start));}
        }
        private async void Start()
        {
            NyAr.Start();

            isTask = true;
            await ShowImage();
        }

        private DelegateCommand stopCommand;
        public DelegateCommand StopCommand
        {
            get { return stopCommand ?? (stopCommand = new DelegateCommand(Stop)); }
        }
        private void Stop()
        {
            NyAr.Stop();
            isTask = false;
        }


        public MainWindowViewModel()
        {
            NyAr = new NyARPositionEstimation();
        }

        /// 
        /// Task
        /// 
        /// 
        private async Task ShowImage()
        {

            while (isTask)
            {
                if (NyAr.IsCaptureOK)
                {
                    Bmp = NyAr.GetImage();
                    Console.WriteLine(NyAr.Matrix);
                }

                await Task.Delay(30);
            }
        }
    }
}


最後にModelです。
マーカーの位置推定はNyARToolKitCSのサンプルを参考にNyARPositionEstimationクラスを作りました。
基本的に処理はこちらで行っています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Drawing.Imaging;
using jp.nyatla.nyartoolkit.cs;
using jp.nyatla.nyartoolkit.cs.core;
using jp.nyatla.nyartoolkit.cs.detector;
using NyARToolkitCSUtils.Capture;
using NyARToolkitCSUtils.Direct3d;
using System.IO;

using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using OpenCvSharp.Extensions;

using System.Windows.Threading;
using System.Windows.Media.Imaging;

using System.Numerics;  //.Net Framework 4.6以上

namespace NyARCaptureTest
{
    class NyARPositionEstimation : CaptureListener
    {
        private NyARToolkitCSUtils.Capture.CaptureDevice m_cap;
        private const String AR_CODE_FILE = "patt.hiro";
        private const String AR_CAMERA_FILE = "camera_para.dat";
        private NyARSingleDetectMarker m_ar;
        private DsRgbRaster m_raster;

        private Mat Img;
        public Matrix4x4 Matrix { get; private set; }

        public bool IsCaptureOK { get; private set; }


        public NyARPositionEstimation()
        {
            IsCaptureOK = false;
            //ARの設定
            //AR用カメラパラメタファイルをロード
            NyARParam ap = NyARParam.loadFromARParamFile(File.OpenRead(AR_CAMERA_FILE), 320, 240);

            //AR用のパターンコードを読み出し 
            NyARCode code = NyARCode.loadFromARPattFile(File.OpenRead(AR_CODE_FILE), 16, 16);

            //NyARDoubleMatrix44 result_mat = new NyARDoubleMatrix44();
            //計算モードの設定
            //キャプチャを作る
            /**************************************************
            このコードは、0番目(一番初めに見つかったキャプチャデバイス)
            を使用するようにされています。
            複数のキャプチャデバイスを持つシステムの場合、うまく動作しないかもしれません。
            n番目のデバイスを使いたいときには、CaptureDevice cap=cl[0];←ここの0を変えてください。
            手動で選択させる方法は、SimpleLiteDirect3Dを参考にしてください。
            **************************************************/
            CaptureDeviceList cl = new CaptureDeviceList();
            NyARToolkitCSUtils.Capture.CaptureDevice cap = cl[0];
            cap.SetCaptureListener(this);
            cap.PrepareCapture(320, 240, 30);
            this.m_cap = cap;
            //ラスタを作る。
            this.m_raster = new DsRgbRaster(cap.video_width, cap.video_height);
            //1パターンのみを追跡するクラスを作成
            this.m_ar = NyARSingleDetectMarker.createInstance(ap, code, 80.0);
            this.m_ar.setContinueMode(false);
        }


        public void Start()
        {
            this.m_cap.StartCapture();
        }

        public void Stop()
        {
            this.m_cap.StopCapture();
        }


        public WriteableBitmap GetImage()
        {
            return Img.ToWriteableBitmap();
        }


        public void OnBuffer(NyARToolkitCSUtils.Capture.CaptureDevice i_sender, double i_sample_time, IntPtr i_buffer, int i_buffer_len)
        {
            int w = i_sender.video_width;
            int h = i_sender.video_height;
            int s = w * (i_sender.video_bit_count / 8);

            Bitmap b = new Bitmap(w, h, s, PixelFormat.Format32bppRgb, i_buffer);

            // If the image is upsidedown
            b.RotateFlip(RotateFlipType.RotateNoneFlipY);
            Img = b.ToMat();

            //ARの計算
            this.m_raster.setBuffer(i_buffer, i_buffer_len, i_sender.video_vertical_flip);
            if (this.m_ar.detectMarkerLite(this.m_raster, 100))
            {
                NyARDoubleMatrix44 result_mat = new NyARDoubleMatrix44();
                this.m_ar.getTransmationMatrix(result_mat);
                Matrix = ConvertNyARDoubleMatrix44ToMatrix4x4(result_mat);

                //Console.WriteLine("Maker is found.");

                NyARSquare square = m_ar.refSquare();

                var point = new OpenCvSharp.CPlusPlus.Point[4];
                for (int i = 0; i < 4; i++)
                {
                    point[i] = new OpenCvSharp.CPlusPlus.Point(square.sqvertex[i].x, square.sqvertex[i].y);
                }
                Img.FillConvexPoly(point, new Scalar(0, 0, 255));
            }
            else
            {
                //Console.WriteLine("Maker is NOT found.");
            }

            IsCaptureOK = true;
        }

        private Matrix4x4 ConvertNyARDoubleMatrix44ToMatrix4x4(NyARDoubleMatrix44 m)
        {
            return new Matrix4x4(   (float)m.m00,
                                    (float)m.m01,
                                    (float)m.m02,
                                    (float)m.m03,
                                    (float)m.m10,
                                    (float)m.m11,
                                    (float)m.m12,
                                    (float)m.m13,
                                    (float)m.m20,
                                    (float)m.m21,
                                    (float)m.m22,
                                    (float)m.m23,
                                    (float)m.m30,
                                    (float)m.m31,
                                    (float)m.m32,
                                    (float)m.m33);
        }
    }
}



2016/11/05

WPF(Livet)とNyARToolKitでマーカー検出

マーカーの位置検出を行うために、マーカーの検出をNyARToolKitで行うサンプルプログラムを作成しました。
次のようなことをやります。
  1. Livetを使用したWPFアプリケーション
  2. NyARToolKitでカメラキャプチャとマーカー検出
  3. OpenCVSharpでマーカー位置に矩形を描画
初めにXAML。Grid内だけ変更しています。ボタンを押すとマーカー検出がスタートします。
    
        


次にVeiwModel。
NyARToolKitはその実装の関係上「onBuffer」という関数が呼ばれるので、その中で処理をしています。
NyARToolKitのサンプル等ではBitmapで画像を処理しているので、
それをMatに変換してOpenCVSharpで矩形を描画しています。

そのあとに、MatをWriteableBitmapに直して、XAMLでBindingできるようにします。
ここで、onBufferという別タスクでUIで利用するWriteableBitmapを変更できないので、
Dispatchar.BeginInvokeを使用して、UI関係にアクセスできるようにしています。

using System.Windows;
using System.Windows.Forms;
using System.Windows.Threading;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using jp.nyatla.nyartoolkit.cs;
using jp.nyatla.nyartoolkit.cs.core;
using jp.nyatla.nyartoolkit.cs.detector;
using NyARToolkitCSUtils.Capture;
using NyARToolkitCSUtils.Direct3d;

using OpenCvSharp;
using OpenCvSharp.CPlusPlus;
using OpenCvSharp.Extensions;

namespace ar_SimpleLite.ViewModels
{
    public class MainWindowViewModel : ViewModel, CaptureListener
    {
        private NyARToolkitCSUtils.Capture.CaptureDevice m_cap;
        private const String AR_CODE_FILE = "patt.hiro";
        private const String AR_CAMERA_FILE = "camera_para.dat";
        private NyARSingleDetectMarker m_ar;
        private DsRgbRaster m_raster;

        private Dispatcher dispatcher;

        public void Initialize()
        {
            //ARの設定
            //AR用カメラパラメタファイルをロード
            NyARParam ap = NyARParam.createFromARParamFile(new StreamReader(AR_CAMERA_FILE));
            ap.changeScreenSize(320, 240);

            //AR用のパターンコードを読み出し 
            NyARCode code = NyARCode.createFromARPattFile(new StreamReader(AR_CODE_FILE), 16, 16);

            NyARDoubleMatrix44 result_mat = new NyARDoubleMatrix44();

            //計算モードの設定
            //キャプチャを作る
            /**************************************************
            このコードは、0番目(一番初めに見つかったキャプチャデバイス)
            を使用するようにされています。
            複数のキャプチャデバイスを持つシステムの場合、うまく動作しないかもしれません。
            n番目のデバイスを使いたいときには、CaptureDevice cap=cl[0];←ここの0を変えてください。
            手動で選択させる方法は、SimpleLiteDirect3Dを参考にしてください。
            **************************************************/
            CaptureDeviceList cl = new CaptureDeviceList();
            NyARToolkitCSUtils.Capture.CaptureDevice cap = cl[0];

            cap.SetCaptureListener(this);

            cap.PrepareCapture(320, 240, 30);
            this.m_cap = cap;
            //ラスタを作る。
            this.m_raster = new DsRgbRaster(cap.video_width, cap.video_height, NyARBufferType.OBJECT_CS_Bitmap);
            //1パターンのみを追跡するクラスを作成
            this.m_ar = NyARSingleDetectMarker.createInstance(ap, code, 80.0);
            this.m_ar.setContinueMode(false);

            dispatcher = Dispatcher.CurrentDispatcher;
        }


        #region WBmp変更通知プロパティ
        private WriteableBitmap _WBmp;

        public WriteableBitmap WBmp
        {
            get
            { return _WBmp; }
            set
            { 
                if (_WBmp == value)
                    return;
                _WBmp = value;
                RaisePropertyChanged();
            }
        }
        #endregion


        #region ButtonCommand
        private ViewModelCommand _ButtonCommand;

        public ViewModelCommand ButtonCommand
        {
            get
            {
                if (_ButtonCommand == null)
                {
                    _ButtonCommand = new ViewModelCommand(Button);
                }
                return _ButtonCommand;
            }
        }

        public void Button()
        {
            this.m_cap.StartCapture();
        }
        #endregion


        public void OnBuffer(NyARToolkitCSUtils.Capture.CaptureDevice i_sender, double i_sample_time, IntPtr i_buffer, int i_buffer_len)
        {            
            int w = i_sender.video_width;
            int h = i_sender.video_height;
            int s = w * (i_sender.video_bit_count / 8);

            Bitmap b = new Bitmap(w, h, s, System.Drawing.Imaging.PixelFormat.Format32bppRgb, i_buffer);

            // If the image is upsidedown
            b.RotateFlip(RotateFlipType.RotateNoneFlipY);
            Mat img = b.ToMat();

            //ARの計算
            this.m_raster.setBuffer(i_buffer, i_buffer_len, i_sender.video_vertical_flip);
            if (this.m_ar.detectMarkerLite(this.m_raster, 100))
            {
                NyARDoubleMatrix44 result_mat = new NyARDoubleMatrix44();
                this.m_ar.getTransmationMatrix(result_mat);
                Console.WriteLine("Maker is found.");

                NyARSquare square = m_ar.refSquare();

                var point = new OpenCvSharp.CPlusPlus.Point[4];
                point[0] = new OpenCvSharp.CPlusPlus.Point(square.sqvertex[0].x, square.sqvertex[0].y);
                point[1] = new OpenCvSharp.CPlusPlus.Point(square.sqvertex[1].x, square.sqvertex[1].y);
                point[2] = new OpenCvSharp.CPlusPlus.Point(square.sqvertex[2].x, square.sqvertex[2].y);
                point[3] = new OpenCvSharp.CPlusPlus.Point(square.sqvertex[3].x, square.sqvertex[3].y);

                img.FillConvexPoly(point, new Scalar(0, 0, 255));
            }
            else
            {
                Console.WriteLine("Maker is NOT found.");
            }

            dispatcher.BeginInvoke(new Action(delegate()
            {

                WBmp = img.ToWriteableBitmap();
            }));
        }

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);
    }
}


2016/11/03

NyARToolKitCSを使ってみる

カメラ画像からマーカーの位置検出を行いたいので、C#で利用できるARライブラリを探してみました。

いろいろあるみたいですが、ARToolKitのC#版であるNyARToolKitCSを利用することにしました。

HPは下です。
NyARToolkit project
http://nyatla.jp/nyartoolkit/wp/

ソースをGithubで公開しているみたいなので、ダウンロードします。
Masterを利用してもいいのですが、キチンとReleaseされたVerのものをダウンロードします。
Releases · nyatla/NyARToolkitCS · GitHub
https://github.com/nyatla/NyARToolkitCS/releases

現時点では、Ver5.0.8がLatest releaseみたいです。
ここではじめはVer5.0.8を使ってみたのですが、エラーが出てしまい、私の力ではビルドできませんでした。
ですので、ここではVer4.1.1の使い方を説明します。
なので、Ver4.1.1をダウンロードします。

ダウンロードしたフォルダの下記ファイルをVisual Studio で開きます。
今回はVisual Studio 2012を使用します。
NyARToolkitCS-4.1.1\NyARToolkitCS-4.1.1\forFW2.0\NyARToolkitCS.sln

次にサンプルプログラムを動かしてみたいと思います。
まずは「CaptureTest」を右クリックして、「スタートアッププロジェクトに追加」します。

そして、ソシューションをリビルドします。
そうすると、下のようにエラーが出てしまいます。。。


ダウンロードしたソースのReadmeをよく見ると。
NyARToolkitCS-4.1.1\readme.ja.txt
「sandboxは実験的なものを集めてあるので、コンパイルできないかもしれません。」とあります。

なので、
sandboxはビルドの対象から外しましょう。
NyARToolKitCS.sandboxを右クリックして、プロジェクトのアンロードをします。

これでおkなので、もう一度ソリューションのビルドをしましょう。
エラーは出なくなったはずです。

それではデバッグで実行しましょう。
これで動くかと思いきや、エラーが出てしまいました。


 これが出る直接的な原因はわかりませんが、
 これはソリューションのプラットフォームを「AnyCPU」から「x86」にするとおkです。

 よし!じゃあ、こんどこそデバッグで実行。
 でも、またまたエラーが出ます。


「camera_para.datが見つからない」っていってます。
これは、先ほどの修正でAnyCPUからx86にしたので、実行ファイルが生成されるフォルダが変わってしまったことが原因です。
AnyCPUであれば、ここに実行ファイルができますが、
NyARToolkitCS-4.1.1\forFW2.0\sample\CaptureTest\bin\Debug
x86はここにできます。フォルダが一階層深いんですね。
NyARToolkitCS-4.1.1\forFW2.0\sample\CaptureTest\bin\x86\Debug

なので、相対パスで指定しているdatファイルの位置がずれて、見つけられなくなっています。
なので、Form1.csの下記コードの部分に赤字を追加します。

        private const String AR_CODE_FILE = "../../../../../../data/patt.hiro";
        private const String AR_CAMERA_FILE = "../../../../../../data/camera_para.dat";

3度目の正直で、もう一度実行します。
今度はきちんと実行されましたね。




このプロジェクトはシンプルでいいんですが、マーカーが表示されたときにグラフィックが描画されません。

次は描画されるやつをビルドします。「SimpleLiteD3d」です。
先ほどと同じように、スタートアップに設定して、
SimpleLiteD3d.cs内のコードも同じように ../ を追加します。

で、ビルドすると成功すると思うので、実行してください。
はい。毎度のことエラーになりましたね(笑)


このエラーをググると下のサイトにあたるので、この対処を行います。
NKY-TECH. ソフトウェア開発部: .NET Framework 4.0で混合モードのアセンブリを読み込めない旨のエラー
http://nky-tech-sdt.blogspot.jp/2010/02/net-framework-40.html

app.configを設定するといいようです。
でも、プロジェクト内にapp.configが見当たりませんので、作る必要があるようです。

作成するときのいい方法が簡単には見つからなかったので、下のようにして作ります。
SimpleLiteD3dプロジェクトを右クリックで「プロパティ」。下のように、適当に値を追加します。
これ自体はどうでもいい変更ですが、変更することでapp.configが作成されるようです。
 


そうするとプロジェクトにapp.configができるので、下をapp.configの2行目と3行目の間に挿入します。

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>

そして実行です。

おkでしたね。

長かったですが、これでマーカー検出とグラフィックのサンプルを実行できました。