OpenCV Tutorials —— Affine Transformations

仿射变换

 

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 matrix.

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

    or

 

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 and T and we also know that they are related. Then our job is to find

2,We know and . To obtain we only need to apply . Our information for 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;
}
时间: 2024-07-30 19:31:36

OpenCV Tutorials —— Affine Transformations的相关文章

学习opencv tutorials

1.opencv里头动态库和静态库的区别 lib是动态库,staticlib是静态库. 这是opencv tutorials中对动态库和静态库的说明.动态库是在runtime时候才load的库文件.而静态库文件会在你build的时候build-in inside your exe file.优点是可以避免误删,缺点是应用程序变大,加载时间也会变长. 2.  Visual Studio中solution和project的关系 在VS中,一个solution中可以包含多个project. 3.  两

OpenCV笔记(二十)——Affine Transformations仿射变换

仿射变换的作用是将图像做旋转.拉伸. 仿射变换是通过一个中间矩阵来使源图像像素的位置变换到指定的目标图像的像素的位置,原理类似于上文的remapping. 所以仿射变换也是矩阵的一种运用. 于是仿射变换一般分成两步:第一.寻找变换的中间矩阵:第二.进行变换. 要找到变换的中间矩阵,一般使用三个点来寻找它,因为用三角形我们可以表现出变换的尺度和角度.在OpenCV中,我们使用getAffineTransform. 进行变换时,我们使用warpAffine函数. Mat getAffineTrans

OpenCV Tutorials —— Transformations

Explanation Create a visualization window. Get camera pose from camera position, camera focal point and y direction. Obtain transform matrix knowing the axes of camera coordinate system. Create a cloud widget from bunny.ply file Given the pose in cam

OpenCV Tutorials &mdash;&mdash; Hough Line Transform

霍夫直线变换 -- 用于检测图像中的直线 利用图像空间和Hough参数空间的点--直线对偶性,把图像空间中的检测问题转换到参数空间,通过在参数空间进行简单的累加统计,然后在Hough参数空间中寻找累加器峰值的方法检测直线 Standard and Probabilistic Hough Line Transform OpenCV implements two kind of Hough Line Transforms: The Standard Hough Transform It consis

OpenCV Tutorials &mdash;&mdash; Feature Matching with FLANN

Extractors of keypoint descriptors in OpenCV have wrappers with a common interface that enables you to easily switch between different algorithms solving the same problem.   DescriptorExtractor::compute Computes the descriptors for a set of keypoints

OpenCV Tutorials &mdash;&mdash; Creating a video with OpenCV

写video 需要用到 VideoWriter  视频文件可看作一个容器 视频的类型由视频文件的后缀名来指定   Due to this OpenCV for video containers supports only the avi extension, its first version. A direct limitation of this is that you cannot save a video file larger than 2 GB. Furthermore you ca

OpenCV Tutorials &mdash;&mdash; Image Moments

图像矩 Moments moments(InputArray array, bool binaryImage=false ) Parameters: array – Raster image (single-channel, 8-bit or floating-point 2D array) or an array ( or ) of 2D points (Point or Point2f ). binaryImage – If it is true, all non-zero image pi

OpenCV Tutorials &mdash;&mdash; Hough Circle Transform

Hough 圆变换 和 Hough 直线变换原理相同,只是参数空间不同 : In the line detection case, a line was defined by two parameters . In the circle case, we need three parameters to define a circle: where define the center position (gree point) and is the radius, which allows us

OpenCV Tutorials &mdash;&mdash; Sobel Derivatives

图像边缘 -- 像素灰度值变换剧烈的点 You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to expresschanges is by using derivatives. A high change in gradient indicates a major change in the image.   To be more graphical,