public RleDetector(NyARParam i_param, INyIdMarkerDataEncoder i_encoder, NyIdMarkerPickup i_id_pickup): base(i_param.getScreenSize())
 {
    
     this._coordline = new NyARCoord2Linear(i_param.getScreenSize(), i_param.getDistortionFactor());
     this._data_temp = i_encoder.createDataInstance();
     this._current_data = i_encoder.createDataInstance();
     this._encoder = i_encoder;
     this._id_pickup = i_id_pickup;
     return;
 }
示例#2
0
        public D3dManager(Form i_main_window, NyARParam i_nyparam, int i_profile_id)
        {
            PresentParameters pp = new PresentParameters();
            // ウインドウモードなら true、フルスクリーンモードなら false を指定
            pp.Windowed = true;
            // スワップとりあえずDiscardを指定。
            pp.SwapEffect = SwapEffect.Discard;
            pp.EnableAutoDepthStencil = true;
            pp.AutoDepthStencilFormat = DepthFormat.D16;
            pp.BackBufferCount = 0;
            pp.BackBufferFormat = Format.R5G6B5;
            this._d3d_device = new Device(0, DeviceType.Default, i_main_window.Handle, CreateFlags.None, pp);

            //ビューポートを指定
            float scale = setupView(i_nyparam,i_main_window.ClientSize);
            this._scale = scale;
            // ライトを無効
            this._d3d_device.RenderState.Lighting = false;

            //カメラ画像の転写矩形を作成
            Viewport vp=this._d3d_device.Viewport;
            this._view_rect = new Rectangle(vp.X, vp.Y, vp.Width, vp.Height);

            NyARIntSize cap_size = i_nyparam.getScreenSize();
            this._background_size = new Size(cap_size.w, cap_size.h);
            return;
        }
 public DetectSquareCB(NyARParam i_param, INyIdMarkerDataEncoder i_encoder)
 {
     this._coordline = new Coord2Linear(i_param.getScreenSize(), i_param.getDistortionFactor());
     this._data_temp = i_encoder.createDataInstance();
     this._current_data = i_encoder.createDataInstance();
     this._encoder = i_encoder;
     return;
 }
 public DetectSquareCB(INyARColorPatt i_inst_patt, NyARCode i_ref_code, NyARParam i_param)
 {
     this._inst_patt = i_inst_patt;
     this._deviation_data = new NyARMatchPattDeviationColorData(i_ref_code.getWidth(), i_ref_code.getHeight());
     this._coordline = new Coord2Linear(i_param.getScreenSize(), i_param.getDistortionFactor());
     this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code);
     return;
 }
      /// <summary>
      /// Initialize a new SquareDetectionListener.
      /// </summary>
      /// <param name="patternMatchers">The pattern matchers with the marker data.</param>
      /// <param name="cameraParameters">The camera calibration data.</param>
      /// <param name="colorPattern">The used color pattern.</param>
      /// <param name="patternMatchDeviationData">The pattern match deviation data.</param>
      public SquareDetectionListener(List<PatternMatcher> patternMatchers, NyARParam cameraParameters, INyARColorPatt colorPattern, NyARMatchPattDeviationColorData patternMatchDeviationData)
      {
         this.patternMatchers = patternMatchers;
         this.colorPattern = colorPattern;
         this.patternMatchDeviationData = patternMatchDeviationData;
         this.coordinationMapper = new Coord2Linear(cameraParameters.getScreenSize(), cameraParameters.getDistortionFactor());
         this.matrixCalculator = new NyARTransMat(cameraParameters);
         this.points = NyARIntPoint2d.createArray(4);
         this.evaluationResult = new NyARMatchPattResult();

         Reset();
      }
        /// <summary>
        /// Initializes the detector for single marker detection.
        /// </summary>
        /// <param name="width">The width of the buffer that will be used for detection.</param>
        /// <param name="height">The height of the buffer that will be used for detection.</param>
        /// <param name="nearPlane">The near view plane of the frustum.</param>
        /// <param name="farPlane">The far view plane of the frustum.</param>
        /// <param name="markers">A list of markers that should be detected.</param>
        /// <param name="bufferType">The type of the buffer.</param>
        /// <param name="adaptive">Performs an adaptive bitmap thresholding if set to true. Default = false.</param>
        protected void Initialize(int width, int height, double nearPlane, double farPlane, IList<Marker> markers, int bufferType, bool adaptive = false)
        {
            // Check arguments
            if (markers == null || !markers.Any())
            {
                throw new ArgumentNullException("markers");
            }

            // Member init
            this.bufferWidth = width;
            this.bufferHeight = height;
            this.isAdaptive = adaptive;

            // Init pattern matchers with markers and check segment size, whcih has to be equal for all markers
            int segmentX = markers[0].SegmentsX;
            int segmentY = markers[0].SegmentsY;
            var patternMatchers = new List<PatternMatcher>(markers.Count);
            foreach (var marker in markers)
            {
                if (marker.SegmentsX != segmentX || marker.SegmentsY != segmentY)
                {
                    throw new ArgumentException("The Segment size has to be equal for all markers. Don't mix 16x16 and 32x32 markers for example.", "markers");
                }
                patternMatchers.Add(new PatternMatcher(marker));
            }

            // Load deafult camera calibration data
            string location = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            StreamReader reader = new StreamReader(location + "/Content/Data/Camera_Calibration_1280x720.dat");
            var cameraParameters = new NyARParam();
            using (var cameraCalibrationDataStream = reader.BaseStream)
            {
                cameraParameters.loadARParam(cameraCalibrationDataStream);
                cameraParameters.changeScreenSize(width, height);
            }

            //var asmName = new System.Reflection.AssemblyName(System.Reflection.Assembly.GetExecutingAssembly().FullName).Name;

            //var uri = new Uri(asmName + ";component/Assets/data/Camera_Calibration_1280x720.dat", UriKind.Relative);
            //var streamResInfoCam = Application.GetResourceStream(uri);
            //if (null == streamResInfoCam)
            //    throw new FileNotFoundException("Application.GetResourceStream returned null", uri.OriginalString);

            //var cameraParameters = new NyARParam();
            //using (var cameraCalibrationDataStream = streamResInfoCam.Stream)
            //{
            //    cameraParameters.loadARParam(cameraCalibrationDataStream);
            //    cameraParameters.changeScreenSize(width, height);
            //}

            // Get projection matrix from camera calibration data
            this.Projection = cameraParameters.GetCameraFrustumRH(nearPlane, farPlane);

            // Init detector and necessary data
            var colorPattern = new NyARColorPatt_Perspective_O2(segmentX, segmentY, 4, 25);
            var patternMatchDeviationData = new NyARMatchPattDeviationColorData(segmentX, segmentY);
            this.squareDetector = new NyARSquareContourDetector_Rle(cameraParameters.getScreenSize());
            this.squareDetectionListener = new SquareDetectionListener(patternMatchers, cameraParameters, colorPattern, patternMatchDeviationData);

            // Init buffer members 
            this.filteredBuffer = new NyARBinRaster(width, height);
            if (adaptive)
            {
                this.bufferFilter = new NyARRasterFilter_AdaptiveThreshold(bufferType);
            }
            else
            {
                this.bufferFilter = new NyARRasterFilter_ARToolkitThreshold(this.Threshold, bufferType);
            }
        }
