概述
将图像进行仿射变换,包括 平移 、缩放 、翻转 、旋转 和 错切 。
函数
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 | 输出图像 |
| M | 变换矩阵(2*3) |
| dsize | 输出图像的大小 |
| flags | 插值方法 |
| borderMode | 图像边框模式 |
| borderValue | 恒定边框的数值(默认为0) |
测试函数
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//载入图像
Mat src = imread("c:/opencv/666.jpg");
//显示
imshow("src",src);
//第一种方法:
//定义中心点
Point2f center(src.size().width/2.0,src.size().height/2.0);
//定义旋转角度
double angle = 45;
//定义比例因子
double scale = 1.0;
//获取旋转矩阵
Mat M1 = getRotationMatrix2D(center,angle,scale);
//定义输出图像
Mat dst1;
//运算
warpAffine(src,dst1,M1,src.size());
//显示
imshow("dst1",dst1);
//第二种方法
//定义三个点
Point2f ps_src[3];
Point2f ps_dst[3];
ps_src[0] = Point2f(0,0);
ps_src[1] = Point2f(src.size().width-1,0);
ps_src[2] = Point2f(0,src.size().height-1);
ps_dst[0] = Point2f(src.size().width*0.00 , src.size().height*0.30);
ps_dst[1] = Point2f(src.size().width*0.85 , src.size().height*0.25);
ps_dst[2] = Point2f(src.size().width*0.15 , src.size().height*0.75);
//获取变换矩阵
Mat M2 = getAffineTransform(ps_src,ps_dst);
//定义输出图像
Mat dst2;
//运算
warpAffine(src,dst2,M2,src.size());
//显示
imshow("dst2",dst2);
}
Widget::~Widget()
{
delete ui;
}
测试结果

本文详细介绍了OpenCV中的warpAffine函数,涉及图像仿射变换,如旋转、缩放和平移操作。通过实例展示了如何使用该函数进行图像变换,并提供了两种不同的方法实现。适合学习者了解图像处理中仿射变换的应用。

6944

被折叠的 条评论
为什么被折叠?



