/**
         * この関数は、ラスタのi_vertexsで定義される四角形からパターンを取得して、インスタンスに格納します。
         */
        public bool pickFromRaster(INyARRgbRaster image, NyARIntPoint2d[] i_vertexs)
        {
            double[] conv_param = this._convparam;
            int      rx2, ry2;

            rx2 = this._size.w;
            ry2 = this._size.h;
            int[] rgb_tmp = new int[3];


            // 変形先領域の頂点を取得

            //変換行列から現在の座標系への変換パラメタを作成
            calcPara(i_vertexs, conv_param);// 変換パラメータを求める
            for (int y = 0; y < ry2; y++)
            {
                for (int x = 0; x < rx2; x++)
                {
                    int ttx = (int)((conv_param[0] * x * y + conv_param[1] * x + conv_param[2] * y + conv_param[3]) + 0.5);
                    int tty = (int)((conv_param[4] * x * y + conv_param[5] * x + conv_param[6] * y + conv_param[7]) + 0.5);
                    image.getPixel((int)ttx, (int)tty, rgb_tmp);
                    this._buf[x + y * rx2] = (rgb_tmp[0] << 16) | (rgb_tmp[1] << 8) | rgb_tmp[2];
                }
            }
            return(true);
        }
        public override void doFilter(int i_l, int i_t, int i_w, int i_h, int i_th, INyARGrayscaleRaster i_gsraster)
        {
            Debug.Assert(i_gsraster.isEqualBufferType(NyARBufferType.INT1D_BIN_8));
            INyARRgbRaster input = this._raster;

            int[]       output   = (int[])i_gsraster.getBuffer();
            int         th       = i_th * 3;
            NyARIntSize s        = i_gsraster.getSize();
            int         skip_dst = (s.w - i_w);
            int         skip_src = skip_dst;
            //左上から1行づつ走査していく
            int pt_dst = (i_t * s.w + i_l);

            int[] rgb = this.__rgb;
            for (int y = 0; y < i_h; y++)
            {
                int x;
                for (x = 0; x < i_w; x++)
                {
                    input.getPixel(x + i_l, y + i_t, rgb);
                    output[pt_dst++] = (rgb[0] + rgb[1] + rgb[2]) <= th ? 0 : 1;
                }
                //スキップ
                pt_dst += skip_dst;
            }
            return;
        }
예제 #3
0
        public double makeColorData(int[] o_out)
        {
            NyARIntSize    size   = this._ref_raster.getSize();
            INyARRgbRaster pixdev = this._ref_raster;

            int[] rgb   = this.__rgb;
            int   width = size.w;
            //<平均値計算>
            int ave = 0;//<PV/>

            for (int y = size.h - 1; y >= 0; y--)
            {
                for (int x = width - 1; x >= 0; x--)
                {
                    pixdev.getPixel(x, y, rgb);
                    ave += rgb[0] + rgb[1] + rgb[2];
                }
            }
            //<平均値計算>
            int number_of_pix = size.w * size.h;

            ave = number_of_pix * 255 * 3 - ave;
            ave = 255 - (ave / (number_of_pix * 3));//(255-R)-ave を分解するための事前計算

            int sum = 0, w_sum;
            int input_ptr = number_of_pix * 3 - 1;

            //<差分値計算>
            for (int y = size.h - 1; y >= 0; y--)
            {
                for (int x = width - 1; x >= 0; x--)
                {
                    pixdev.getPixel(x, y, rgb);
                    w_sum = (ave - rgb[2]); o_out[input_ptr--] = w_sum; sum += w_sum * w_sum; //B
                    w_sum = (ave - rgb[1]); o_out[input_ptr--] = w_sum; sum += w_sum * w_sum; //G
                    w_sum = (ave - rgb[0]); o_out[input_ptr--] = w_sum; sum += w_sum * w_sum; //R
                }
            }
            //<差分値計算(FORの1/8展開)/>
            double p = Math.Sqrt((double)sum);

            return(p != 0.0 ? p : 0.0000001);
        }
