OpenCv 014---图像插值(Image interpolation)

男娘i 2021-11-08 17:00 530阅读 0赞

1 前备知识

图像在进行几何变换、透视变换等场景时需要插值计算新像素的像素值。

图像处理之三种常见双立方插值算法 - CSDN博客

图像放缩之双立方插值 - CSDN博客

图像放缩之双线性内插值 - CSDN博客

图像处理之Lanczos采样放缩算法 - CSDN博客

(1)INTER_NEAREST最邻近插值

将离新像素所在位置最近的像素像素值赋值给新像素。计算亮最小。

(2)双线性插值

x、y方向临近像素取乘以相应权重并相加赋值给i新的像素值。

(3)双立方插值

精度更高,计算量最大,取附近十六个点加权取像素值。

(4)INTER_LANCZOS4

附近像素及原像素加权取值。

2 所用到的主要OpenCv API

/** @brief 对一副图像指定x,y方向上的缩放比例进行缩放。

@code
// explicitly specify dsize=dst.size(); fx and fy will be computed from that.
resize(src, dst, dst.size(), 0, 0, interpolation);
@endcode
If you want to decimate the image by factor of 2 in each direction, you can call the function this
way:
@code
// specify fx and fy and let the function compute the destination image size.
resize(src, dst, Size(), 0.5, 0.5, interpolation);
@endcode
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).

@param src input image.
@param dst output image; it has the size dsize (when it is non-zero) or the size computed from
src.size(), fx, and fy; the type of dst is the same as of src.
@param dsize output image size; if it equals zero, it is computed as:
\f[\texttt{dsize = Size(round(fx*src.cols), round(fy*src.rows))}\f]
Either dsize or both fx and fy must be non-zero.
@param fx scale factor along the horizontal axis; when it equals 0, it is computed as
\f[\texttt{(double)dsize.width/src.cols}\f]
@param fy scale factor along the vertical axis; when it equals 0, it is computed as
\f[\texttt{(double)dsize.height/src.rows}\f]
@param interpolation interpolation method, see #InterpolationFlags

@sa warpAffine, warpPerspective, remap
*/

  1. CV_EXPORTS_W void resize( InputArray src, OutputArray dst,
  2. Size dsize, double fx = 0, double fy = 0,
  3. int interpolation = INTER_LINEAR );

3 程序代码

  1. #include"opencv2\opencv.hpp"
  2. #include<iostream>
  3. using namespace std;
  4. using namespace cv;
  5. int main(int argc, char** argv)
  6. {
  7. Mat src = imread("G:\\CVworkstudy\\program_wwx\\研习社140课时\\ZhaiZhigang140\\colormap.png");
  8. if (src.empty())
  9. {
  10. printf("Could not load image...\n");
  11. return -1;
  12. }
  13. imshow("srcImg", src);
  14. Mat dst = Mat::zeros(src.size(), src.type());
  15. double fx = 0, fy = 0;
  16. int height = src.rows;
  17. int width = src.cols;
  18. //最邻近插值
  19. resize(src, dst, Size(0, 0), 0.5, 0.5, INTER_NEAREST);//如果Size(0,0),则图像大小为Size(width*fx height*fy)
  20. imshow("NEAREST", dst);
  21. //双线性插值
  22. resize(src, dst, Size(width * 2, height * 2), fx, fy, INTER_LINEAR);
  23. imshow("LineNear", dst);
  24. //双立方插值
  25. resize(src, dst, Size(width * 2, height * 2), fx, fy, INTER_CUBIC);
  26. imshow("CUBIC", dst);
  27. //LANCZOS插值
  28. resize(src, dst, Size(width * 2, height * 2), fx, fy, INTER_LANCZOS4);
  29. imshow("LANCZOS", dst);
  30. waitKey(0);
  31. return 0;
  32. }

4 运行结果

1321981-20190716144332957-1911126177.png

1321981-20190716144355784-1019991392.png

1321981-20190716144431606-753847711.png

1321981-20190716144535102-814843486.png

5 扩展及注意事项

null

转载于:https://www.cnblogs.com/Vince-Wu/p/11194910.html

发表评论

表情:
评论列表 (有 0 条评论,530人围观)

还没有评论,来说两句吧...

相关阅读