Exemplo n.º 1
0
        //使用参数矩阵W求得目标图
        public void get_result_img(string interp_method)
        {
            myMat.myCoor source_leftUp_point   = new myMat.myCoor(in_i: sourceFace_distort.inputImg.height, in_j: sourceFace_distort.inputImg.width); //初始化为右下角点
            myMat.myCoor source_leftDown_point = new myMat.myCoor(in_i: 0, in_j: sourceFace_distort.inputImg.width);                                  //初始化为右上角点
            leftUp_point   = new myMat.myCoor();                                                                                                      //初始化为右下角点
            leftDown_point = new myMat.myCoor();                                                                                                      //初始化为右上角点
            myMat.myCoor vir_leftUp_point   = new myMat.myCoor(0, 0);
            myMat.myCoor vir_leftDown_point = new myMat.myCoor(sourceFace_distort.inputImg.height, 0);

            sourceFace_distort.resultImg = new myMat();
            sourceFace_distort.resultImg.init_bytes(sourceFace_distort.inputImg.height, sourceFace_distort.inputImg.width);
            for (int i = 0; i < sourceFace_distort.inputImg.height; i++)
            {
                for (int j = 0; j < sourceFace_distort.inputImg.width; j++)
                {
                    //得到目标图中第i行i列坐标对应原图中的坐标
                    myMat.myCoor sourceCoor = TPS_get_sourceCoor(i, j);

                    if (sourceCoor.i >= 0 && sourceCoor.j >= 0 && sourceCoor.i < sourceFace_distort.inputImg.height && sourceCoor.j < sourceFace_distort.inputImg.width)
                    {
                        if (myMat.myCoor.getDis(sourceCoor, vir_leftUp_point) < myMat.myCoor.getDis(source_leftUp_point, vir_leftUp_point))
                        {
                            source_leftUp_point.i = sourceCoor.i; source_leftUp_point.j = sourceCoor.j;
                            leftUp_point.i        = i; leftUp_point.j = j;
                        }

                        if (myMat.myCoor.getDis(sourceCoor, vir_leftDown_point) < myMat.myCoor.getDis(source_leftDown_point, vir_leftDown_point))
                        {
                            source_leftDown_point.i = sourceCoor.i; source_leftDown_point.j = sourceCoor.j;
                            leftDown_point.i        = i; leftDown_point.j = j;
                        }
                    }


                    Color c = sourceFace_distort.interp(sourceCoor, interp_method);
                    sourceFace_distort.resultImg.set_rgb(c, new myMat.myCoor(i, j));
                }
            }
        }
Exemplo n.º 2
0
        //切除黑边
        public void BlackCut_tran(string interp_method = "最近邻")
        {
            blackCut_distort = new Pic_distort();

            blackCut_distort.inputImg = new myMat();
            blackCut_distort.inputImg.getData_Mat(sourceFace_distort.resultImg);
            blackCut_distort.resultImg = new myMat();
            blackCut_distort.resultImg.init_bytes(blackCut_distort.inputImg.height, blackCut_distort.inputImg.width);

            /*Bitmap bm = blackCut_distort.inputImg.img2Bitmap();
             * Image<Bgr, byte> draw_img = new Image<Bgr, byte>(bm);
             * draw_img.Draw(new Cross2DF(new PointF((int)leftUp_point.j, (int)leftUp_point.i), 10, 10), new Bgr(0, 0, 255), 2);
             * draw_img.Draw(new Cross2DF(new PointF((int)leftDown_point.j, (int)leftDown_point.i), 10, 10), new Bgr(0, 0, 255), 2);
             *
             * CvInvoke.Imshow("a", draw_img);
             * CvInvoke.WaitKey();*/

            double turn_angle   = System.Math.Atan2((leftDown_point.j - leftUp_point.j), (leftDown_point.i - leftUp_point.i));
            double height_scale = myMat.myCoor.getDis(leftUp_point, leftDown_point) / blackCut_distort.resultImg.height;

            for (int i = 0; i < blackCut_distort.inputImg.height; i++)
            {
                for (int j = 0; j < blackCut_distort.inputImg.width; j++)
                {
                    if (i == blackCut_distort.inputImg.height - 3 && j == 0)
                    {
                        int a = 0;
                    }
                    myMat.myCoor sourceCoor = Turn_get_sourceCoor(i, j, turn_angle);
                    sourceCoor.i *= height_scale;
                    sourceCoor.j *= height_scale;
                    sourceCoor.i += (int)leftUp_point.i;
                    sourceCoor.j += (int)leftUp_point.j;
                    Color c = blackCut_distort.interp(sourceCoor, interp_method);
                    blackCut_distort.resultImg.set_rgb(c, new myMat.myCoor(i, j));
                }
            }

            /*Bitmap bm2 = blackCut_distort.resultImg.img2Bitmap();
             * Image<Bgr, byte> draw_img2 = new Image<Bgr, byte>(bm2);
             *
             * CvInvoke.Imshow("a", draw_img2);
             * CvInvoke.WaitKey();*/

            int min_y, max_y, max_x, min_x;

            blackCut_getIJ(out min_y, out max_y, out min_x, out max_x);

            int new_height = max_x - min_x + 1;
            int new_width  = max_y - min_y + 1;

            for (int i = 0; i < new_height; i++)
            {
                for (int j = 0; j < new_width; j++)
                {
                    int   i_cut        = min_x + i;
                    int   j_cut        = min_y + j;
                    Color source_color = blackCut_distort.resultImg.getRGB(new myMat.myCoor(i_cut, j_cut));
                    blackCut_distort.resultImg.set_rgb(source_color, new myMat.myCoor(i, j));
                }
            }
            blackCut_distort.resultImg.height = new_height;
            blackCut_distort.resultImg.width  = new_width;
        }