OpenCV5-图像几何变换


1.图像连接

图像连接是指将相同高度或者宽度的图像连接在一起。

vconcat()函数用于实现图像或矩阵的上下连接:上下连接的矩阵应该具有相同的列数,并且具有相同的数据类型和通道数。

void vconcat(const Mat* src,  // Mat矩阵类型数组
             size_t nsrc,     // 数组数目
             OutputArray dst);// 连接后的Mat类矩阵


void vconcat(InputArray src1,  // 第一个需要连接的Mat类矩阵
             InputArray src2,  // 第二个需要连接的Mat类矩阵
             OutputArray dst); // 连接后的Mat类矩阵

hconcat()函数用于实现图像或者矩阵左右连接:左右连接的矩阵应该具有相同的行数,并且具有相同的数据类型和通道数。参数意义同vconcat函数。

void hconcat(const Mat* src, 
             size_t nsrc, 
             OutputArray dst);

void hconcat(InputArray src1, 
             InputArray src2, 
             OutputArray dst);

下面是示例代码:

#include <opencv2/core/utils/logger.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	cout << "OpenCV Version: " << CV_VERSION << endl;
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);

	//矩阵数组的横竖连接
	Mat matArray[] = { Mat(1, 2, CV_32FC1, cv::Scalar(1)),
		Mat(1, 2, CV_32FC1, cv::Scalar(2)) };
	Mat vout, hout;
	vconcat(matArray, 2, vout);
	cout << "图像数组竖向连接:" << endl << vout << endl;
	hconcat(matArray, 2, hout);
	cout << "图像数组横向连接:" << endl << hout << endl;

	//矩阵的横竖拼接
	Mat A = (cv::Mat_<float>(2, 2) << 1, 7, 2, 8);
	Mat B = (cv::Mat_<float>(2, 2) << 4, 10, 5, 11);
	Mat vC, hC;
	vconcat(A, B, vC);
	cout << "多个图像竖向连接:" << endl << vC << endl;
	hconcat(A, B, hC);
	cout << "多个图像横向连接:" << endl << hC << endl;

	//读取4个子图像,00表示左上角、01表示右上角、10表示左下角、11表示右下角
	Mat img00 = imread("lena00.png");
	Mat img01 = imread("lena01.png");
	Mat img10 = imread("lena10.png");
	Mat img11 = imread("lena11.png");
	if (img00.empty() || img01.empty() || img10.empty() || img11.empty())
	{
		cout << "请确认图像文件名称是否正确" << endl;
		return -1;
	}
	//显示4个子图像
	imshow("img00", img00);
	imshow("img01", img01);
	imshow("img10", img10);
	imshow("img11", img11);

	//图像连接
	Mat img, img0, img1;
	//图像横向连接
	hconcat(img00, img01, img0);
	hconcat(img10, img11, img1);
	//横向连接结果再进行竖向连接
	vconcat(img0, img1, img);

	//显示连接图像的结果
	imshow("img0", img0);
	imshow("img1", img1);
	imshow("img", img);

	int k = waitKey(0); // Wait for a keystroke in the window
	return 0;
}

2.图像尺寸变换

图像的尺寸变换实际上就是改变图像的长和宽,实现图像的缩放。resize()函数用于将图像修改成指定尺寸。

void resize(InputArray src,  // 输入图像
            OutputArray dst, // 输出图像
            Size dsize,      // 输出图像尺寸
            double fx = 0,   
            double fy = 0,
            int interpolation = INTER_LINEAR );

fx和fy分别为水平轴和垂直轴的比例因子,如果需要将水平轴或者垂直轴变为原来的两倍,则参数需赋值2。

dsize用来调整输出图像大小,fx和fy一起也可以调整输出图像,因此两类参数在实际使用时只需要使用一类,当根据两个参数计算出来的输出图像尺寸不一致时,以dsize设置的图像尺寸为准。这两类参数的关系式:

dsize = Size(round(fx * src.cols), round(fy * src.rows))

interpolation参数是选择图像插值方法的标志。如果要缩小图像,那么通常使用IMTER_AREA标志会有更好的效果。而在放大图像时,采用INTER_CUBIC和INTER_LINEAR标志通常会有比较好的效果,这两个标志,前者计算速度较慢但效果较好,后者计算速度较快效果也比较理想。

