OpenCV几何图像变换(9)仿射变换函数warpAffine()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

函数是应用一个仿射变换到图像上。

warpAffine 函数使用指定的矩阵对源图像进行仿射变换:
dst ( x , y ) = src ( M 11 x + M 12 y + M 13 , M 21 x + M 22 y + M 23 ) \texttt{dst} (x,y) = \texttt{src} ( \texttt{M} _{11} x + \texttt{M} _{12} y + \texttt{M} _{13}, \texttt{M} _{21} x + \texttt{M} _{22} y + \texttt{M} _{23}) dst(x,y)=src(M11x+M12y+M13,M21x+M22y+M23)

当设置了标志 WARP_INVERSE_MAP 时。否则,先使用 invertAffineTransform 函数对变换进行反转,然后将反转后的矩阵放入上述公式中代替 M。该函数不能原地操作

warpAffine()函数用于应用仿射变换到图像上。仿射变换是一种线性变换,它可以包括旋转、缩放、平移和错切(shear)等操作。

函数原型

cpp 复制代码
void cv::warpAffine	
(
	InputArray 	src,
	OutputArray 	dst,
	InputArray 	M,
	Size 	dsize,
	int 	flags = INTER_LINEAR,
	int 	borderMode = BORDER_CONSTANT,
	const Scalar & 	borderValue = Scalar() 
)		

参数

  • 参数src 输入图像。
  • 参数dst 输出图像,它具有 dsize 的大小和与 src 相同的类型。
  • 参数M 2×3的变换矩阵。
  • 参数dsize 输出图像的大小。
  • 参数flags 插值方法的组合(参见 InterpolationFlags)和可选标志 WARP_INVERSE_MAP,该标志表示 M 是逆变换(dst→src)。
  • 参数borderMode 像素外推方法(参见 BorderTypes);当 borderMode=BORDER_TRANSPARENT 时,意味着目标图像中对应于源图像中的"异常值"的像素不会被函数修改。
  • 参数borderValue 在存在常数边界时所使用的值;默认情况下,它是 0。

代码示例

cpp 复制代码
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;

int main( int argc, char** argv )
{
    // 读取图像
    Mat image = imread( "/media/dingxin/data/study/OpenCV/sources/images/fruit_small.jpg", IMREAD_COLOR );

    if ( image.empty() )
    {
        std::cerr << "Error: Could not open or find the image." << std::endl;
        return -1;
    }

     // 获取图像的尺寸
    int cols = image.cols;
    int rows = image.rows;
    
    // 计算旋转中心点
    Point2f center(cols / 2.0, rows / 2.0);
    
    // 设置旋转角度和缩放因子
    double angle = 45.0; // 旋转角度
    double scale = 1.0;  // 缩放因子
    
    // 计算旋转矩阵
    Mat rotationMatrix = getRotationMatrix2D(center, angle, scale);
    
    // 获取输出图像的大小
    Size dsize(cols, rows);
    
    // 创建输出图像
    Mat rotatedImage;
    
    // 应用仿射变换
    warpAffine(image, rotatedImage, rotationMatrix, dsize, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));


    // ===错切===

    // 设置错切系数
    double shearX = 0.5; // 水平方向错切系数
    double shearY = 0.5; // 垂直方向错切系数
    
    // 计算错切矩阵
    Mat shearMatrix = (Mat_<double>(2,3) << 1, shearX, 0, shearY, 1, 0);
    
    // 创建输出图像
    Mat shearedImage;
    
    // 设置输出图像的大小
    Size dsize2(image.cols, image.rows);
    
    // 应用仿射变换
    warpAffine(image, shearedImage, shearMatrix, dsize2, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));

    // 平移

       // 设置平移距离
    int dx = 100; // 水平方向平移距离
    int dy = 50;  // 垂直方向平移距离
    
    // 计算平移矩阵
    Mat translationMatrix = (Mat_<double>(2,3) << 1, 0, dx, 0, 1, dy);
    
    // 创建输出图像
    Mat translatedImage;
    
    // 设置输出图像的大小
    Size dsize3(image.cols, image.rows);
    
    // 应用仿射变换
    warpAffine(image, translatedImage, translationMatrix, dsize3, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));

    // 缩放
      // 设置缩放比例
    double scaleX = 0.5; // 水平方向缩放比例
    double scaleY = 0.5; // 垂直方向缩放比例
    
    // 计算缩放矩阵
    Mat scalingMatrix = (Mat_<double>(2,3) << scaleX, 0, 0, 0, scaleY, 0);
    
    // 创建输出图像
    Mat scaledImage;
    
    // 设置输出图像的大小
    Size dsize4(image.cols * scaleX, image.rows * scaleY);
    
    // 应用仿射变换
    warpAffine(image, scaledImage, scalingMatrix, dsize4, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));
    
    
    // 显示结果
   
    imshow("原始图像", image);
    imshow("旋转图像", rotatedImage);
    imshow("错切图像", shearedImage);
    imshow("平移图像", translatedImage);
    imshow("缩放图像", scaledImage);
    
    waitKey(0);

    return 0;
}

运行结果

相关推荐
加点油。。。。5 分钟前
【强化学习】——策略梯度方法
人工智能·机器学习·强化学习
2401_8414956414 分钟前
【自然语言处理】处理 GBK 编码汉字的算法设计
人工智能·python·自然语言处理·校验·文件读写·gbk编码与解码·批量过滤
怎么全是重名20 分钟前
Survey on semantic segmentation using deep learning techniques
图像处理·人工智能·深度学习·图像分割
老蒋新思维21 分钟前
创客匠人:工作流嵌入式智能体,重构知识变现的效率底层
大数据·服务器·人工智能·重构·创始人ip·创客匠人·知识变现
2501_9419820522 分钟前
展望:RPA与AI在企业微信自动化领域的未来融合趋势
人工智能·企业微信·rpa
小脉传媒GEO优化22 分钟前
GEO优化数据统计系统DeepAnaX系统详细介绍:开启AI数据智能分析新范式
人工智能·信息可视化
爱笑的眼睛1124 分钟前
MLflow Tracking API:超越实验记录,构建可复现的机器学习工作流
java·人工智能·python·ai
世岩清上25 分钟前
以技术预研为引擎,驱动脑机接口等未来产业研发与应用创新发展
人工智能·脑机接口·未来产业
YuforiaCode26 分钟前
黑马AI大模型神经网络与深度学习课程笔记(个人记录、仅供参考)
人工智能·笔记·深度学习