示例#7
0
 public static void toCameraFrustumRH(NyARParam i_arparam, double i_near, double i_far, ref Matrix o_d3d_projection)
 {
     toCameraFrustumRH(i_arparam.getPerspectiveProjectionMatrix(),i_arparam.getScreenSize(),1.0,i_near, i_far,ref o_d3d_projection);
 }
        protected void initInstance(NyARParam i_param, INyIdMarkerDataEncoder i_encoder, double i_marker_width, int i_raster_format)
        {
            //初期化済?
            Debug.Assert(this._initialized == false);

            NyARIntSize scr_size = i_param.getScreenSize();
            // 解析オブジェクトを作る
            this._square_detect = new NyARSquareContourDetector_Rle(scr_size);
            this._transmat = new NyARTransMat(i_param);
            this._callback = new DetectSquareCB(i_param, i_encoder);

            // 2値画像バッファを作る
            this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
            //ワーク用のデータオブジェクトを2個作る
            this._data_current = i_encoder.createDataInstance();
            this._tobin_filter = new NyARRasterFilter_ARToolkitThreshold(110, i_raster_format);
            this._threshold_detect = new NyARRasterThresholdAnalyzer_SlidePTile(15, i_raster_format, 4);
            this._initialized = true;
            this._is_active = false;
            this._offset = new NyARRectOffset();
            this._offset.setSquare(i_marker_width);
            return;

        }