标志参数简记作用
INTER_NEAREST0最邻近插值法
INTER_LINEAR1双线性插值法
INTER_CUBIC2双三次插值
INTER_AREA3使用像素区域关系重新采样,首选用于图像缩小,图像放大时效果与INTER_NEAREST相似
INTER_LANCZOS44Lanczos插值法
INTER_LINEAR_EXACT5位精确双线性插值法
INTER_MAX6用掩码进行插值

下面示例代码以灰度图像形式读入一幅图像,利用INTER_AREA将图像缩小,分别用INTER_CUBIC、INTER_NEAREST、INTER_LINEAR三种方法将图像放大到相同的尺寸:

#include <opencv2/core/utils/logger.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	cout << "OpenCV Version: " << CV_VERSION << endl;
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);

	Mat gray = imread("lena.png", IMREAD_GRAYSCALE);
	if (gray.empty())
	{
		cout << "请确认图像文件名称是否正确" << endl;
		return -1;
	}
	Mat smallImg, bigImg0, bigImg1, bigImg2;
	resize(gray, smallImg, Size(15, 15), 0, 0, INTER_AREA);  //先将图像缩小
	resize(smallImg, bigImg0, Size(30, 30), 0, 0, INTER_NEAREST);  //最近邻差值
	resize(smallImg, bigImg1, Size(30, 30), 0, 0, INTER_LINEAR);  //双线性差值
	resize(smallImg, bigImg2, Size(30, 30), 0, 0, INTER_CUBIC);  //双三次差值
	namedWindow("smallImg", WINDOW_NORMAL);  //图像尺寸太小,一定要设置可以调节窗口大小标志
	imshow("smallImg", smallImg);
	namedWindow("bigImg0", WINDOW_NORMAL);
	imshow("bigImg0", bigImg0);
	namedWindow("bigImg1", WINDOW_NORMAL);
	imshow("bigImg1", bigImg1);
	namedWindow("bigImg2", WINDOW_NORMAL);
	imshow("bigImg2", bigImg2);

	int k = waitKey(0); // Wait for a keystroke in the window
	return 0;
}

3.图像翻转变换

OpenCV中提供了flip函数用于将图像翻转:

void flip(InputArray src,  // 输入图像
          OutputArray dst, // 输出图像
          int flipCode);   // 翻转方式标志

flipCode数值大于0表示绕y轴翻转,等于0表示绕x翻转,小于0表示绕两个轴翻转。

#include <opencv2/core/utils/logger.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	cout << "OpenCV Version: " << CV_VERSION << endl;
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);

	Mat img = imread("lena.png");
	if (img.empty())
	{
		cout << "请确认图像文件名称是否正确" << endl;
		return -1;
	}
	Mat img_x, img_y, img_xy;
	flip(img, img_x, 0);  //沿x轴对称
	flip(img, img_y, 1);  //沿y轴对称
	flip(img, img_xy, -1);  //先x轴对称,再y轴对称
	imshow("img", img);
	imshow("img_x", img_x);
	imshow("img_y", img_y);
	imshow("img_xy", img_xy);

	int k = waitKey(0); // Wait for a keystroke in the window
	return 0;
}

4.图像仿射变换

实现图像的旋转需要确定旋转角度和旋转中心(确定了旋转矩阵),然后进行仿射变换实现旋转。OpenCV提供了getRotationMatrix2D函数用于计算旋转矩阵,提供了warpAffine函数用于实现图像的仿射变换。

inline
Mat getRotationMatrix2D(Point2f center, // 旋转中心
                        double angle,   // 旋转角度,度为单位,正值为逆时针旋转
                        double scale);  // 两个轴的比例因子,实现旋转过程中的图像缩放,不缩放则为1

该函数返回图像旋转矩阵,返回值为Mat类是一个2x3的矩阵。
该函数生成的旋转矩阵与旋转角度和旋转中心的关系:
R o t a t i o n = [ α β ( 1 − α ) ∗ c e n t e r . x − β ∗ c e n t e r . y − β α β ∗ c e n t e r . x + ( 1 − α ) ∗ c e n t e r . y ] Rotation = \begin{bmatrix} \alpha & \beta & (1-\alpha)*center.x - \beta*center.y \\ -\beta & \alpha & \beta*center.x+(1-\alpha)*center.y \end{bmatrix} Rotation=[αββα(1α)center.xβcenter.yβcenter.x+(1α)center.y]
其中
α = s c a l e ∗ c o s ( a n g l e ) β = s c a l e ∗ s i n ( a n g l e ) \alpha = scale * cos(angle) \\ \beta = scale * sin(angle) α=scalecos(angle)β=scalesin(angle)

