使用OpenCV调整图像大小
要调整图像的大小,可以沿着每个轴(高度和宽度)
缩放它,考虑指定的比例系数,或者设置所需的高度和宽度。
当调整图片大小时:
各种插值技术来完成这些操作。OpenCV中有几种可用的方法,具体的选择通常取决于特定的应用程序
让我们来看看通过自定义高度和宽度的大小来使图像变大或变小的代码示例。随着您的深入,我们将讨论使用不同的尺度因子和插值方法调整大小。
Python
# let's start with the Imports
import cv2
import numpy as np
# Read the image using imread function
image = cv2.imread('image.jpg')
cv2.imshow('Original Image', image)
# let's downscale the image using new width and height
down_width = 300
down_height = 200
down_points = (down_width, down_height)
resized_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR)
# let's upscale the image using new width and height
up_width = 600
up_height = 400
up_points = (up_width, up_height)
resized_up = cv2.resize(image, up_points, interpolation= cv2.INTER_LINEAR)
# Display images
cv2.imshow('Resized Down by defining height and width', resized_down)
cv2.waitKey()
cv2.imshow('Resized Up image by defining height and width', resized_up)
cv2.waitKey()
#press any key to close the windows
cv2.destroyAllWindows()
C++
// let's start with including libraries
#include<opencv2/opencv.hpp>
#include<iostream>
// Namespace to nullify use of cv::function(); syntax
using namespace std;
using namespace cv;
int main()
{
// Read the image using imread function
Mat image = imread("image.jpg");
imshow("Original Image", image);
// let's downscale the image using new width and height
int down_width = 300;
int down_height = 200;
Mat resized_down;
//resize down
resize(image, resized_down, Size(down_width, down_height), INTER_LINEAR);
// let's upscale the image using new width and height
int up_width = 600;
int up_height = 400;
Mat resized_up;
//resize up
resize(image, resized_up, Size(up_width, up_height), INTER_LINEAR);
// Display Images and press any key to continue
imshow("Resized Down by defining height and width", resized_down);
waitKey();
imshow("Resized Up image by defining height and width", resized_up);
waitKey();
destroyAllWindows();
return 0;
}
Resize 函数语法
让我们从OpenCV resize()函数语法开始。注意,只需要两个输入参数:
我们将在下面的小节中讨论各种输入参数选项。
resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
通过指定宽度和高度调整大小
在第一个例子中,让我们通过指定一个新的宽度和高度来调整图像的大小。在下面的代码中
Python
# Set rows and columns
# lets downsize the image using new width and height
down_width = 300
down_height = 200
down_points = (down_width, down_height)
resize_down = cv2.resize(image, down_points, interpolation= cv2.INTER_LINEAR)
C++
// Set rows and columns
// lets downsize the image using new width and height
int down_width = 300;
int down_height = 200;
Mat resize_down;
// resize down
resize(image, resize_down, Size(down_width, down_height), INTER_LINEAR);
接下来,我们创建另一个变量来增加图像的大小。
Python
# Set rows and columns
up_width = 600
up_height = 400
up_points = (up_width, up_height)
# resize the image
resized_up = cv2.resize(image, up_points, interpolation = cv2.INTER_LINEAR)
C++
// Set rows and columns
int up_width = 600;
int up_height = 400;
Mat resized_up;
//resize up
resize(image, resized_up, Size(up_width, up_height), INTER_LINEAR);
在上面的Python代码片段中,我们使用resize()函数定义了新的宽度和高度来优化图像。流程和步骤与前面的代码片段类似。
使用缩放因子调整大小
缩放因子(Scale Factor)
通常是一个数字,它缩放或乘以某个数量,在我们的例子中是图像的宽度和高度。它有助于保持高宽比完好无损,并保持显示质量。因此,当你放大
或缩小
它时,图像不会出现扭曲。
Python
# Scaling Up the image 1.2 times by specifying both scaling factors
scale_up_x = 1.2
scale_up_y = 1.2
# Scaling Down the image 0.6 times specifying a single scale factor.
scale_down = 0.6
scaled_f_down = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_LINEAR)
scaled_f_up = cv2.resize(image, None, fx= scale_up_x, fy= scale_up_y, interpolation= cv2.INTER_LINEAR)
C++
// Scaling Up the image 1.2 times by specifying both scaling factors
double scale_up_x = 1.2;
double scale_up_y = 1.2;
// Scaling Down the image 0.6 times specifying a single scale factor.
double scale_down = 0.6;
Mat scaled_f_up, scaled_f_down;
//resize
resize(image,scaled_f_down, Size(), scale_down, scale_down, INTER_LINEAR);
resize(image, scaled_f_up, Size(), scale_up_x, scale_up_y, INTER_LINEAR);
在上面的Python代码片段中:
dsize
为None
。在上面的c++代码片段中
使用不同的插值方法调整大小
INTER_AREA
: INTER_AREA使用像素面积关系进行重采样。这最适合于缩小图像的大小(缩小)。当用于放大图像时,它使用INTER_NEAREST方法。INTER_CUBIC:
使用双三次插值来调整图像的大小。在调整大小和插值新像素时,该方法作用于图像的4×4邻近像素。然后取16个像素的加权平均值来创建新的插值像素。INTER_LINEAR
:这种方法有点类似于INTER_CUBIC插值。但与INTER_CUBIC不同的是,它使用2×2邻近像素来获得插值像素的加权平均。INTER_NEAREST
: INTER_NEAREST方法使用最近邻概念进行插值。这是最简单的方法之一,只使用图像中的一个相邻像素进行插值。Python
# Scaling Down the image 0.6 times using different Interpolation Method
res_inter_nearest = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_NEAREST)
res_inter_linear = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_LINEAR)
res_inter_area = cv2.resize(image, None, fx= scale_down, fy= scale_down, interpolation= cv2.INTER_AREA)
C++
# Scaling Down the image 0.6 using different Interpolation Method
Mat res_inter_linear, res_inter_nearest, res_inter_area;
resize(image, res_inter_linear, Size(), scale_down, scale_down, INTER_LINEAR);
resize(image, res_inter_nearest, Size(), scale_down, scale_down, INTER_NEAREST);
resize(image, res_inter_area, Size(), scale_down, scale_down, INTER_AREA);
在上面的Python代码片段中,我们使用不同的插值方法来调整图像的大小。类似地,在c++代码片段中,我们首先为输出图像定义新的矩阵,然后用不同的插值方法调整它们的大小。现在让我们显示图像。
Python
# Concatenate images in horizontal axis for comparison
vertical= np.concatenate((res_inter_nearest, res_inter_linear, res_inter_area), axis = 0)
# Display the image Press any key to continue
cv2.imshow('Inter Nearest :: Inter Linear :: Inter Area', vertical)
C++
Mat a,b,c;
vconcat(res_inter_linear, res_inter_nearest, a);
vconcat(res_inter_area, res_inter_area, b);
vconcat(a, b, c);
// Display the image Press any key to continue
imshow("Inter Linear :: Inter Nearest :: Inter Area :: Inter Area", c);
来源:@BangBang