三门峡市建设局网站,网站界面设计描述,广州软件开发招聘,二维码生成器网站OpenCV5-图像几何变换 1.图像连接2.图像尺寸变换3.图像翻转变换4.图像仿射变换5.图像透视变换6.极坐标变换 1.图像连接
图像连接是指将相同高度或者宽度的图像连接在一起。
vconcat()函数用于实现图像或矩阵的上下连接#xff1a;上下连接的矩阵应该具有相同的列数#xff… OpenCV5-图像几何变换 1.图像连接2.图像尺寸变换3.图像翻转变换4.图像仿射变换5.图像透视变换6.极坐标变换 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.hppusing 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 windowreturn 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.hppusing 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 windowreturn 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.hppusing 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 windowreturn 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) αscale∗cos(angle)βscale∗sin(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 thesource image, they are set to zero */WARP_FILL_OUTLIERS 8,/** flag, inverse transformationFor 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 iBORDER_REPLICATE 1, //! aaaaaa|abcdefgh|hhhhhhhBORDER_REFLECT 2, //! fedcba|abcdefgh|hgfedcbBORDER_WRAP 3, //! cdefgh|abcdefgh|abcdefgBORDER_REFLECT_101 4, //! gfedcb|abcdefgh|gfedcbaBORDER_TRANSPARENT 5, //! uvwxyz|abcdefgh|ijklmnoBORDER_REFLECT101 BORDER_REFLECT_101, //! same as BORDER_REFLECT_101BORDER_DEFAULT BORDER_REFLECT_101, //! same as BORDER_REFLECT_101BORDER_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 TA[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.hppusing 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 windowreturn 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_CHOLESKYCholesky分解法
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 matrixsrc1 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 positivelydefined */DECOMP_CHOLESKYCholesky分解法 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 withany 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$ aresolved 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, // 透视变换矩阵3x3Size dsize, // 输出图像的尺寸int flags INTER_LINEAR, // 插值方法标志int borderMode BORDER_CONSTANT, // 像素边界外推方法标志const Scalar borderValue Scalar()); // 填充边界使用的数值默认情况下为0warpPerspective函数所有参数含义同仿射变换函数warpAffine()。
下面例子给出了将相机视线不垂直于二维码平面拍摄的图像经过透视变换变成相机视线垂直于二维码平面拍摄的图像。本程序中实现通过ImageWatch插件查看了拍摄图像二维码4个角点的坐标并希望透视变换后二维码可以填满全部的图像因此在程序中手动输入4个对应点的像素坐标。但在实际的工程中二维码的角点坐标可以通过角点检测的方式获取。
#include opencv2/core/utils/logger.hpp
#include opencv2/opencv.hppusing 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 windowreturn 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.hppusing 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 windowreturn 0;
}