// 根据旋转矩阵,进行仿射变换
void warpAffine(
    InputArray src,  // 输入图像
    OutputArray dst, // 仿射变换后的输出图像
    InputArray M,    // 2x3的变换矩阵
    Size dsize,      // 输出图像尺寸
    int flags = INTER_LINEAR,  // 插值方法标志
    int borderMode = BORDER_CONSTANT,      // 像素边界外推方法标志
    const Scalar& borderValue = Scalar()); // 填充边界使用的值,默认为0

/*
M:前面求取的旋转矩阵
dsize:输出图像尺寸
flags:插值方法标志,同尺寸变换resize时的插值方法标志,并且多了两个可以和前面几个标志一起使用
WARP_FILL_OUTLIERS:填充所有输出图像的像素,如果部分像素落在输入图像的边界外,则他们的值设定为fillval
WARP_INVERSE_MAP:设置为M输出图像到输入图像的反变换。
*/
enum InterpolationFlags{
    /** nearest neighbor interpolation */
    INTER_NEAREST        = 0,
    /** bilinear interpolation */
    INTER_LINEAR         = 1,
    
    // ...
    /** flag, fills all of the destination image pixels. If some of them correspond to outliers in the
    source image, they are set to zero */
    WARP_FILL_OUTLIERS   = 8,
    /** flag, inverse transformation

    For example, #linearPolar or #logPolar transforms:
    - flag is __not__ set: \f$dst( \rho , \phi ) = src(x,y)\f$
    - flag is set: \f$dst(x,y) = src( \rho , \phi )\f$
    */
    WARP_INVERSE_MAP     = 16
};

/*
第六个参数borderMode为像素边界外推标志:
BORDER_CONSTANT:用特定值填充,如用i填充:iiiiii|abcdefgh|iiiiiii
BORDER_REPLICATE:两端复制填充,如两端用a和h填充:aaaaaa|abcdefgh|hhhhhhh
BORDER_REFLECT:倒序填充
BORDER_WRAP:正序填充
BORDER_REFLECT_101:不包含边界值的倒序填充
BORDER_TRANSPARENT:随即填充
BORDER_REFLECT101、BORDER_DEFAULT:同BORDER_REFLECT_101
BORDER_ISOLATED:不关心感兴趣区域之外的部分
*/
enum BorderTypes {
    BORDER_CONSTANT    = 0, //!< `iiiiii|abcdefgh|iiiiiii`  with some specified `i`
    BORDER_REPLICATE   = 1, //!< `aaaaaa|abcdefgh|hhhhhhh`
    BORDER_REFLECT     = 2, //!< `fedcba|abcdefgh|hgfedcb`
    BORDER_WRAP        = 3, //!< `cdefgh|abcdefgh|abcdefg`
    BORDER_REFLECT_101 = 4, //!< `gfedcb|abcdefgh|gfedcba`
    BORDER_TRANSPARENT = 5, //!< `uvwxyz|abcdefgh|ijklmno`

    BORDER_REFLECT101  = BORDER_REFLECT_101, //!< same as BORDER_REFLECT_101
    BORDER_DEFAULT     = BORDER_REFLECT_101, //!< same as BORDER_REFLECT_101
    BORDER_ISOLATED    = 16 //!< do not look outside of ROI
};

仿射变换就是图像的旋转、平移和缩放的统称,可以表示为线性变换和平移变换的叠加。仿射变换的数学表示是先乘以一个线性变换矩阵再加上一个平移向量,其中线性变换矩阵为2x2,平移向量为2x1。
M = [ A B ] = [ a 00 a 01 b 00 a 10 a 11 b 10 ] M = \begin{bmatrix} A & B \end{bmatrix} = \begin{bmatrix} a_{00} & a_{01} & b_{00} \\ a_{10} & a_{11} & b_{10} \end{bmatrix} M=[AB]=[a00a10a01a11b00b10]
根据线性变换矩阵A和平移矩阵B,以及图像像素值 [ x y ] \begin{bmatrix}x \\ y\end{bmatrix} [xy],放射变换的数学原理: T = A [ x y ] + B T = A\begin{bmatrix}x \\ y\end{bmatrix} + B T=A[xy]+B,其中T为变换后的像素值。

仿射变换又称为三点变换。如果知道变换前后两幅图像中3个像素点坐标的对应关系,就可以求得仿射变换中的变换矩阵M。OpenCV中也提供了利用3个对应像素点来确定变换矩阵M的函数getAffineTransform():

