opencv-014-使用resize进行图像插值(Image Interpolation)

知识点

最常见四种插值算法

  • INTER_NEAREST = 0 #最近邻插值,速度快,没考虑周围像素影响

  • INTER_LINEAR = 1 #双线性插值

  • INTER_CUBIC = 2 #双立方插值,高质量

  • INTER_LANCZOS4 = 4 #高质量

关于这四种插值算法的详细代码实现与解释

三种常见双立方插值算法-CSDN

图像放缩之双立方插值

图像放缩之双线性内插值

Lanczos采样放缩算法

相关的应用场景
几何变换、透视变换、插值计算新像素

API

resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR )

代码(c++,python)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/*
* 图像插值
*/
int main() {
Mat src = imread("../images/test.png");
if (src.empty()) {
cout << "could not load image.." << endl;
}
imshow("input", src);

int h = src.rows;
int w = src.cols;
float fx = 0.0, fy = 0.0;
Mat dst = Mat::zeros(src.size(), src.type());
Size S(w * 2, h * 2);

resize(src, dst, S, fx, fy, INTER_NEAREST);
imshow("INTER_NEAREST", dst);

resize(src, dst, S, fx, fy, INTER_LINEAR);
imshow("INTER_LINEAR", dst);

resize(src, dst, S, fx, fy, INTER_CUBIC);
imshow("INTER_CUBIC", dst);

resize(src, dst, S, fx, fy, INTER_LANCZOS4);
imshow("INTER_LANCZOS4", dst);

waitKey(0);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import cv2 as cv

src = cv.imread("D:/vcprojects/images/test.png")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)

h, w = src.shape[:2]
print(h, w)
dst = cv.resize(src, (w*2, h*2), fx=0.75, fy=0.75, interpolation=cv.INTER_NEAREST)
cv.imshow("INTER_NEAREST", dst)

dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_LINEAR)
cv.imshow("INTER_LINEAR", dst)

dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_CUBIC)
cv.imshow("INTER_CUBIC", dst)

dst = cv.resize(src, (w*2, h*2), interpolation=cv.INTER_LANCZOS4)
cv.imshow("INTER_LANCZOS4", dst)

cv.warpAffine()

cv.waitKey(0)
cv.destroyAllWindows()

结果

代码地址

github