Emgu cv透视变换

时间:2016-02-17 11:57:22

标签: c# emgucv perspective opencvsharp

我正在工作emgucv项目,但我有问题。

我想要这张图片的结果

enter image description here

我接受四个输入坐标,并希望透视新图像。

但我的代码是不可能的......

这是我的代码:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Image<Bgr, byte> image = new Image<Bgr, byte>(@"C:\Users4.jpg");
        Bitmap bitImage;
        ImageConverter im = new ImageConverter();
        int w, h;   

        int count = 4;
        int a = 0;

        HomographyMatrix homography;

        PointF[] spoint = new PointF[4];
        PointF[] dpoint = new PointF[4];

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            bitImage = new Bitmap(@"C:\Users4.jpg");

            w = bitImage.Width;
            h = bitImage.Height;

            this.Size = new System.Drawing.Size(w, h);

            ibCanvas.BackgroundImage = bitImage;
        }

        /*
         When taking the mouse coordinates being the X and Y axes to the coordinates stored in the Point array.
         */
        private void ibCanvas_MouseDown(object sender, MouseEventArgs e)
        {
            if (count != 0)
            {
                spoint[a] = new PointF(e.X, e.Y);
                a++;
                count -= 1;
            }

            if (count == 0)
            {
                count = 4;
                a = 0;

                PointF[] pts1 = new PointF[4];
                PointF[] pts2 = new PointF[4];

                label1.Text = spoint[0].ToString();
                label2.Text = spoint[1].ToString();
                label3.Text = spoint[2].ToString();
                label4.Text = spoint[3].ToString();

                double w1 = Math.Sqrt(Math.Pow(spoint[3].X - spoint[0].X, 2)
                                    + Math.Pow(spoint[3].X - spoint[0].X, 2));
                double w2 = Math.Sqrt(Math.Pow(spoint[2].X - spoint[1].X, 2)
                                    + Math.Pow(spoint[2].X - spoint[1].X, 2));

                double h1 = Math.Sqrt(Math.Pow(spoint[3].Y - spoint[2].Y, 2)
                                    + Math.Pow(spoint[3].Y - spoint[2].Y, 2));
                double h2 = Math.Sqrt(Math.Pow(spoint[0].Y - spoint[1].Y, 2)
                                    + Math.Pow(spoint[0].Y - spoint[1].Y, 2));



                double maxWidth = (w1 < w2) ? w1 : w2;
                double maxHeight = (h1 < h2) ? h1 : h2;

                dpoint[0].X = 0;
                dpoint[0].Y = 0;

                dpoint[1].X = 0;
                dpoint[1].Y = ((float)maxHeight-1);

                dpoint[2].X = ((float)maxWidth - 1); ;
                dpoint[2].Y = ((float)maxHeight - 1); ;

                dpoint[3].X = ((float)maxWidth - 1); ;
                dpoint[3].Y = 0;

                homography = CameraCalibration.GetPerspectiveTransform(spoint, dpoint);
                Image<Bgr, byte> newImage = image.WarpPerspective(homography, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC, Emgu.CV.CvEnum.WARP.CV_WARP_DEFAULT, new Bgr(0, 0, 0));

                CvInvoke.cvShowImage("new Image", newImage);
            }
        }
    }
}

我想真正解决这个问题

请帮助我!

0 个答案:

没有答案