Mat getAffineTransform(
    const Point2f src[], // 源图像中的3个像素点坐标
    const Point2f dst[]  // 目标图像中的3个像素点坐标
);
/*
函数返回值是2x3变换矩阵M。
*/
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	cout << "OpenCV Version: " << CV_VERSION << endl;
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);

	Mat img = imread("lena.png");
	if (img.empty())
	{
		cout << "请确认图像文件名称是否正确" << endl;
		return -1;
	}
	Mat rotation0, rotation1, img_warp0, img_warp1;
	double angle = 30;  //设置图像旋转的角度
	Size dst_size(img.rows, img.cols);  //设置输出图像的尺寸
	Point2f center(img.rows / 2.0, img.cols / 2.0);  //设置图像的旋转中心
	rotation0 = getRotationMatrix2D(center, angle, 1);  //计算放射变换矩阵
	warpAffine(img, img_warp0, rotation0, dst_size);  //进行仿射变换
	imshow("img_warp0", img_warp0);

	//根据定义的三个点进行仿射变换
	Point2f src_points[3];
	Point2f dst_points[3];
	src_points[0] = Point2f(0, 0);  //原始图像中的三个点
	src_points[1] = Point2f(0, (float)(img.cols - 1));
	src_points[2] = Point2f((float)(img.rows - 1), (float)(img.cols - 1));
	dst_points[0] = Point2f((float)(img.rows) * 0.11, (float)(img.cols) * 0.20);  //放射变换后图像中的三个点
	dst_points[1] = Point2f((float)(img.rows) * 0.15, (float)(img.cols) * 0.70);
	dst_points[2] = Point2f((float)(img.rows) * 0.81, (float)(img.cols) * 0.85);
	rotation1 = getAffineTransform(src_points, dst_points);  //根据对应点求取仿射变换矩阵
	warpAffine(img, img_warp1, rotation1, dst_size);  //进行仿射变换
	imshow("img_warp1", img_warp1);

	int k = waitKey(0); // Wait for a keystroke in the window
	return 0;
}

5.图像透视变换

透视变换是按照物体成像投影规律进行变换,即将物体重新投影到新的成像平面。通常通过透视变换实现对物体图像的校正。在透视变换中,透视前的图像和透视后的图像之间的变换关系可以用3x3的变换矩阵表示,该矩阵可以通过两幅图像中4个对应点的坐标求取,OpenCV中提供了根据4个对应点求取变换矩阵的getPerspectiveTransform函数和进行透视变换的warpPerspective函数:

Mat getPerspectiveTransform(
    const Point2f src[], // 原图像中的4个像素坐标
    const Point2f dst[], // 目标图像中的4个像素坐标
    int solveMethod = DECOMP_LU // 根据对应点坐标计算透视变换矩阵的选择标志
);

/*
DECOMP_LU:最佳主轴元素的高斯消元法
DECOMP_SVD:奇异值分解方法
DECOMP_EIG:特征值分解
DECOMP_CHOLESKY:Cholesky分解法
DECOMP_NORMAL:使用正规方程公式,可以和其他标志一起使用。
*/

enum DecompTypes {
    /** Gaussian elimination with the optimal pivot element chosen. */
    DECOMP_LU       = 0,
    /** singular value decomposition (SVD) method; the system can be over-defined and/or the matrix
    src1 can be singular */
    DECOMP_SVD      = 1,
    /** eigenvalue decomposition; the matrix src1 must be symmetrical */
    DECOMP_EIG      = 2,
    /** Cholesky \f$LL^T\f$ factorization; the matrix src1 must be symmetrical and positively
    defined */
    DECOMP_CHOLESKY:Cholesky分解法
        = 3,
    /** QR factorization; the system can be over-defined and/or the matrix src1 can be singular */
    DECOMP_QR       = 4,
    /** while all the previous flags are mutually exclusive, this flag can be used together with
    any of the previous; it means that the normal equations
    \f$\texttt{src1}^T\cdot\texttt{src1}\cdot\texttt{dst}=\texttt{src1}^T\texttt{src2}\f$ are
    solved instead of the original system
    \f$\texttt{src1}\cdot\texttt{dst}=\texttt{src2}\f$ */
    DECOMP_NORMAL   = 16
};
void warpPerspective(
    InputArray src,  // 输入图像
    OutputArray dst, // 透视变换后图像
    InputArray M,    // 透视变换矩阵3x3
    Size dsize,      // 输出图像的尺寸
    int flags = INTER_LINEAR, // 插值方法标志
    int borderMode = BORDER_CONSTANT, // 像素边界外推方法标志
    const Scalar& borderValue = Scalar()); // 填充边界使用的数值,默认情况下为0