예제 #4
0
        public void createHistogram(int i_l, int i_t, int i_w, int i_h, int i_skip, NyARHistogram o_histogram)
        {
            o_histogram.reset();
            int[]          data_ptr     = o_histogram.data;
            INyARRgbRaster drv          = this._gsr;
            int            pix_count    = i_w;
            int            pix_mod_part = pix_count - (pix_count % 8);

            //左上から1行づつ走査していく
            for (int y = i_h - 1; y >= 0; y -= i_skip)
            {
                for (int x = pix_count - 1; x >= pix_mod_part; x--)
                {
                    drv.getPixel(x, y, tmp);
                    data_ptr[(tmp[0] + tmp[1] + tmp[2]) / 3]++;
                }
            }
            o_histogram.total_of_data = i_w * i_h / i_skip;
            return;
        }
        public void convertRect(int l, int t, int w, int h, INyARGrayscaleRaster o_raster)
        {
            int[] wk        = this._wk;
            int   b         = t + h;
            int   pix_count = w;

            switch (o_raster.getBufferType())
            {
            default:
                INyARRgbRaster in_drv = this._ref_raster;
                for (int y = t; y < b; y++)
                {
                    for (int x = pix_count - 1; x >= 0; x--)
                    {
                        in_drv.getPixel(x, y, wk);
                        o_raster.setPixel(x, y, (306 * (wk[2] & 0xff) + 601 * (wk[1] & 0xff) + 117 * (wk[0] & 0xff)) >> 10);
                    }
                }
                return;
            }
        }