示例#9
0
	    /**
	     * コンストラクタ。
	     * 樽型歪みが少ない、または補正済みの画像を入力するときには、{@link #NyARReality(NyARIntSize, double, double, NyARPerspectiveProjectionMatrix, NyARCameraDistortionFactor, int, int)}
	     * のi_dist_factorにnullを指定すると、より高速な動作が期待できます。
	     * @param i_param
	     * カメラパラメータを指定します。
	     * @param i_near
	     * 視錐体のnear-pointをmm単位で指定します。
	     * default値は{@link #FRASTRAM_ARTK_NEAR}です。
	     * @param i_far
	     * 視錐体のfar-pointをmm単位で指定します。
	     * default値は{@link #FRASTRAM_ARTK_FAR}です。
	     * @param i_max_known_target
	     * Knownターゲットの最大数を指定します。
	     * @param i_max_unknown_target
	     * UnKnownターゲットの最大数を指定します。
	     * @throws NyARException
	     */
	    public NyARReality(NyARParam i_param,double i_near,double i_far,int i_max_known_target,int i_max_unknown_target)
	    {
		    //定数とかいろいろ
		    this.MAX_LIMIT_KNOWN=i_max_known_target;
		    this.MAX_LIMIT_UNKNOWN=i_max_unknown_target;
		    this.initInstance(i_param.getScreenSize(),i_near,i_far,i_param.getPerspectiveProjectionMatrix(),i_param.getDistortionFactor());
		    return;
	    }