warpPerspective函数所有参数含义同仿射变换函数warpAffine()。

下面例子给出了将相机视线不垂直于二维码平面拍摄的图像经过透视变换变成相机视线垂直于二维码平面拍摄的图像。本程序中实现通过ImageWatch插件查看了拍摄图像二维码4个角点的坐标,并希望透视变换后二维码可以填满全部的图像,因此在程序中手动输入4个对应点的像素坐标。但在实际的工程中,二维码的角点坐标可以通过角点检测的方式获取。

#include <opencv2/core/utils/logger.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	cout << "OpenCV Version: " << CV_VERSION << endl;
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);

	Mat img = imread("noobcvqr.png");
	if (img.empty())
	{
		cout << "请确认图像文件名称是否正确" << endl;
		return -1;
	}
	Point2f src_points[4];
	Point2f dst_points[4];
	//通过Image Watch查看的二维码四个角点坐标
	src_points[0] = Point2f(94.0, 374.0);
	src_points[1] = Point2f(507.0, 380.0);
	src_points[2] = Point2f(1.0, 623.0);
	src_points[3] = Point2f(627.0, 627.0);
	//期望透视变换后二维码四个角点的坐标
	dst_points[0] = Point2f(0.0, 0.0);
	dst_points[1] = Point2f(627.0, 0.0);
	dst_points[2] = Point2f(0.0, 627.0);
	dst_points[3] = Point2f(627.0, 627.0);
	Mat rotation, img_warp;
	rotation = getPerspectiveTransform(src_points, dst_points);  //计算透视变换矩阵
	warpPerspective(img, img_warp, rotation, img.size());  //透视变换投影
	imshow("img", img);
	imshow("img_warp", img_warp);

	int k = waitKey(0); // Wait for a keystroke in the window
	return 0;
}

6.极坐标变换

极坐标变换就是将图像在直角坐标系与极坐标系中相互变换。

可以将一个圆形图像变换成一个矩形图像,圆形图案边缘上的文字经过极坐标变换后可以垂直地排列在新图像的边缘。

OpenCV中提供了warpPolar函数用于实现图像的极坐标变换:

void warpPolar(
    InputArray src,   // 源图像,可以是灰度图或彩色图
    OutputArray dst,  // 极坐标变换后输出图像
    Size dsize,       // 目标图像大小
    Point2f center,   // 极坐标变换时,极坐标的原点坐标
    double maxRadius, // 变换时的边界园半径,它也决定了逆变换时的比例参数
    int flags); // 插值方法与极坐标映射方法标志 |连接

/*
该函数实现了图像极坐标和半对数极坐标变换。
center:极坐标变换时极坐标远点在原始图像中的位置,同样适用于逆变换中。
flags:插值方法如INTER_NEAREST,极坐标映射方法如下
WARP_POLAR_LINEAR:极坐标变换
WARP_POLAR_LOG:半对数极坐标变换
WARP_INVERSE_MAP:逆变换
*/
enum WarpPolarMode
{
    WARP_POLAR_LINEAR = 0, ///< Remaps an image to/from polar space.
    WARP_POLAR_LOG = 256   ///< Remaps an image to/from semilog-polar space.
};

下面的例子给出了对仪表盘图像进行极坐标正变换和逆变换的示例程序:程序中选择表盘的中心作为极坐标原点

#include <opencv2/core/utils/logger.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	cout << "OpenCV Version: " << CV_VERSION << endl;
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);

	Mat img = imread("dial.png");
	if (!img.data)
	{
		cout << "请检查图像文件名称是否输入正确" << endl;
		return -1;
	}

	Mat img1, img2;
	Point2f center = Point2f(img.cols / 2, img.rows / 2);  //极坐标在图像中的原点
	//正极坐标变换
	warpPolar(img, img1, Size(300, 600), center, center.x, INTER_LINEAR + WARP_POLAR_LINEAR);
	//逆极坐标变换
	warpPolar(img1, img2, Size(img.rows, img.cols), center, center.x, INTER_LINEAR + WARP_POLAR_LINEAR + WARP_INVERSE_MAP);

	imshow("原表盘图", img);
	imshow("表盘极坐标变换结果", img1);
	imshow("逆变换结果", img2);

	int k = waitKey(0); // Wait for a keystroke in the window
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shlyyy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值