1 前备知识
图像在进行几何变换、透视变换等场景时需要插值计算新像素的像素值。
(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[ exttt{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[ exttt{(double)dsize.width/src.cols}f]
@param fy scale factor along the vertical axis; when it equals 0, it is computed as
f[ exttt{(double)dsize.height/src.rows}f]
@param interpolation interpolation method, see #InterpolationFlags
@sa warpAffine, warpPerspective, remap
*/
CV_EXPORTS_W void resize( InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR );
3 程序代码
#include"opencv2opencv.hpp" #include<iostream> using namespace std; using namespace cv; int main(int argc, char** argv) { Mat src = imread("G:\CVworkstudy\program_wwx\研习社140课时\ZhaiZhigang140\colormap.png"); if (src.empty()) { printf("Could not load image... "); return -1; } imshow("srcImg", src); Mat dst = Mat::zeros(src.size(), src.type()); double fx = 0, fy = 0; int height = src.rows; int width = src.cols; //最邻近插值 resize(src, dst, Size(0, 0), 0.5, 0.5, INTER_NEAREST);//如果Size(0,0),则图像大小为Size(width*fx height*fy) imshow("NEAREST", dst); //双线性插值 resize(src, dst, Size(width * 2, height * 2), fx, fy, INTER_LINEAR); imshow("LineNear", dst); //双立方插值 resize(src, dst, Size(width * 2, height * 2), fx, fy, INTER_CUBIC); imshow("CUBIC", dst); //LANCZOS插值 resize(src, dst, Size(width * 2, height * 2), fx, fy, INTER_LANCZOS4); imshow("LANCZOS", dst); waitKey(0); return 0; }
4 运行结果
5 扩展及注意事项
null