예제 #6
0
        /**
         * この関数は、元画像を回転してから、差分画像を生成して、格納します。
         * 制限として、この関数はあまり高速ではありません。連続使用するときは、最適化を検討してください。
         * @param i_raster
         * 差分画像の元画像。サイズは、このインスタンスと同じである必要があります。
         * @param i_direction
         * 右上の位置です。0=1象限、1=2象限、、2=3象限、、3=4象限の位置に対応します。
         * @
         */
        public void setRaster(INyARRgbRaster i_raster, int i_direction)
        {
            int width           = this._size.w;
            int height          = this._size.h;
            int i_number_of_pix = width * height;

            int[] rgb  = new int[3];
            int[] dout = this._data;
            int   ave;//<PV/>

            //<平均値計算>
            ave = 0;
            for (int y = height - 1; y >= 0; y--)
            {
                for (int x = width - 1; x >= 0; x--)
                {
                    i_raster.getPixel(x, y, rgb);
                    ave += rgb[0] + rgb[1] + rgb[2];
                }
            }
            //<平均値計算>
            ave = i_number_of_pix * 255 * 3 - ave;
            ave = 255 - (ave / (i_number_of_pix * 3));//(255-R)-ave を分解するための事前計算

            int sum = 0, w_sum;
            int input_ptr = i_number_of_pix * 3 - 1;

            switch (i_direction)
            {
            case 0:
                for (int y = height - 1; y >= 0; y--)
                {
                    for (int x = width - 1; x >= 0; x--)
                    {
                        i_raster.getPixel(x, y, rgb);
                        w_sum = (ave - rgb[2]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //B
                        w_sum = (ave - rgb[1]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //G
                        w_sum = (ave - rgb[0]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //R
                    }
                }
                break;

            case 1:
                for (int x = 0; x < width; x++)
                {
                    for (int y = height - 1; y >= 0; y--)
                    {
                        i_raster.getPixel(x, y, rgb);
                        w_sum = (ave - rgb[2]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //B
                        w_sum = (ave - rgb[1]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //G
                        w_sum = (ave - rgb[0]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //R
                    }
                }
                break;

            case 2:
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        i_raster.getPixel(x, y, rgb);
                        w_sum = (ave - rgb[2]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //B
                        w_sum = (ave - rgb[1]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //G
                        w_sum = (ave - rgb[0]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //R
                    }
                }
                break;

            case 3:
                for (int x = width - 1; x >= 0; x--)
                {
                    for (int y = 0; y < height; y++)
                    {
                        i_raster.getPixel(x, y, rgb);
                        w_sum = (ave - rgb[2]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //B
                        w_sum = (ave - rgb[1]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //G
                        w_sum = (ave - rgb[0]); dout[input_ptr--] = w_sum; sum += w_sum * w_sum;    //R
                    }
                }
                break;
            }
            //<差分値計算>
            //<差分値計算(FORの1/8展開)/>
            double p = Math.Sqrt((double)sum);

            this._pow = (p != 0.0 ? p : 0.0000001);
        }
        protected override bool onePixel(int pk_l, int pk_t, double[] cpara, INyARRaster o_out)
        {
            int[] rgb_tmp = this.__pickFromRaster_rgb_tmp;
            int   in_w    = this._ref_raster.getWidth();
            int   in_h    = this._ref_raster.getHeight();

            //ピクセルリーダーを取得
            double cp0 = cpara[0];
            double cp3 = cpara[3];
            double cp6 = cpara[6];
            double cp1 = cpara[1];
            double cp4 = cpara[4];
            double cp7 = cpara[7];

            int    out_w      = o_out.getWidth();
            int    out_h      = o_out.getHeight();
            double cp7_cy_1   = cp7 * pk_t + 1.0 + cp6 * pk_l;
            double cp1_cy_cp2 = cp1 * pk_t + cpara[2] + cp0 * pk_l;
            double cp4_cy_cp5 = cp4 * pk_t + cpara[5] + cp3 * pk_l;

            INyARRgbRaster i_in_reader = this._ref_raster;

            switch (o_out.getBufferType())
            {
            case NyARBufferType.INT1D_X8R8G8B8_32:
                int[] pat_data = (int[])o_out.getBuffer();
                int   p        = 0;
                for (int iy = out_h - 1; iy >= 0; iy--)
                {
                    //解像度分の点を取る。
                    double cp7_cy_1_cp6_cx   = cp7_cy_1;
                    double cp1_cy_cp2_cp0_cx = cp1_cy_cp2;
                    double cp4_cy_cp5_cp3_cx = cp4_cy_cp5;

                    for (int ix = out_w - 1; ix >= 0; ix--)
                    {
                        //1ピクセルを作成
                        double d = 1 / (cp7_cy_1_cp6_cx);
                        int    x = (int)((cp1_cy_cp2_cp0_cx) * d);
                        int    y = (int)((cp4_cy_cp5_cp3_cx) * d);
                        if (x < 0)
                        {
                            x = 0;
                        }
                        else if (x >= in_w)
                        {
                            x = in_w - 1;
                        }
                        if (y < 0)
                        {
                            y = 0;
                        }
                        else if (y >= in_h)
                        {
                            y = in_h - 1;
                        }

                        i_in_reader.getPixel(x, y, rgb_tmp);
                        cp7_cy_1_cp6_cx   += cp6;
                        cp1_cy_cp2_cp0_cx += cp0;
                        cp4_cy_cp5_cp3_cx += cp3;

                        pat_data[p] = (rgb_tmp[0] << 16) | (rgb_tmp[1] << 8) | ((rgb_tmp[2] & 0xff));
                        p++;
                    }
                    cp7_cy_1   += cp7;
                    cp1_cy_cp2 += cp1;
                    cp4_cy_cp5 += cp4;
                }
                return(true);

            default:
                //ANY to RGBx
                if (o_out is INyARRgbRaster)
                {
                    INyARRgbRaster out_reader = ((INyARRgbRaster)o_out);
                    for (int iy = 0; iy < out_h; iy++)
                    {
                        //解像度分の点を取る。
                        double cp7_cy_1_cp6_cx   = cp7_cy_1;
                        double cp1_cy_cp2_cp0_cx = cp1_cy_cp2;
                        double cp4_cy_cp5_cp3_cx = cp4_cy_cp5;
                        for (int ix = 0; ix < out_w; ix++)
                        {
                            //1ピクセルを作成
                            double d = 1 / (cp7_cy_1_cp6_cx);
                            int    x = (int)((cp1_cy_cp2_cp0_cx) * d);
                            int    y = (int)((cp4_cy_cp5_cp3_cx) * d);
                            if (x < 0)
                            {
                                x = 0;
                            }
                            else if (x >= in_w)
                            {
                                x = in_w - 1;
                            }
                            if (y < 0)
                            {
                                y = 0;
                            }
                            else if (y >= in_h)
                            {
                                y = in_h - 1;
                            }

                            i_in_reader.getPixel(x, y, rgb_tmp);
                            cp7_cy_1_cp6_cx   += cp6;
                            cp1_cy_cp2_cp0_cx += cp0;
                            cp4_cy_cp5_cp3_cx += cp3;

                            out_reader.setPixel(ix, iy, rgb_tmp);
                        }
                        cp7_cy_1   += cp7;
                        cp1_cy_cp2 += cp1;
                        cp4_cy_cp5 += cp4;
                    }
                    return(true);
                }
                break;
            }
            return(false);
        }
        protected override bool multiPixel(int pk_l, int pk_t, double[] cpara, int i_resolution, INyARRaster o_out)
        {
            int res_pix = i_resolution * i_resolution;

            int[]          rgb_tmp     = this.__pickFromRaster_rgb_tmp;
            int            in_w        = this._ref_raster.getWidth();
            int            in_h        = this._ref_raster.getHeight();
            INyARRgbRaster i_in_reader = this._ref_raster;

            //ピクセルリーダーを取得
            double cp0 = cpara[0];
            double cp3 = cpara[3];
            double cp6 = cpara[6];
            double cp1 = cpara[1];
            double cp4 = cpara[4];
            double cp7 = cpara[7];
            double cp2 = cpara[2];
            double cp5 = cpara[5];

            int out_w = o_out.getWidth();
            int out_h = o_out.getHeight();

            switch (o_out.getBufferType())
            {
            case NyARBufferType.INT1D_X8R8G8B8_32:
                int[] pat_data = (int[])o_out.getBuffer();
                int   p        = (out_w * out_h - 1);
                for (int iy = out_h - 1; iy >= 0; iy--)
                {
                    //解像度分の点を取る。
                    for (int ix = out_w - 1; ix >= 0; ix--)
                    {
                        int r, g, b;
                        r = g = b = 0;
                        int    cy = pk_t + iy * i_resolution;
                        int    cx = pk_l + ix * i_resolution;
                        double cp7_cy_1_cp6_cx_b   = cp7 * cy + 1.0 + cp6 * cx;
                        double cp1_cy_cp2_cp0_cx_b = cp1 * cy + cp2 + cp0 * cx;
                        double cp4_cy_cp5_cp3_cx_b = cp4 * cy + cp5 + cp3 * cx;
                        for (int i2y = i_resolution - 1; i2y >= 0; i2y--)
                        {
                            double cp7_cy_1_cp6_cx   = cp7_cy_1_cp6_cx_b;
                            double cp1_cy_cp2_cp0_cx = cp1_cy_cp2_cp0_cx_b;
                            double cp4_cy_cp5_cp3_cx = cp4_cy_cp5_cp3_cx_b;
                            for (int i2x = i_resolution - 1; i2x >= 0; i2x--)
                            {
                                //1ピクセルを作成
                                double d = 1 / (cp7_cy_1_cp6_cx);
                                int    x = (int)((cp1_cy_cp2_cp0_cx) * d);
                                int    y = (int)((cp4_cy_cp5_cp3_cx) * d);
                                if (x < 0)
                                {
                                    x = 0;
                                }
                                else if (x >= in_w)
                                {
                                    x = in_w - 1;
                                }
                                if (y < 0)
                                {
                                    y = 0;
                                }
                                else if (y >= in_h)
                                {
                                    y = in_h - 1;
                                }

                                i_in_reader.getPixel(x, y, rgb_tmp);
                                r += rgb_tmp[0];
                                g += rgb_tmp[1];
                                b += rgb_tmp[2];
                                cp7_cy_1_cp6_cx   += cp6;
                                cp1_cy_cp2_cp0_cx += cp0;
                                cp4_cy_cp5_cp3_cx += cp3;
                            }
                            cp7_cy_1_cp6_cx_b   += cp7;
                            cp1_cy_cp2_cp0_cx_b += cp1;
                            cp4_cy_cp5_cp3_cx_b += cp4;
                        }
                        r          /= res_pix;
                        g          /= res_pix;
                        b          /= res_pix;
                        pat_data[p] = ((r & 0xff) << 16) | ((g & 0xff) << 8) | ((b & 0xff));
                        p--;
                    }
                }
                return(true);

            default:
                //ANY to RGBx
                if (o_out is INyARRgbRaster)
                {
                    INyARRgbRaster out_reader = ((INyARRgbRaster)o_out);
                    for (int iy = out_h - 1; iy >= 0; iy--)
                    {
                        //解像度分の点を取る。
                        for (int ix = out_w - 1; ix >= 0; ix--)
                        {
                            int r, g, b;
                            r = g = b = 0;
                            int    cy = pk_t + iy * i_resolution;
                            int    cx = pk_l + ix * i_resolution;
                            double cp7_cy_1_cp6_cx_b   = cp7 * cy + 1.0 + cp6 * cx;
                            double cp1_cy_cp2_cp0_cx_b = cp1 * cy + cp2 + cp0 * cx;
                            double cp4_cy_cp5_cp3_cx_b = cp4 * cy + cp5 + cp3 * cx;
                            for (int i2y = i_resolution - 1; i2y >= 0; i2y--)
                            {
                                double cp7_cy_1_cp6_cx   = cp7_cy_1_cp6_cx_b;
                                double cp1_cy_cp2_cp0_cx = cp1_cy_cp2_cp0_cx_b;
                                double cp4_cy_cp5_cp3_cx = cp4_cy_cp5_cp3_cx_b;
                                for (int i2x = i_resolution - 1; i2x >= 0; i2x--)
                                {
                                    //1ピクセルを作成
                                    double d = 1 / (cp7_cy_1_cp6_cx);
                                    int    x = (int)((cp1_cy_cp2_cp0_cx) * d);
                                    int    y = (int)((cp4_cy_cp5_cp3_cx) * d);
                                    if (x < 0)
                                    {
                                        x = 0;
                                    }
                                    else if (x >= in_w)
                                    {
                                        x = in_w - 1;
                                    }
                                    if (y < 0)
                                    {
                                        y = 0;
                                    }
                                    else if (y >= in_h)
                                    {
                                        y = in_h - 1;
                                    }

                                    i_in_reader.getPixel(x, y, rgb_tmp);
                                    r += rgb_tmp[0];
                                    g += rgb_tmp[1];
                                    b += rgb_tmp[2];
                                    cp7_cy_1_cp6_cx   += cp6;
                                    cp1_cy_cp2_cp0_cx += cp0;
                                    cp4_cy_cp5_cp3_cx += cp3;
                                }
                                cp7_cy_1_cp6_cx_b   += cp7;
                                cp1_cy_cp2_cp0_cx_b += cp1;
                                cp4_cy_cp5_cp3_cx_b += cp4;
                            }
                            out_reader.setPixel(ix, iy, r / res_pix, g / res_pix, b / res_pix);
                        }
                    }
                    return(true);
                }
                break;
            }
            return(false);
        }
        /**
         * この関数は、ラスタのi_vertexsで定義される四角形からパターンを取得して、インスタンスに格納します。
         */
        public virtual bool pickFromRaster(INyARRgbRaster image, NyARIntPoint2d[] i_vertexs)
        {
            // パターンの切り出しに失敗することもある。
            NyARMat cpara = new NyARMat(8, 1);

            if (!get_cpara(i_vertexs, cpara))
            {
                return(false);
            }
            double[][] para   = cpara.getArray();
            double     para00 = para[0 * 3 + 0][0];
            double     para01 = para[0 * 3 + 1][0];
            double     para02 = para[0 * 3 + 2][0];
            double     para10 = para[1 * 3 + 0][0];
            double     para11 = para[1 * 3 + 1][0];
            double     para12 = para[1 * 3 + 2][0];
            double     para20 = para[2 * 3 + 0][0];
            double     para21 = para[2 * 3 + 1][0];
            double     para22 = 1.0;

            int lx1 = (int)((i_vertexs[0].x - i_vertexs[1].x) * (i_vertexs[0].x - i_vertexs[1].x) + (i_vertexs[0].y - i_vertexs[1].y) * (i_vertexs[0].y - i_vertexs[1].y));
            int lx2 = (int)((i_vertexs[2].x - i_vertexs[3].x) * (i_vertexs[2].x - i_vertexs[3].x) + (i_vertexs[2].y - i_vertexs[3].y) * (i_vertexs[2].y - i_vertexs[3].y));
            int ly1 = (int)((i_vertexs[1].x - i_vertexs[2].x) * (i_vertexs[1].x - i_vertexs[2].x) + (i_vertexs[1].y - i_vertexs[2].y) * (i_vertexs[1].y - i_vertexs[2].y));
            int ly2 = (int)((i_vertexs[3].x - i_vertexs[0].x) * (i_vertexs[3].x - i_vertexs[0].x) + (i_vertexs[3].y - i_vertexs[0].y) * (i_vertexs[3].y - i_vertexs[0].y));

            if (lx2 > lx1)
            {
                lx1 = lx2;
            }
            if (ly2 > ly1)
            {
                ly1 = ly2;
            }

            int sample_pixel_x = this._size.w;
            int sample_pixel_y = this._size.h;

            while (sample_pixel_x * sample_pixel_x < lx1 / 4)
            {
                sample_pixel_x *= 2;
            }
            while (sample_pixel_y * sample_pixel_y < ly1 / 4)
            {
                sample_pixel_y *= 2;
            }

            if (sample_pixel_x > AR_PATT_SAMPLE_NUM)
            {
                sample_pixel_x = AR_PATT_SAMPLE_NUM;
            }
            if (sample_pixel_y > AR_PATT_SAMPLE_NUM)
            {
                sample_pixel_y = AR_PATT_SAMPLE_NUM;
            }

            int xdiv = sample_pixel_x / this._size.w; // xdiv = xdiv2/Config.AR_PATT_SIZE_X;
            int ydiv = sample_pixel_y / this._size.h; // ydiv = ydiv2/Config.AR_PATT_SIZE_Y;


            int img_x = image.getWidth();
            int img_y = image.getHeight();

            double xdiv2_reciprocal = 1.0 / sample_pixel_x;
            double ydiv2_reciprocal = 1.0 / sample_pixel_y;
            int    r, g, b;

            int[] rgb_tmp = new int[3];


            int xdiv_x_ydiv = xdiv * ydiv;

            for (int iy = 0; iy < this._size.h; iy++)
            {
                for (int ix = 0; ix < this._size.w; ix++)
                {
                    r = g = b = 0;
                    //1ピクセルを作成
                    for (int j = 0; j < ydiv; j++)
                    {
                        double yw = 102.5 + 5.0 * (iy * ydiv + j + 0.5) * ydiv2_reciprocal;
                        for (int i = 0; i < xdiv; i++)
                        {
                            double xw = 102.5 + 5.0 * (ix * xdiv + i + 0.5) * xdiv2_reciprocal;
                            double d  = para20 * xw + para21 * yw + para22;
                            if (d == 0)
                            {
                                throw new NyARRuntimeException();
                            }
                            int xc = (int)((para00 * xw + para01 * yw + para02) / d);
                            int yc = (int)((para10 * xw + para11 * yw + para12) / d);

                            if (xc >= 0 && xc < img_x && yc >= 0 && yc < img_y)
                            {
                                image.getPixel(xc, yc, rgb_tmp);
                                r += rgb_tmp[0]; // R
                                g += rgb_tmp[1]; // G
                                b += rgb_tmp[2]; // B
                                // System.out.println(xc+":"+yc+":"+rgb_tmp[0]+":"+rgb_tmp[1]+":"+rgb_tmp[2]);
                            }
                        }
                    }
                    this._buf[iy * this._size.w + ix] = (((r / xdiv_x_ydiv) & 0xff) << 16) | (((g / xdiv_x_ydiv) & 0xff) << 8) | (((b / xdiv_x_ydiv) & 0xff));
                }
            }
            return(true);
        }