Example #1
0
        //override
        public void pca(double[] i_v1, double[] i_v2, int i_number_of_point, NyARDoubleMatrix22 o_evec, double[] o_ev, double[] o_mean)
        {
            NyARMatPca input = this.__pca_input;// 次処理で初期化される。

            // pcaの準備
            input.realloc(i_number_of_point, 2);
            double[][] input_array = input.getArray();
            for (int i = 0; i < i_number_of_point; i++)
            {
                input_array[i][0] = i_v1[i];
                input_array[i][1] = i_v2[i];
            }
            // 主成分分析
            input.pca(this.__pca_evec, this.__pca_ev, this.__pca_mean);
            double[]   mean_array = this.__pca_mean.getArray();
            double[][] evec_array = this.__pca_evec.getArray();
            double[]   ev_array   = this.__pca_ev.getArray();
            o_evec.m00 = evec_array[0][0];
            o_evec.m01 = evec_array[0][1];
            o_evec.m10 = evec_array[1][0];
            o_evec.m11 = evec_array[1][1];
            o_ev[0]    = ev_array[0];
            o_ev[1]    = ev_array[1];
            o_mean[0]  = mean_array[0];
            o_mean[1]  = mean_array[1];
            return;
        }
 //override
 public void pca(double[] i_v1, double[] i_v2, int i_number_of_point, NyARDoubleMatrix22 o_evec, double[] o_ev, double[] o_mean)
 {
     NyARMatPca input = this.__pca_input;// 次処理で初期化される。		
     // pcaの準備
     input.realloc(i_number_of_point, 2);
     double[][] input_array = input.getArray();
     for (int i = 0; i < i_number_of_point; i++)
     {
         input_array[i][0] = i_v1[i];
         input_array[i][1] = i_v2[i];
     }
     // 主成分分析
     input.pca(this.__pca_evec, this.__pca_ev, this.__pca_mean);
     double[] mean_array = this.__pca_mean.getArray();
     double[][] evec_array = this.__pca_evec.getArray();
     double[] ev_array = this.__pca_ev.getArray();
     o_evec.m00 = evec_array[0][0];
     o_evec.m01 = evec_array[0][1];
     o_evec.m10 = evec_array[1][0];
     o_evec.m11 = evec_array[1][1];
     o_ev[0] = ev_array[0];
     o_ev[1] = ev_array[1];
     o_mean[0] = mean_array[0];
     o_mean[1] = mean_array[1];
     return;
 }
        /**
         * この関数は、遠近法のパラメータを計算して、返却します。
         */
        public override sealed bool getParam(int i_dest_w, int i_dest_h, double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] o_param)
        {
            double ltx = this._local_x;
            double lty = this._local_y;
            double rbx = ltx + i_dest_w;
            double rby = lty + i_dest_h;

            NyARDoubleMatrix44 mat_x = new NyARDoubleMatrix44();
            mat_x.m00 = ltx; mat_x.m01 = lty; mat_x.m02 = -ltx * x1; mat_x.m03 = -lty * x1;
            mat_x.m10 = rbx; mat_x.m11 = lty; mat_x.m12 = -rbx * x2; mat_x.m13 = -lty * x2;
            mat_x.m20 = rbx; mat_x.m21 = rby; mat_x.m22 = -rbx * x3; mat_x.m23 = -rby * x3;
            mat_x.m30 = ltx; mat_x.m31 = rby; mat_x.m32 = -ltx * x4; mat_x.m33 = -rby * x4;
            mat_x.inverse(mat_x);
            NyARDoubleMatrix44 mat_y = new NyARDoubleMatrix44();
            mat_y.m00 = ltx; mat_y.m01 = lty; mat_y.m02 = -ltx * y1; mat_y.m03 = -lty * y1;
            mat_y.m10 = rbx; mat_y.m11 = lty; mat_y.m12 = -rbx * y2; mat_y.m13 = -lty * y2;
            mat_y.m20 = rbx; mat_y.m21 = rby; mat_y.m22 = -rbx * y3; mat_y.m23 = -rby * y3;
            mat_y.m30 = ltx; mat_y.m31 = rby; mat_y.m32 = -ltx * y4; mat_y.m33 = -rby * y4;
            mat_y.inverse(mat_y);
            double a = mat_x.m20 * x1 + mat_x.m21 * x2 + mat_x.m22 * x3 + mat_x.m23 * x4;
            double b = mat_x.m20 + mat_x.m21 + mat_x.m22 + mat_x.m23;
            double d = mat_x.m30 * x1 + mat_x.m31 * x2 + mat_x.m32 * x3 + mat_x.m33 * x4;
            double f = mat_x.m30 + mat_x.m31 + mat_x.m32 + mat_x.m33;

            double g = mat_y.m20 * y1 + mat_y.m21 * y2 + mat_y.m22 * y3 + mat_y.m23 * y4;
            double h = mat_y.m20 + mat_y.m21 + mat_y.m22 + mat_y.m23;
            double i = mat_y.m30 * y1 + mat_y.m31 * y2 + mat_y.m32 * y3 + mat_y.m33 * y4;
            double j = mat_y.m30 + mat_y.m31 + mat_y.m32 + mat_y.m33;

            NyARDoubleMatrix22 tm = new NyARDoubleMatrix22();
            tm.m00 = b;
            tm.m01 = -h;
            tm.m10 = f;
            tm.m11 = -j;
            tm.inverse(tm);

            double A, B, C, D, E, F, G, H;

            C = tm.m00 * (a - g) + tm.m01 * (d - i);	//C
            F = tm.m10 * (a - g) + tm.m11 * (d - i);	//F
            G = a - C * b;
            H = d - C * f;
            A = (mat_x.m00 * x1 + mat_x.m01 * x2 + mat_x.m02 * x3 + mat_x.m03 * x4) - C * (mat_x.m00 + mat_x.m01 + mat_x.m02 + mat_x.m03);
            B = (mat_x.m10 * x1 + mat_x.m11 * x2 + mat_x.m12 * x3 + mat_x.m13 * x4) - C * (mat_x.m10 + mat_x.m11 + mat_x.m12 + mat_x.m13);
            D = (mat_y.m00 * y1 + mat_y.m01 * y2 + mat_y.m02 * y3 + mat_y.m03 * y4) - F * (mat_y.m00 + mat_y.m01 + mat_y.m02 + mat_y.m03);
            E = (mat_y.m10 * y1 + mat_y.m11 * y2 + mat_y.m12 * y3 + mat_y.m13 * y4) - F * (mat_y.m10 + mat_y.m11 + mat_y.m12 + mat_y.m13);

            o_param[0] = A;
            o_param[1] = B;
            o_param[2] = C;
            o_param[3] = D;
            o_param[4] = E;
            o_param[5] = F;
            o_param[6] = G;
            o_param[7] = H;

            return true;
        }
 /**
  * この関数は、逆行列を計算して、インスタンスにセットします。
  * @param i_src
  * 逆行列を計算するオブジェクト。thisを指定できます。
  * @return
  * 逆行列を得られると、trueを返します。
  */
 public bool inverse(NyARDoubleMatrix22 i_src)
 {
     double a11,a12,a21,a22;
     a11=i_src.m00;
     a12=i_src.m01;
     a21=i_src.m10;
     a22=i_src.m11;
     double det=a11*a22-a12*a21;
     if(det==0){
     return false;
     }
     det=1/det;
     this.m00=a22*det;
     this.m01=-a12*det;
     this.m10=-a21*det;
     this.m11=a11*det;
     return true;
 }
