resize函数有五种插值算法
最新版OpenCV2.4.7中,cv::resize函数有五种插值:最近邻、双线性、双三次、基于像素区域关系、兰索斯插值。下⾯⽤for循环代替cv::resize函数来说明其详细的插值实现过程,其中部分代码摘⾃于cv::resize函数中的源代码。
每种插值算法的前部分代码是相同的,如下:
[cpp]
1. cv::Mat matSrc, matDst1, matDst2;
2.
3. matSrc = cv::imread("lena.jpg", 2 | 4);
4. matDst1 = cv::Mat(cv::Size(800, 1000), pe(), cv::Scalar::all(0));
5. matDst2 = cv::Mat(matDst1.size(), pe(), cv::Scalar::all(0));
6.
7. double scale_x = (ls / ls;
8. double scale_y = (ws / ws;
1、最近邻:公式,
[cpp]
1. for (int i = 0; i < ls; ++i)
2. {
3. int sx = cvFloor(i * scale_x);
4. sx = std::min(sx, ls - 1);
5. for (int j = 0; j < ws; ++j)
6. {
7. int sy = cvFloor(j * scale_y);
8. sy = std::min(sy, ws - 1);
9. matDst1.at<cv::Vec3b>(j, i) = matSrc.at<cv::Vec3b>(sy, sx);
10. }
11. }
12. cv::imwrite("nearest_1.jpg", matDst1);
13.resize函数c++
14. cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 0);
15. cv::imwrite("nearest_2.jpg", matDst2);
2、双线性:由相邻的四像素(2*2)计算得出,公式,
[cpp]
1. uchar* dataDst = matDst1.data;
2. int stepDst = matDst1.step;
3. uchar* dataSrc = matSrc.data;
4. int stepSrc = matSrc.step;
5. int iWidthSrc = ls;
6. int iHiehgtSrc = ws;
7.
8. for (int j = 0; j < ws; ++j)
9. {
10. float fy = (float)((j + 0.5) * scale_y - 0.5);
11. int sy = cvFloor(fy);
12. fy -= sy;
13. sy = std::min(sy, iHiehgtSrc - 2);
14. sy = std::max(0, sy);
15.
16. short cbufy[2];
17. cbufy[0] = cv::saturate_cast<short>((1.f - fy) * 2048);
18. cbufy[1] = 2048 - cbufy[0];
19.
20. for (int i = 0; i < ls; ++i)
21. {
22. float fx = (float)((i + 0.5) * scale_x - 0.5);
23. int sx = cvFloor(fx);
24. fx -= sx;
25.
26. if (sx < 0) {
27. fx = 0, sx = 0;
28. }
29. if (sx >= iWidthSrc - 1) {
30. fx = 0, sx = iWidthSrc - 2;
31. }
32.
33. short cbufx[2];
34. cbufx[0] = cv::saturate_cast<short>((1.f - fx) * 2048);
35. cbufx[1] = 2048 - cbufx[0];
36.
37. for (int k = 0; k < matSrc.channels(); ++k)
38. {
39. *(dataDst+ j*stepDst + 3*i + k) = (*(dataSrc + sy*stepSrc + 3*sx + k) * cbufx[0] * cbufy[0] +
40. *(dataSrc + (sy+1)*stepSrc + 3*sx + k) * cbufx[0] * cbufy[1] +
41. *(dataSrc + sy*stepSrc + 3*(sx+1) + k) * cbufx[1] * cbufy[0] +
42. *(dataSrc + (sy+1)*stepSrc + 3*(sx+1) + k) * cbufx[1] * cbufy[1]) >> 22;
43. }
44. }
45. }
46. cv::imwrite("linear_1.jpg", matDst1);
47.
48. cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 1);
49. cv::imwrite("linear_2.jpg", matDst2);
3、双三次:由相邻的4*4像素计算得出,公式类似于双线性
[cpp]
1. int iscale_x = cv::saturate_cast<int>(scale_x);
2. int iscale_y = cv::saturate_cast<int>(scale_y);
3.
4. for (int j = 0; j < ws; ++j)
5. {
6. float fy = (float)((j + 0.5) * scale_y - 0.5);
7. int sy = cvFloor(fy);
8. fy -= sy;
9. sy = std::min(sy, ws - 3);
10. sy = std::max(1, sy);
11.
12. const float A = -0.75f;
13.
14. float coeffsY[4];
15. coeffsY[0] = ((A*(fy + 1) - 5*A)*(fy + 1) + 8*A)*(fy + 1) - 4*A;
16. coeffsY[1] = ((A + 2)*fy - (A + 3))*fy*fy + 1;
17. coeffsY[2] = ((A + 2)*(1 - fy) - (A + 3))*(1 - fy)*(1 - fy) + 1;
18. coeffsY[3] = 1.f - coeffsY[0] - coeffsY[1] - coeffsY[2];
19.
20. short cbufY[4];
21. cbufY[0] = cv::saturate_cast<short>(coeffsY[0] * 2048);
22. cbufY[1] = cv::saturate_cast<short>(coeffsY[1] * 2048);
23. cbufY[2] = cv::saturate_cast<short>(coeffsY[2] * 2048);
24. cbufY[3] = cv::saturate_cast<short>(coeffsY[3] * 2048);
25.
26. for (int i = 0; i < ls; ++i)
27. {
28. float fx = (float)((i + 0.5) * scale_x - 0.5);
29. int sx = cvFloor(fx);
30. fx -= sx;
31.
32. if (sx < 1) {
33. fx = 0, sx = 1;
34. }
35. if (sx >= ls - 3) {
36. fx = 0, sx = ls - 3;
37. }
38.
39. float coeffsX[4];
40. coeffsX[0] = ((A*(fx + 1) - 5*A)*(fx + 1) + 8*A)*(fx + 1) - 4*A;
41. coeffsX[1] = ((A + 2)*fx - (A + 3))*fx*fx + 1;
42. coeffsX[2] = ((A + 2)*(1 - fx) - (A + 3))*(1 - fx)*(1 - fx) + 1;
43. coeffsX[3] = 1.f - coeffsX[0] - coeffsX[1] - coeffsX[2];
44.
45. short cbufX[4];
46. cbufX[0] = cv::saturate_cast<short>(coeffsX[0] * 2048);
47. cbufX[1] = cv::saturate_cast<short>(coeffsX[1] * 2048);
48. cbufX[2] = cv::saturate_cast<short>(coeffsX[2] * 2048);
49. cbufX[3] = cv::saturate_cast<short>(coeffsX[3] * 2048);
50.
51. for (int k = 0; k < matSrc.channels(); ++k)
52. {
53. matDst1.at<cv::Vec3b>(j, i)[k] = abs((matSrc.at<cv::Vec3b>(sy-1, sx-1)[k] * cbufX[0] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx-1)
[k] * cbufX[0] * cbufY[1] +
54. matSrc.at<cv::Vec3b>(sy+1, sx-1)[k] * cbufX[0] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx-1)[k] * cbufX[0] * cbufY[3] +
55. matSrc.at<cv::Vec3b>(sy-1, sx)[k] * cbufX[1] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx)[k] * cbufX[1] * cbufY[1] +
56. matSrc.at<cv::Vec3b>(sy+1, sx)[k] * cbufX[1] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx)[k] * cbufX[1] * cbufY[3] +
57. matSrc.at<cv::Vec3b>(sy-1, sx+1)[k] * cbufX[2] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx+1)[k] * cbufX[2] * cbufY[1] +
58. matSrc.at<cv::Vec3b>(sy+1, sx+1)[k] * cbufX[2] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx+1)[k] * cbufX[2] * cbufY[3] +
59. matSrc.at<cv::Vec3b>(sy-1, sx+2)[k] * cbufX[3] * cbufY[0] + matSrc.at<cv::Vec3b>(sy, sx+2)[k] * cbufX[3] * cbufY[1] +
60. matSrc.at<cv::Vec3b>(sy+1, sx+2)[k] * cbufX[3] * cbufY[2] + matSrc.at<cv::Vec3b>(sy+2, sx+2)
[k] * cbufX[3] * cbufY[3] ) >> 22);
61. }
62. }
63. }
64. cv::imwrite("cubic_1.jpg", matDst1);
65.
66. cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 2);
67. cv::imwrite("cubic_2.jpg", matDst2);
4、基于像素区域关系:共分三种情况,图像放⼤时类似于双线性插值,图像缩⼩(x轴、y轴同时缩⼩)⼜分两种情况,此情况下可以避免波纹出现。
[cpp]
1. cv::resize(matSrc, matDst2, matDst1.size(), 0, 0, 3);
2. cv::imwrite("area_2.jpg", matDst2);
3.
4. double inv_scale_x = 1. / scale_x;
5. double inv_scale_y = 1. / scale_y;
6. int iscale_x = cv::saturate_cast<int>(scale_x);
7. int iscale_y = cv::saturate_cast<int>(scale_y);
8. bool is_area_fast = std::abs(scale_x - iscale_x) < DBL_EPSILON && std::abs(scale_y - iscale_y) < DBL_EPSILON;
9.
10. if (scale_x >= 1 && scale_y >= 1) //zoom out
11. {
12. if (is_area_fast) //integer multiples
13. {
14. for (int j = 0; j < ws; ++j)
15. {
16. int sy = j * scale_y;
17.
18. for (int i = 0; i < ls; ++i)
19. {
20. int sx = i * scale_x;
21.
22. matDst1.at<cv::Vec3b>(j, i) = matSrc.at<cv::Vec3b>(sy, sx);
23. }
24. }
25. cv::imwrite("area_1.jpg", matDst1);
26. return 0;
27. }
28.
29. for (int j = 0; j < ws; ++j)
30. {
31. double fsy1 = j * scale_y;
32. double fsy2 = fsy1 + scale_y;
33. double cellHeight = cv::min(scale_y, ws - fsy1);
34.
35. int sy1 = cvCeil(fsy1), sy2 = cvFloor(fsy2);
36.
37. sy2 = std::min(sy2, ws - 1);
38. sy1 = std::min(sy1, sy2);
39.
40. float cbufy[2];
41. cbufy[0] = (float)((sy1 - fsy1) / cellHeight);
42. cbufy[1] = (float)(std::min(std::min(fsy2 - sy2, 1.), cellHeight) / cellHeight);
43.
44. for (int i = 0; i < ls; ++i)
45. {
46. double fsx1 = i * scale_x;
47. double fsx2 = fsx1 + scale_x;
48. double cellWidth = std::min(scale_x, ls - fsx1);
49.
50. int sx1 = cvCeil(fsx1), sx2 = cvFloor(fsx2);
51.
52. sx2 = std::min(sx2, ls - 1);
53. sx1 = std::min(sx1, sx2);
54.
55. float cbufx[2];
56. cbufx[0] = (float)((sx1 - fsx1) / cellWidth);
57. cbufx[1] = (float)(std::min(std::min(fsx2 - sx2, 1.), cellWidth) / cellWidth);
58.
59. for (int k = 0; k < matSrc.channels(); ++k)
60. {
61. matDst1.at<cv::Vec3b>(j, i)[k] = (uchar)(matSrc.at<cv::Vec3b>(sy1, sx1)[k] * cbufx[0] * cbufy[0] +
62. matSrc.at<cv::Vec3b>(sy1 + 1, sx1)[k] * cbufx[0] * cbufy[1] +
63. matSrc.at<cv::Vec3b>(sy1, sx1 + 1)[k] * cbufx[1] * cbufy[0] +
64. matSrc.at<cv::Vec3b>(sy1 + 1, sx1 + 1)[k] * cbufx[1] * cbufy[1]);
65. }
66. }
67. }
68. cv::imwrite("area_1.jpg", matDst1);
69. return 0;
70. }
71.
72. //zoom in,it is emulated using some variant of bilinear interpolation
73. for (int j = 0; j < ws; ++j)
74. {
75. int sy = cvFloor(j * scale_y);
76. float fy = (float)((j + 1) - (sy + 1) * inv_scale_y);
77. fy = fy <= 0 ? 0.f : fy - cvFloor(fy);
78.
79. short cbufy[2];
80. cbufy[0] = cv::saturate_cast<short>((1.f - fy) * 2048);
81. cbufy[1] = 2048 - cbufy[0];
82.
83. for (int i = 0; i < ls; ++i)
84. {
85. int sx = cvFloor(i * scale_x);
86. float fx = (float)((i + 1) - (sx + 1) * inv_scale_x);
87. fx = fx < 0 ? 0.f : fx - cvFloor(fx);
88.
89. if (sx < 0) {
90. fx = 0, sx = 0;
91. }
92.
93. if (sx >= ls - 1) {
94. fx = 0, sx = ls - 2;
95. }
96.
97. short cbufx[2];
98. cbufx[0] = cv::saturate_cast<short>((1.f - fx) * 2048);
99. cbufx[1] = 2048 - cbufx[0];
100.
101. for (int k = 0; k < matSrc.channels(); ++k)
102. {
103. matDst1.at<cv::Vec3b>(j, i)[k] = (matSrc.at<cv::Vec3b>(sy, sx)[k] * cbufx[0] * cbufy[0] + 104. matSrc.at<cv::Vec3b>(sy + 1, sx)[k] * cbufx[0] * cbufy[1] +
105. matSrc.at<cv::Vec3b>(sy, sx + 1)[k] * cbufx[1] * cbufy[0] +
106. matSrc.at<cv::Vec3b>(sy + 1, sx + 1)[k] * cbufx[1] * cbufy[1]) >> 22;
107. }
108. }
109. }
110. cv::imwrite("area_1.jpg", matDst1);
5、兰索斯插值:由相邻的8*8像素计算得出,公式类似于双线性
[cpp]
1. int iscale_x = cv::saturate_cast<int>(scale_x);
2. int iscale_y = cv::saturate_cast<int>(scale_y);
3.
4. for (int j = 0; j < ws; ++j)
5. {
6. float fy = (float)((j + 0.5) * scale_y - 0.5);
7. int sy = cvFloor(fy);
8. fy -= sy;
9. sy = std::min(sy, ws - 5);
10. sy = std::max(3, sy);
11.
12. const double s45 = 0.70710678118654752440084436210485;
13. const double cs[][2] = {{1, 0}, {-s45, -s45}, {0, 1}, {s45, -s45}, {-1, 0}, {s45, s45}, {0, -1}, {-s45, s45}};
14. float coeffsY[8];
15.
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论