OpenCV - C++ -
cv::resize
cv::resize
1. cv::resize
C++
void cv::resize (InputArray src,
OutputArray dst,
Size dsize,
double fx = 0,
double fy = 0,
int interpolation = INTER_LINEAR
)
Python
dst = cv.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
#include <opencv2/imgproc.hpp>
Resizes an image.
缩放图像大小。
The function resize
resizes the image src
down to or up to the specified size. Note that the initial dst
type or size
are not taken into account. Instead, the size
and type
are derived from the src, dsize, fx, and fy
. If you want to resize src so that it fits the pre-created dst, you may call the function as follows:
函数 resize
将图像 src
缩放为指定大小。注意,不考虑初始的 dst
类型或 size
。相反,size
and type
是从 src, dsize, fx, and fy
推导出来的。如果要缩放 src
的大小以使其适合预先创建的 dst
,则可以按以下方式调用该函数:
// explicitly specify dsize=dst.size(), fx and fy will be computed from that.
resize(src, dst, dst.size(), 0, 0, interpolation);
If you want to decimate the image by factor of 2 in each direction, you can call the function this way:
如果要在每个方向上将图像缩小 2 倍,则可以通过以下方式调用该函数:
// specify fx and fy and let the function compute the destination image size.
resize(src, dst, Size(), 0.5, 0.5, interpolation);
To shrink an image, it will generally look best with INTER_AREA
interpolation, whereas to enlarge an image, it will generally look best with c::INTER_CUBIC
(slow) or INTER_LINEAR
(faster but still looks OK).
要缩小图像,通常使用 INTER_AREA
插值时效果最佳,而要放大图像,通常使用 c::INTER_CUBIC
(速度慢) 或 INTER_LINEAR
(速度更快,但仍然可以) 来显示最佳效果。
1.1 Parameters
src
- input image.dst
- output image. It has the sizedsize
(when it is non-zero) or the size computed fromsrc.size()
,fx
, andfy
; the type ofdst
is the same as ofsrc
.dsize
- output image size. If it equals zero, it is computed as:dsize = Size(round(fx*src.cols), round(fy*src.rows))
. Eitherdsize
or bothfx
andfy
must be non-zero.fx
- scale factor along the horizontal axis; when it equals 0, it is computed as(double)dsize.width/src.cols
fy
- scale factor along the vertical axis; when it equals 0, it is computed as(double)dsize.height/src.rows
dsize = cv::Size(round(fx*src.cols), round(fy*src.rows))
dsize = cv::Size(width, height)
dsize = cv::Size(x, y)
dsize = cv::Size(cols, rows)
2D array size: Size(cols, rows).
2. Example
//============================================================================
// Name : using namespace cv;
// Author : Yongqiang Cheng
// Version : Feb 22, 2020
// Copyright : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
Mat resized_image;
//--- INITIALIZE VIDEOCAPTURE
VideoCapture cap;
// open the default camera using default API
// cap.open(0);
// OR advance usage: select any API backend
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID + apiID);
// check if we succeeded
if (!cap.isOpened())
{
cerr << "ERROR! Unable to open camera\n";
return -1;
}
namedWindow("source image", WINDOW_AUTOSIZE);
namedWindow("resized image", WINDOW_AUTOSIZE);
float scale = 0.5;
int frame_width = cap.get(CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(CAP_PROP_FRAME_HEIGHT);
int resized_width = frame_width * scale;
int resized_height = frame_height * scale;
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl << "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty())
{
cerr << "ERROR! blank frame grabbed\n";
break;
}
resize(frame, resized_image, Size(resized_width, resized_height));
// show live and wait for a key with timeout long enough to show images
imshow("source image", frame);
imshow("resized image", resized_image);
if (waitKey(5) >= 0)
{
break;
}
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
09:25:57 **** Build of configuration Debug for project DisplayImage ****
make all
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cpp
Building target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage" ./src/DisplayImage.o -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage
09:26:01 Build Finished (took 3s.127ms)