示例#10
0
        private Matrix GetProjectionMatrix(NyARParam i_arparam, float near, float far)
        {
            NyARMat trans_mat = new NyARMat(3, 4);
            NyARMat icpara_mat = new NyARMat(3, 4);
            double[,] p = new double[3, 3], q = new double[4, 4];
            int width, height;
            int i, j;

            NyARIntSize size = i_arparam.getScreenSize();
            width = size.w;
            height = size.h;

            i_arparam.getPerspectiveProjectionMatrix().decompMat(icpara_mat, trans_mat);

            double[][] icpara = icpara_mat.getArray();
            double[][] trans = trans_mat.getArray();

            for (i = 0; i < 3; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    p[i, j] = icpara[i][j] / icpara[2][2];
                }
            }

            q[0, 0] = (2.0 * p[0, 0] / (width));
            q[0, 1] = (2.0 * p[0, 1] / (width));
            q[0, 2] = ((2.0 * p[0, 2] / (width)) - 1.0);
            q[0, 3] = 0.0;

            q[1, 0] = 0.0;
            q[1, 1] = (2.0 * p[1, 1] / (height));
            q[1, 2] = ((2.0 * p[1, 2] / (height)) - 1.0);
            q[1, 3] = 0.0;

            q[2, 0] = 0.0;
            q[2, 1] = 0.0;
            q[2, 2] = (far + near) / (far - near);
            q[2, 3] = -2.0 * far * near / (far - near);

            q[3, 0] = 0.0;
            q[3, 1] = 0.0;
            q[3, 2] = 1.0;
            q[3, 3] = 0.0;

            Matrix mat = Matrix.Identity;
            mat.M11 = (float)(q[0, 0] * trans[0][0] + q[0, 1] * trans[1][0] + q[0, 2] * trans[2][0]);
            mat.M12 = (float)(q[1, 0] * trans[0][0] + q[1, 1] * trans[1][0] + q[1, 2] * trans[2][0]);
            mat.M13 = (float)(q[2, 0] * trans[0][0] + q[2, 1] * trans[1][0] + q[2, 2] * trans[2][0]);
            mat.M14 = (float)(q[3, 0] * trans[0][0] + q[3, 1] * trans[1][0] + q[3, 2] * trans[2][0]);
            mat.M21 = (float)(q[0, 1] * trans[0][1] + q[0, 1] * trans[1][1] + q[0, 2] * trans[2][1]);
            mat.M22 = (float)(q[1, 1] * trans[0][1] + q[1, 1] * trans[1][1] + q[1, 2] * trans[2][1]);
            mat.M23 = (float)(q[2, 1] * trans[0][1] + q[2, 1] * trans[1][1] + q[2, 2] * trans[2][1]);
            mat.M24 = (float)(q[3, 1] * trans[0][1] + q[3, 1] * trans[1][1] + q[3, 2] * trans[2][1]);
            mat.M31 = (float)(q[0, 2] * trans[0][2] + q[0, 1] * trans[1][2] + q[0, 2] * trans[2][2]);
            mat.M32 = (float)(q[1, 2] * trans[0][2] + q[1, 1] * trans[1][2] + q[1, 2] * trans[2][2]);
            mat.M33 = -(float)(q[2, 2] * trans[0][2] + q[2, 1] * trans[1][2] + q[2, 2] * trans[2][2]);
            mat.M34 = -(float)(q[3, 2] * trans[0][2] + q[3, 1] * trans[1][2] + q[3, 2] * trans[2][2]);
            mat.M41 = (float)(q[0, 3] * trans[0][3] + q[0, 1] * trans[1][3] + q[0, 2] * trans[2][3] + q[0, 3]);
            mat.M42 = (float)(q[1, 3] * trans[0][3] + q[1, 1] * trans[1][3] + q[1, 2] * trans[2][3] + q[1, 3]);
            mat.M43 = (float)(q[2, 3] * trans[0][3] + q[2, 1] * trans[1][3] + q[2, 2] * trans[2][3] + q[2, 3]);
            mat.M44 = (float)(q[3, 3] * trans[0][3] + q[3, 1] * trans[1][3] + q[3, 2] * trans[2][3] + q[3, 3]);

            return mat;
        }
        /**
         * この関数は、インスタンスを初期化します。
         * 継承先のクラスから呼び出してください。
         * @param i_param
         * カメラパラメータオブジェクト。このサイズは、{@link #detectMarker}に入力する画像と同じサイズである必要があります。
         * @
         */
        protected void initInstance(NyARParam i_param)
        {
            //初期化済?
            Debug.Assert(this._initialized == false);

            NyARIntSize scr_size = i_param.getScreenSize();
            // 解析オブジェクトを作る
            this._transmat = new NyARTransMat(i_param);
            this._thdetect = new NyARHistogramAnalyzer_SlidePTile(15);

            // 2値画像バッファを作る
            this._gs_raster = new NyARGrayscaleRaster(scr_size.w, scr_size.h);
            this._initialized = true;
            //コールバックハンドラ
            this._detectmarker = new DetectSquare(i_param);
            this._offset = new NyARRectOffset();
            return;
        }
        protected void initInstance(
            INyARColorPatt i_patt_inst,
            NyARSquareContourDetector i_sqdetect_inst,
            INyARTransMat i_transmat_inst,
            INyARRasterFilter_Rgb2Bin i_filter,
            NyARParam i_ref_param,
            NyARCode i_ref_code,
            double i_marker_width)
        {
            NyARIntSize scr_size = i_ref_param.getScreenSize();
            // 解析オブジェクトを作る
            this._square_detect = i_sqdetect_inst;
            this._transmat = i_transmat_inst;
            this._tobin_filter = i_filter;
            //2値画像バッファを作る
            this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
            //_detect_cb
            this._detect_cb = new DetectSquareCB(i_patt_inst, i_ref_code, i_ref_param);
            //オフセットを作成
            this._offset = new NyARRectOffset();
            this._offset.setSquare(i_marker_width);
            return;

        }
