概述
这个函数的作用是调整图像大小。
函数
void cv::resize
(
InputArray src,
OutputArray dst,
Size dsize,
double fx = 0,
double fy = 0,
int interpolation = INTER_LINEAR
)
src | 源图像 |
dst | 输出图像 |
dsize | 输出图像的大小 |
fx | 水平方向的缩放因子 |
fy | 垂直方向的缩放因子 |
interpolation | 插值方法(参考InterpolationFlags) |
- 当 dsize 的值为0,输出图像的尺寸取决于 fx 和 fy ;当 fx 和 fy 的值为0时,输出图像的尺寸取决于 dsize 。
- 关于 interpolation ,如果要缩小图像,通常选用 INTER_AREA ,插值效果最好;如果要放大图像,通常选用 INTER_CUBIC(速度慢/效果好)或者 INTER_LINEAR(速度快/效果差)。
测试代码
#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/star.jpg");
//显示
imshow("src",src);
//打印尺寸
qDebug() << src.size().width;
qDebug() << src.size().height;
//缩小图像
Mat dst1;
//重新定义尺寸
cv::resize(src,dst1,Size(0,0),0.5,0.5,INTER_AREA);
//打印尺寸
qDebug() << dst1.size().width;
qDebug() << dst1.size().height;
//显示
imshow("dst1",dst1);
//放大图像
Mat dst2;
//重新定义尺寸
cv::resize(src,dst2,Size(500,500),0,0,INTER_CUBIC);
//打印尺寸
qDebug() << dst2.size().width;
qDebug() << dst2.size().height;
//显示
imshow("dst2",dst2);
}
Widget::~Widget()
{
delete ui;
}