概述
这个函数用来对图像进行 透视变换 。
函数
void cv::warpPerspective
(
InputArray src,
OutputArray dst,
InputArray M,
Size dsize,
int flags = INTER_LINEAR,
int borderMode = BORDER_CONSTANT,
const Scalar & borderValue = Scalar()
)
src | 源图像 |
dst | 输出图像 |
M | 变换矩阵(3*3) |
dsize | 输出图像的大小 |
flags | 插值方法 |
borderMode | 边框模式 ● BORDER_CONSTANT ● BORDER_REPLICATE |
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);
//定义4个点
Point2f ps_src[4];
Point2f ps_dst[4];
ps_src[0] = Point2f(0,0);
ps_src[1] = Point2f(src.size().width-1 , 0);
ps_src[2] = Point2f(src.size().width-1 , src.size().height-1);
ps_src[3] = Point2f(0 , src.size().height-1);
ps_dst[0] = Point2f(src.size().width*0.25 , src.size().height*0.20);
ps_dst[1] = Point2f(src.size().width*0.75 , src.size().height*0.20);
ps_dst[2] = Point2f(src.size().width*0.85 , src.size().height*0.85);
ps_dst[3] = Point2f(src.size().width*0.15 , src.size().height*0.85);
//获取变换矩阵
Mat M = getPerspectiveTransform(ps_src,ps_dst);
//定义输出图像
Mat dst;
//运算
warpPerspective(src,dst,M,src.size());
//显示
imshow("dst",dst);
}
Widget::~Widget()
{
delete ui;
}