示例#13
0
        private float setupView(NyARParam i_nyparam, Size i_client_size)
        {
            NyARIntSize cap_size=i_nyparam.getScreenSize();
            float scale;
            int new_w, new_h;
            //縦にあわせてみる。
            scale = (float)i_client_size.Height / (float)cap_size.h;
            new_h = i_client_size.Height;
            new_w = (int)((float)cap_size.w * scale);
            //幅が収まってないなら、幅に合わせる。
            if (new_w > i_client_size.Width)
            {
                scale = (float)i_client_size.Width / (float)cap_size.w;
                new_w = i_client_size.Width;
                new_h = (int)(cap_size.h * scale);
            }
            //ビューポート作成
            Viewport vp = new Viewport();
            vp.X = (i_client_size.Width - new_w) / 2;
            vp.Y = (i_client_size.Height - new_h) / 2;
            vp.Height = new_h;
            vp.Width = new_w;
            //ビューポート設定
            this._d3d_device.Viewport = vp;

            // ビュー変換の設定(左手座標系ビュー行列で設定する)
            // 0,0,0から、Z+方向を向いて、上方向がY軸
            this._d3d_device.Transform.View = Matrix.LookAtLH(
                new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, 1.0f, 0.0f));
            return scale;
        }
        /**
         * コンストラクタから呼び出す関数です。
         * @param i_ref_param
         * @param i_ref_code
         * @param i_marker_width
         * @param i_input_raster_type
         * @param i_profile_id
         * @throws NyARException
         */
        protected void initialize(
            NyARParam i_ref_param,
            NyARCode i_ref_code,
            double i_marker_width,
            int i_input_raster_type,
            int i_profile_id)
        {
            NyARRasterFilter_ARToolkitThreshold th = new NyARRasterFilter_ARToolkitThreshold(100, i_input_raster_type);
            INyARColorPatt patt_inst;
            NyARSquareContourDetector sqdetect_inst;
            INyARTransMat transmat_inst;

            switch (i_profile_id)
            {
                case PF_ARTOOLKIT_COMPATIBLE:
                    patt_inst = new NyARColorPatt_O3(i_ref_code.getWidth(), i_ref_code.getHeight());
                    sqdetect_inst = new NyARSquareContourDetector_ARToolKit(i_ref_param.getScreenSize());
                    transmat_inst = new NyARTransMat_ARToolKit(i_ref_param);
                    break;
                case PF_NYARTOOLKIT_ARTOOLKIT_FITTING:
                    patt_inst = new NyARColorPatt_Perspective_O2(i_ref_code.getWidth(), i_ref_code.getHeight(), 4, 25);
                    sqdetect_inst = new NyARSquareContourDetector_Rle(i_ref_param.getScreenSize());
                    transmat_inst = new NyARTransMat_ARToolKit(i_ref_param);
                    break;
                case PF_NYARTOOLKIT://default
                    patt_inst = new NyARColorPatt_Perspective_O2(i_ref_code.getWidth(), i_ref_code.getHeight(), 4, 25);
                    sqdetect_inst = new NyARSquareContourDetector_Rle(i_ref_param.getScreenSize());
                    transmat_inst = new NyARTransMat(i_ref_param);
                    break;
                default:
                    throw new NyARException();
            }
            base.initInstance(patt_inst, sqdetect_inst, transmat_inst,th, i_ref_param, i_ref_code, i_marker_width);
            return;
        }
        protected void initInstance(NyARParam i_param, int i_raster_type)
        {
            //初期化済?
            Debug.Assert(this._initialized == false);

            NyARIntSize scr_size = i_param.getScreenSize();
            // 解析オブジェクトを作る
            this._square_detect = new NyARSquareContourDetector_Rle(scr_size);
            this._transmat = new NyARTransMat(i_param);
            this._tobin_filter = new NyARRasterFilter_ARToolkitThreshold(110, i_raster_type);

            // 2値画像バッファを作る
            this._bin_raster = new NyARBinRaster(scr_size.w, scr_size.h);
            this._threshold_detect = new NyARRasterThresholdAnalyzer_SlidePTile(15, i_raster_type, 4);
            this._initialized = true;
            //コールバックハンドラ
            this._detectmarker_cb = new DetectSquareCB(i_param);
            this._offset = new NyARRectOffset();
            return;
        }
 public NyARSingleDetectMarker_NyARTK_FITTING_ARTKv2(NyARParam i_ref_param, NyARCode i_ref_code, double i_marker_width)
     : base(i_ref_param, i_ref_code, i_marker_width)
 {
     this._inst_patt = new NyARColorPatt_Perspective(i_ref_code.getWidth(), i_ref_code.getHeight(), 4, 25);
     this._transmat = new NyARTransMat_ARToolKit(i_ref_param);
     this._square_detect = new NyARSingleDetectMarker_ARTKv2.ARTKDetector(this, i_ref_param.getScreenSize());
 }
        /**
         * この関数は、インスタンスを初期化します。
         * 継承先のクラスから呼び出してください。
         * @param i_param
         * カメラパラメータオブジェクト。このサイズは、{@link #detectMarker}に入力する画像と同じサイズである必要があります。
         * @param i_encoder
         * IDマーカの値エンコーダを指定します。
         * @param i_marker_width
         * マーカの物理縦横サイズをmm単位で指定します。
         * @
         */
        protected void initInstance(NyARParam i_param, INyIdMarkerDataEncoder i_encoder, double i_marker_width)
        {
            //初期化済?
            Debug.Assert(this._initialized == false);

            NyARIntSize scr_size = i_param.getScreenSize();
            // 解析オブジェクトを作る
            this._square_detect = new RleDetector(
                i_param,
                i_encoder,
                new NyIdMarkerPickup());
            this._transmat = new NyARTransMat(i_param);

            // 2値画像バッファを作る
            this._gs_raster = new NyARGrayscaleRaster(scr_size.w, scr_size.h);
            this._histmaker = (INyARHistogramFromRaster)this._gs_raster.createInterface(typeof(INyARHistogramFromRaster));
            //ワーク用のデータオブジェクトを2個作る
            this._data_current = i_encoder.createDataInstance();
            this._threshold_detect = new NyARHistogramAnalyzer_SlidePTile(15);
            this._initialized = true;
            this._is_active = false;
            this._offset = new NyARRectOffset();
            this._offset.setSquare(i_marker_width);
            return;

        }
 protected NyARSingleDetectMarker(NyARParam i_ref_param, NyARCode i_ref_code, double i_marker_width)
 {
     this._deviation_data = new NyARMatchPattDeviationColorData(i_ref_code.getWidth(), i_ref_code.getHeight());
     this._match_patt = new NyARMatchPatt_Color_WITHOUT_PCA(i_ref_code);
     this._offset = new NyARRectOffset();
     this._offset.setSquare(i_marker_width);
     this._coordline = new NyARCoord2Linear(i_ref_param.getScreenSize(), i_ref_param.getDistortionFactor());
     //2値画像バッファを作る
     NyARIntSize s = i_ref_param.getScreenSize();
     this._bin_raster = new NyARBinRaster(s.w, s.h);
 }
 public DetectSquare(NyARParam i_param): base(i_param.getScreenSize())
 {
     this._match_patt = null;
     this._coordline = new NyARCoord2Linear(i_param.getScreenSize(), i_param.getDistortionFactor());
     return;
 }
