所謂的透視變換,就是利用透視中心、像點、目標點共線的條件,按透視旋轉定律使承影片 ( 透視面 ) 繞積線 (透視軸) 旋轉某一角度,破壞原有的投影光線束,仍能保持投影面上投影幾何圖形不變的變換。
透視變換常用於圖像的校正,例如在移動機器人視覺導航研究中,由於攝影機與地面之間有一個傾斜角,而不是直接垂直朝下 ( 正投影 ),有時希望將圖像校正成正投影的形式,就需要利用透視變換。
private void button19_Click(object sender, EventArgs e)
{
// 透視變換 (Perspective Transformation)
var op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
{
Mat scr_mat = new Mat(op.FileName);
Mat dst_mat = new Mat();
// 創建用於獲取透視的四個點座標
PointF [] scr = new PointF[]
{
new PointF(0,0),
new PointF(scr_mat.Width,0),
new PointF(0,scr_mat.Height),
new PointF(scr_mat.Width,scr_mat.Height)
};
// 創建用於取得透視的四個點座標,這四個點座標與之前的座標順序到序
// { 1,2,3,4 } => { 4,3,2,1 }
#region 上下翻轉
/*
PointF[] dst = new PointF[]
{
new PointF (scr_mat.Width,scr_mat.Height),
new PointF (0,scr_mat.Height),
new PointF(scr_mat.Width,0),
new PointF(0,0)
};
* */
#endregion
PointF[] dst = new PointF[]
{
new PointF (scr_mat.Width-300 ,scr_mat.Height -100),
new PointF (140 ,scr_mat.Height -120),
new PointF(scr_mat.Width -123,99),
new PointF(230,180)
};
Console.WriteLine("[SCR]W: {0} , H:{1}",
scr_mat.Width, scr_mat.Height);
Console.WriteLine("[DST]W: {0} , H:{1}",
dst_mat.Width, dst_mat.Height);
Mat data = CvInvoke.GetPerspectiveTransform(scr, dst);
CvInvoke.WarpPerspective(scr_mat, dst_mat, data, scr_mat.Size);
imageBox1.Image = scr_mat;
imageBox2.Image = dst_mat;
}
}