1.opencv插值介绍
opencv提供resize函数用来做图像缩放,该函数有6个参数:
(1)输入图像,Mat型
(2)输出图像,Mat型
(3)输出图像大小,可用cv::Size(out_img_width, out_img_height)来表示,如果该值为0,则输出大小由第4,5两个参数fx、fy决定
(4)水平缩放因子,如果该值为0,则会按照(double)dsize.width/src.cols来计算
(5)重直缩放因子,如果该值为0,则会按照(double)dsize.height/src.rows来计算
(6)插值算法,可以选择各种插值算法
2.插值参数选择
opencv文档及代码写的很清楚了,这里就不再细说,实际使用上,对于放大场景来说,双立方插值INTER_CUBIC与lanczos插值INTER_LANCZOS4效果较好,而缩小场景,效果最好的算法为面积关联法INTER_AREA,该方法在放大时,效果与最近邻算法差不多。
INTER_NEAREST - a nearest-neighbor interpolation
INTER_LINEAR - a bilinear interpolation (used by default)
INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
enum InterpolationFlags{
/** nearest neighbor interpolation */
INTER_NEAREST = 0,
/** bilinear interpolation */
INTER_LINEAR = 1,
/** bicubic interpolation */
INTER_CUBIC = 2,
/** resampling using pixel area relation. It may be a preferred method for image decimation, as
it gives moire'-free results. But when the image is zoomed, it is similar to the INTER_NEAREST
method. */
INTER_AREA = 3,
/** Lanczos interpolation over 8x8 neighborhood */
INTER_LANCZOS4 = 4,
/** Bit exact bilinear interpolation */
INTER_LINEAR_EXACT = 5,
/** mask for interpolation codes */
INTER_MAX = 7,
/** flag, fills all of the destination image pixels. If some of them correspond to outliers in the
source image, they are set to zero */
WARP_FILL_OUTLIERS = 8,
/** flag, inverse transformation
For example, #linearPolar or #logPolar transforms:
- flag is __not__ set: \f$dst( \rho , \phi ) = src(x,y)\f$
- flag is set: \f$dst(x,y) = src( \rho , \phi )\f$
*/
WARP_INVERSE_MAP = 16
};
3.demo
img_decode为输入图像,img_resize为输出图像
resize(img_decode, img_resize, Size(img_dst_width, img_dst_height), 0, 0, INTER_LANCZOS4);
下面为缩小case,原图分辨率为633x773,目标分辨率为286x350,如下分别为面积插值、双线性、lanczos效果,面积关联法效果最佳:
这里的插值基本都是传统的算法,基于底层像素数据做的处理,后续单独再来介绍这里的算法原理。而在放大等领域,近几年出现了大量的super resolution算法,在手机zoom、视频播放等场景应用广泛。
参考
[1] https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=resize