码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV Tutorials —— Affine Transformations

时间:2014-11-21 18:03:05      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   os   sp   for   strong   on   

仿射变换

 

Affine Transformation

  1. 1,It is any transformation that can be expressed in the form of a matrix multiplication (linear transformation) followed by a vector addition (translation).

  2. 2,From the above, We can use an Affine Transformation to express:

    1. Rotations (linear transformation)   旋转
    2. Translations (vector addition)   平移
    3. Scale operations (linear transformation)  缩放

    you can see that, in essence, an Affine Transformation represents a relation between two images.

  3. 3,The usual way to represent an Affine Transform is by using a bubuko.com,布布扣 matrix.

    bubuko.com,布布扣

    Considering that we want to transform a 2D vector bubuko.com,布布扣 by using bubuko.com,布布扣 and bubuko.com,布布扣, we can do it equivalently with:

    bubuko.com,布布扣 or bubuko.com,布布扣

    bubuko.com,布布扣

 

We mentioned that an Affine Transformation is basically a relation between two images. The information about this relation can come, roughly, in two ways:

  1. 1,We know both bubuko.com,布布扣 and T and we also know that they are related. Then our job is to find bubuko.com,布布扣

2,We know bubuko.com,布布扣 and bubuko.com,布布扣. To obtain bubuko.com,布布扣 we only need to apply bubuko.com,布布扣. Our information for bubuko.com,布布扣 may be explicit (i.e. have the 2-by-3 matrix) or it can come as a geometric relation between points.

通过参考点找到映射关系 M

对整幅图像应用 M,得到目标图像

 

warp_mat = getAffineTransform( srcTri, dstTri );

 

warpAffine( src, warp_dst, warp_mat, warp_dst.size() );

 

rot_mat = getRotationMatrix2D( center, angle, scale );

 

Code

#include "stdafx.h"

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

/// Global variables
char* source_window = "Source image";
char* warp_window = "Warp";
char* warp_rotate_window = "Warp + Rotate";

/** @function main */
int main( int argc, char** argv )
{
	Point2f srcTri[3];
	Point2f dstTri[3];

	Mat rot_mat( 2, 3, CV_32FC1 );
	Mat warp_mat( 2, 3, CV_32FC1 );
	Mat src, warp_dst, warp_rotate_dst;

	/// Load the image
	src = imread( "img2.jpg", 1 );

	/// Set the dst image the same type and size as src
	warp_dst = Mat::zeros( src.rows, src.cols, src.type() );

	/// Set your 3 points to calculate the  Affine Transform
	srcTri[0] = Point2f( 0,0 );
	srcTri[1] = Point2f( src.cols - 1, 0 );
	srcTri[2] = Point2f( 0, src.rows - 1 );

	dstTri[0] = Point2f( src.cols*0.0, src.rows*0.33 );
	dstTri[1] = Point2f( src.cols*0.85, src.rows*0.25 );
	dstTri[2] = Point2f( src.cols*0.15, src.rows*0.7 );

	/// Get the Affine Transform
	warp_mat = getAffineTransform( srcTri, dstTri );	// 通过参考点对应关系获得仿射矩阵

	/// Apply the Affine Transform just found to the src image
	warpAffine( src, warp_dst, warp_mat, warp_dst.size() );	// 应用仿射变换

	/** Rotating the image after Warp */

	/// Compute a rotation matrix with respect to the center of the image
	Point center = Point( warp_dst.cols/2, warp_dst.rows/2 );
	double angle = -50.0;
	double scale = 0.6;

	/// Get the rotation matrix with the specifications above
	rot_mat = getRotationMatrix2D( center, angle, scale );	// 通过中心点,偏角,尺度来获得旋转矩阵

	/// Rotate the warped image
	warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() );	// 应用仿射变换

	/// Show what you got
	namedWindow( source_window, CV_WINDOW_AUTOSIZE );
	imshow( source_window, src );

	namedWindow( warp_window, CV_WINDOW_AUTOSIZE );
	imshow( warp_window, warp_dst );

	namedWindow( warp_rotate_window, CV_WINDOW_AUTOSIZE );
	imshow( warp_rotate_window, warp_rotate_dst );

	/// Wait until user exits the program
	waitKey(0);

	return 0;
}

OpenCV Tutorials —— Affine Transformations

标签:style   http   io   ar   os   sp   for   strong   on   

原文地址:http://www.cnblogs.com/sprint1989/p/4113317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!