emgu cv 透视变换



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
namespace 透视变换
{
    public partial class Form1 : Form
    {
        static string filename;//文件图像
        //static string beijing;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                filename = open.FileName;
                test();
            }
        }
        private void test()
        {
            PointF[] srcTri = new PointF[4];//浮点型的坐标点,用于计算旋转矩阵
            PointF[] dstTri = new PointF[4];

            Matrix<float> warp_matrix = new Matrix<float>(3, 3);//旋转矩阵

            Image<Bgr, byte> src = new Image<Bgr, byte>(filename);

            pictureBox_src.Image = src.ToBitmap();
            Rectangle rc;
            rc = CvInvoke.cvGetImageROI(src);//获取图像的大小

            /
            float xx, yy, xx1, yy1;
            int uu;/左右选择/
            Random ran = new Random();
            int rand = ran.Next(0, 30);
            xx = (float)(1.0 * rand / 100);
            rand = ran.Next(0, 30);
            yy = (float)(1.0 * rand / 100);

            rand = ran.Next(0, 30);
            xx1 = (float)(1.0 * rand / 100);
            rand = ran.Next(0, 30);
            yy1 = (float)(1.0 * rand / 100);

            this.Text = xx.ToString() + "  " + yy.ToString() + "  " + xx1.ToString() + "  " + yy1.ToString();
            /
            uu = ran.Next(1, 100);

            Image<Bgr, byte> dst = new Image<Bgr, byte>(rc.Size);
            CvInvoke.cvZero(dst);
            srcTri[0].X = 0;          //src Top left
            srcTri[0].Y = 0;
            srcTri[1].X = src.Width - 1;    //src Top right
            srcTri[1].Y = 0;
            srcTri[2].X = 0;          //src Bottom left
            srcTri[2].Y = src.Height - 1;
            srcTri[3].X = src.Width - 1;  //src Bot right
            srcTri[3].Y = src.Height - 1;

            ///下面为两种变换,第一种为左上右下贴着图像,第二种为右上左下贴着图像
 ///我这里是随机生成的,也可以自己制定
            if (uu % 2 == 0)
            {
                dstTri[0].X = src.Width * 0;    //dst Top left
                dstTri[0].Y = src.Height * 0;
                dstTri[1].X = src.Width * (1.0f - xx); //dst Top right
                dstTri[1].Y = src.Height * yy;
                dstTri[2].X = src.Width * xx1; //dst Bottom left
                dstTri[2].Y = src.Height * (1.0f - yy1);
                dstTri[3].X = src.Width * 1.0f; //dst Bottom right
                dstTri[3].Y = src.Height * 1.0f;
            }
            else
            {
                dstTri[0].X = src.Width * yy;    //dst Top left
                dstTri[0].Y = src.Height * xx;
                dstTri[1].X = src.Width * 1.0f; //dst Top right
                dstTri[1].Y = src.Height * 0.0f;
                dstTri[2].X = src.Width * 0.0f; //dst Bottom left
                dstTri[2].Y = src.Height * 1.0f;
                dstTri[3].X = src.Width * (1.0f - xx1); //dst Bottom right
                dstTri[3].Y = src.Height * (1.0f - yy1);
            }
//
            MCvScalar mm = new MCvScalar();
            mm.v0 = 1;
            CvInvoke.cvGetPerspectiveTransform(srcTri, dstTri, warp_matrix);//获取旋转矩阵warp_matrix

            CvInvoke.cvWarpPerspective(src, dst, warp_matrix, 4, mm);//根据旋转矩阵进行透视变换
            pictureBox_dst.Image = dst.ToBitmap();


        }

    }
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值