Example #5
0
        /**
         * この関数は、逆行列を計算して、インスタンスにセットします。
         * @param i_src
         * 逆行列を計算するオブジェクト。thisを指定できます。
         * @return
         * 逆行列を得られると、trueを返します。
         */
        public bool inverse(NyARDoubleMatrix22 i_src)
        {
            double a11, a12, a21, a22;

            a11 = i_src.m00;
            a12 = i_src.m01;
            a21 = i_src.m10;
            a22 = i_src.m11;
            double det = a11 * a22 - a12 * a21;

            if (det == 0)
            {
                return(false);
            }
            det      = 1 / det;
            this.m00 = a22 * det;
            this.m01 = -a12 * det;
            this.m10 = -a21 * det;
            this.m11 = a11 * det;
            return(true);
        }
Example #6
0
        /**
         * この関数は、輪郭点集合からay+bx+c=0の直線式を計算します。
         * @param i_st
         * 直線計算の対象とする、輪郭点の開始インデックス
         * @param i_ed
         * 直線計算の対象とする、輪郭点の終了インデックス
         * @param i_coord
         * 輪郭点集合のオブジェクト。
         * @param o_line
         * 直線式を受け取るオブジェクト
         * @return
         * 直線式の計算に成功すると、trueを返します。
         * @
         */
        public bool coord2Line(int i_st, int i_ed, NyARIntCoordinates i_coord, NyARLinear o_line)
        {
            //頂点を取得
            int    n, st, ed;
            double w1;
            int    cood_num = i_coord.length;

            //探索区間の決定
            if (i_ed >= i_st)
            {
                //頂点[i]から頂点[i+1]までの輪郭が、1区間にあるとき
                w1 = (double)(i_ed - i_st + 1) * 0.05 + 0.5;
                //探索区間の決定
                st = (int)(i_st + w1);
                ed = (int)(i_ed - w1);
            }
            else
            {
                //頂点[i]から頂点[i+1]までの輪郭が、2区間に分かれているとき
                w1 = (double)((i_ed + cood_num - i_st + 1) % cood_num) * 0.05 + 0.5;
                //探索区間の決定
                st = ((int)(i_st + w1)) % cood_num;
                ed = ((int)(i_ed + cood_num - w1)) % cood_num;
            }
            //探索区間数を確認
            if (st <= ed)
            {
                //探索区間は1区間
                n = ed - st + 1;
                if (this._dist_factor != null)
                {
                    this._dist_factor.observ2IdealBatch(i_coord.items, st, n, this._xpos, this._ypos, 0);
                }
            }
            else
            {
                //探索区間は2区間
                n = ed + 1 + cood_num - st;
                if (this._dist_factor != null)
                {
                    this._dist_factor.observ2IdealBatch(i_coord.items, st, cood_num - st, this._xpos, this._ypos, 0);
                    this._dist_factor.observ2IdealBatch(i_coord.items, 0, ed + 1, this._xpos, this._ypos, cood_num - st);
                }
            }
            //要素数の確認
            if (n < 2)
            {
                // nが2以下でmatrix.PCAを計算することはできないので、エラー
                return(false);
            }
            //主成分分析する。
            NyARDoubleMatrix22 evec = this.__getSquareLine_evec;

            double[] mean = this.__getSquareLine_mean;


            this._pca.pca(this._xpos, this._ypos, n, evec, this.__getSquareLine_ev, mean);
            o_line.a = evec.m01;                                   // line[i][0] = evec->m[1];
            o_line.b = -evec.m00;                                  // line[i][1] = -evec->m[0];
            o_line.c = -(o_line.a * mean[0] + o_line.b * mean[1]); // line[i][2] = -(line[i][0]*mean->v[0] + line[i][1]*mean->v[1]);

            return(true);
        }
        /**
         * この関数は、遠近法のパラメータを計算して、返却します。
         */
        public sealed override bool getParam(int i_dest_w, int i_dest_h, double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] o_param)
        {
            double ltx = this._local_x;
            double lty = this._local_y;
            double rbx = ltx + i_dest_w;
            double rby = lty + i_dest_h;

            NyARDoubleMatrix44 mat_x = new NyARDoubleMatrix44();

            mat_x.m00 = ltx; mat_x.m01 = lty; mat_x.m02 = -ltx * x1; mat_x.m03 = -lty * x1;
            mat_x.m10 = rbx; mat_x.m11 = lty; mat_x.m12 = -rbx * x2; mat_x.m13 = -lty * x2;
            mat_x.m20 = rbx; mat_x.m21 = rby; mat_x.m22 = -rbx * x3; mat_x.m23 = -rby * x3;
            mat_x.m30 = ltx; mat_x.m31 = rby; mat_x.m32 = -ltx * x4; mat_x.m33 = -rby * x4;
            mat_x.inverse(mat_x);
            NyARDoubleMatrix44 mat_y = new NyARDoubleMatrix44();

            mat_y.m00 = ltx; mat_y.m01 = lty; mat_y.m02 = -ltx * y1; mat_y.m03 = -lty * y1;
            mat_y.m10 = rbx; mat_y.m11 = lty; mat_y.m12 = -rbx * y2; mat_y.m13 = -lty * y2;
            mat_y.m20 = rbx; mat_y.m21 = rby; mat_y.m22 = -rbx * y3; mat_y.m23 = -rby * y3;
            mat_y.m30 = ltx; mat_y.m31 = rby; mat_y.m32 = -ltx * y4; mat_y.m33 = -rby * y4;
            mat_y.inverse(mat_y);
            double a = mat_x.m20 * x1 + mat_x.m21 * x2 + mat_x.m22 * x3 + mat_x.m23 * x4;
            double b = mat_x.m20 + mat_x.m21 + mat_x.m22 + mat_x.m23;
            double d = mat_x.m30 * x1 + mat_x.m31 * x2 + mat_x.m32 * x3 + mat_x.m33 * x4;
            double f = mat_x.m30 + mat_x.m31 + mat_x.m32 + mat_x.m33;

            double g = mat_y.m20 * y1 + mat_y.m21 * y2 + mat_y.m22 * y3 + mat_y.m23 * y4;
            double h = mat_y.m20 + mat_y.m21 + mat_y.m22 + mat_y.m23;
            double i = mat_y.m30 * y1 + mat_y.m31 * y2 + mat_y.m32 * y3 + mat_y.m33 * y4;
            double j = mat_y.m30 + mat_y.m31 + mat_y.m32 + mat_y.m33;

            NyARDoubleMatrix22 tm = new NyARDoubleMatrix22();

            tm.m00 = b;
            tm.m01 = -h;
            tm.m10 = f;
            tm.m11 = -j;
            tm.inverse(tm);


            double A, B, C, D, E, F, G, H;

            C = tm.m00 * (a - g) + tm.m01 * (d - i);    //C
            F = tm.m10 * (a - g) + tm.m11 * (d - i);    //F
            G = a - C * b;
            H = d - C * f;
            A = (mat_x.m00 * x1 + mat_x.m01 * x2 + mat_x.m02 * x3 + mat_x.m03 * x4) - C * (mat_x.m00 + mat_x.m01 + mat_x.m02 + mat_x.m03);
            B = (mat_x.m10 * x1 + mat_x.m11 * x2 + mat_x.m12 * x3 + mat_x.m13 * x4) - C * (mat_x.m10 + mat_x.m11 + mat_x.m12 + mat_x.m13);
            D = (mat_y.m00 * y1 + mat_y.m01 * y2 + mat_y.m02 * y3 + mat_y.m03 * y4) - F * (mat_y.m00 + mat_y.m01 + mat_y.m02 + mat_y.m03);
            E = (mat_y.m10 * y1 + mat_y.m11 * y2 + mat_y.m12 * y3 + mat_y.m13 * y4) - F * (mat_y.m10 + mat_y.m11 + mat_y.m12 + mat_y.m13);

            o_param[0] = A;
            o_param[1] = B;
            o_param[2] = C;
            o_param[3] = D;
            o_param[4] = E;
            o_param[5] = F;
            o_param[6] = G;
            o_param[7] = H;


            return(true);
        }