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[\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
*/
CV_EXPORTS_W void resize( InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR );
3 程序代碼
#include"opencv2\opencv.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...\n"); 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
