赞
踩
仿射变换是一种线性变换,它将一个二维或三维空间中的对象(如点、图像、图形等)映射到另一个空间中,保持了一些基本的几何性质,如直线的平行性和点的共线性。仿射变换通常由矩阵乘法和向量加法组成,其一般形式如下:
其中,是映射矩阵。
- void cv::warpAffine(
- InputArray src,
- OutputArray dst,
- InputArray M,
- Size dsize,
- int flags = INTER_LINEAR,
- int borderMode = BORDER_CONSTANT,
- const Scalar & borderValue = Scalar()
- )
函数warpAffine对图像应用仿射变换,使用指定的矩阵变换源图像:
src:输入图像
dst:输出图像,大小为dsize,类型与src相同
M:2×3变换矩阵
dsize:输出图像的大小
flags:插值方法
bordermode:像素外推法
bordervalue:边界不变时使用的值,默认为0
INTER_NEAREST:最近邻插值
INTER_LINEAR :双线性插值
INTER_CUBIC :双三次插值
......
BORDER_CONSTANT:常数边界,图像边界的像素被填充成一个常数值
BORDER_REPLICATE:复制边界,图像的边界像素会被复制,也就是用最近边界上的像素值来填充边界
BORDER_REFLECT:反射边界,图像的边界像素被镜像反射,也就是边界上的像素值会像镜子一样反射到图像外部
BORDER_WRAP:环绕边界,图像的边界像素被视为环绕的,也就是当你越过图像边界时,会从另一侧的边界开始计算像素值
......
getRotationMatrix2D()
- Mat cv::getRotationMatrix2D(
- Point2f center,
- double angle,
- double scale
- )
center:源图像中旋转的中心
angle:以度为单位的旋转角度,正值表示逆时针旋转
sacle:尺度因子
getAffineTransform()
- Mat cv::getAffineTransform(
- const Point2f src[],
- const Point2f dst[]
- )
src:源图像中三角形顶点的坐标
dst:目标图像中对应三角形顶点的坐标
- # include <iostream>
- # include <opencv2/opencv.hpp>
-
- using namespace std;
- using namespace cv;
-
- int main()
- {
- Mat img = imread("lena.jpg");
-
- // 定义用于旋转的变量
- Mat rotation0, img_warp0, img_warp1;
-
- // 设置旋转角度为45度
- double angle = 45;
-
- // 创建目标图像的尺寸
- Size dst_size(img.rows, img.cols); // cv::Size_<int>
-
- // 定义旋转中心点
- Point2f center(img.rows / 2.0, img.cols / 2.0); // cv::Point_<float>
-
- // 获取旋转矩阵,其中参数分别为旋转中心、旋转角度、缩放因子
- rotation0 = getRotationMatrix2D(center, angle, 1);
-
- // 对图像进行仿射变换,采用线性插值,边界填充方式为常数填充和反射填充
- warpAffine(img, img_warp0, rotation0, dst_size, INTER_LINEAR, BORDER_CONSTANT);
- warpAffine(img, img_warp1, rotation0, dst_size, INTER_LINEAR, BORDER_REFLECT);
-
- // 根据定义的三个点进行仿射变换
- Point2f src_points[3];
- Point2f dst_points[3];
-
- // 定义原始图像中的三个点
- src_points[0] = Point2f(0, 0);
- src_points[1] = Point2f(0, (float)(img.rows - 1));
- src_points[2] = Point2f((float)(img.cols - 1), (float)(img.rows - 1));
-
- // 定义目标图像中的三个点
- dst_points[0] = Point2f((float)(0, 0));
- dst_points[1] = Point2f((float)(img.cols - 1) * 0.1, (float)(img.rows - 1) * 0.5);
- dst_points[2] = Point2f((float)(img.cols - 1) * 0.8, (float)(img.rows - 1) * 0.8);
-
- // 创建仿射变换矩阵
- Mat rotation1, img_warp2;
- rotation1 = getAffineTransform(src_points, dst_points);
-
- // 对图像进行仿射变换
- warpAffine(img, img_warp2, rotation1, dst_size);
-
- // 显示原始图像和三个处理后的图像
- imshow("img", img);
- imshow("img_warp0", img_warp0);
- imshow("img_warp1", img_warp1);
- imshow("img_warp2", img_warp2);
-
- waitKey(0);
- }
原图 旋转45度(黑色填充)
旋转45度(反射填充) 三点仿射变换
透视变换是一种线性变换,用于将图像从一种视角或透视变换到另一种视角。这通常用于校正图像中的透视畸变,例如将斜切的文档图像变换成正常的矩形形状,或者将一个平面上的物体投影到另一个平面上。一般公式如下:
其中,是映射矩阵。
函数warpPerspective对图像应用透视转换,使用指定的矩阵变换源图像:
src:输入图像
dst:输出图像,大小为dsize,类型与src相同
M:3×3变换矩阵
dsize:输出图像的大小
flags:插值方法
bordermode:像素外推法(BORDER_CONSTANT或border_replication)。
bordervalue:边界不变时使用的值,默认为0
getPerspectiveTransform()
- Mat cv::getPerspectiveTransform(
- const Point2f src[],
- const Point2f dst[],
- int solveMethod = DECOMP_LU
- )
src:源图像中四个点的坐标
dst:目标图像中对应四个点的坐标
solveMethod:不用填
- # include <iostream>
- # include <opencv2/opencv.hpp>
-
- using namespace std;
- using namespace cv;
-
- int main()
- {
- Mat img = imread("test");
- Mat rotation0, img_warp0, img_warp1;
-
- Point2f src_points[4];
- Point2f dst_points[4];
-
- // 获取棋盘格的四个角点
- src_points[0] = Point2f(205, 107);
- src_points[1] = Point2f(472, 43);
- src_points[2] = Point2f(238, 440);
- src_points[3] = Point2f(607, 350);
-
- // 将这四点映射到
- dst_points[0] = Point2f(0, 0);
- dst_points[1] = Point2f(700, 0);
- dst_points[2] = Point2f(0, 800);
- dst_points[3] = Point2f(700, 800);
-
- Size dst_size(700, 800);
-
- Mat rotation, img_warp;
-
- // 得到映射矩阵
- rotation = getPerspectiveTransform(src_points, dst_points);
-
- warpPerspective(img, img_warp, rotation, dst_size);
-
- imshow("img", img);
- imshow("img_warp", img_warp);
-
- waitKey(0);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。