示例#20
0
		/**
		 * この関数は、ARToolKitスタイルのカメラパラメータから、 CameraFrustamを計算します。
		 * カメラパラメータの要素のうち、ProjectionMatrix成分のみを使います。
		 * @param i_arparam
		 * ARToolKitスタイルのカメラパラメータ。
		 * @param i_scale
		 * スケール値を指定します。1=1mmです。10ならば1=1cm,1000ならば1=1mです。
		 * 2.53以前のNyARToolkitと互換性を持たせるときは、{@link #SCALE_FACTOR_toCameraFrustumRH_NYAR2}を指定してください。
		 * @param i_near
		 * 視錐体のnearPointを指定します。単位は、i_scaleに設定した値で決まります。
		 * @param i_far
		 * 視錐体のfarPointを指定します。単位は、i_scaleに設定した値で決まります。
		 * @param o_gl_projection
		 * OpenGLスタイルのProjectionMatrixです。double[16]を指定します。
		 */
		public static void toCameraFrustumRH(NyARParam i_arparam,double i_scale,double i_near,double i_far,ref Matrix4x4 o_mat)
		{
			toCameraFrustumRH(i_arparam.getPerspectiveProjectionMatrix(),i_arparam.getScreenSize(),i_scale,i_near,i_far,ref o_mat);
			return;
		}