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

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
*/

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CT image compression (a) Implement the simplified DCT compression process above for n = 2, 4, and 8 and apply it to the attached image. Show the reconstructed images for these three different cases. [3 images] Compute the PSNR values of the three reconstructed images and discuss what the PSNR value means here. (b) Use the same process in (a) with image transformed to YIQ color model and show the reconstructed image in RGB space. [3 images] Compute the PSNR values of the three reconstructed images and discuss what the PSNR value means here. Dithering 2. Dithering (30%) Convert the image cat2_gray.png to binary (black and white) image with different methods of dithering, show the results, and make some comparison with the results. (a) Apply noise (random) dithering on the provided image and show the result. [1 image] (b) Apply average dithering on the provided image and show the result. [1 image] (c) Apply error diffusion dithering (Floyd-Steinberg algorithm) on the provided image and show the result. [1 image] Image Interpolation Implement the image interpolation function to upsample an image to four times the original width and height. Implement the following two different interpolation methods and show the 4× upsampled images. (a) Apply nearest-neighbor interpolation on the low resolution image, cat3_LR.png, and compute the PSNR with the original high resolution image, cat3_HR.png. [1 image] (b) Apply bilinear interpolation on the low resolution image and compute the PSNR with the high resolution image. [1 image] (c) Apply bicubic interpolation on the low resolution image and compute the PSNR with the high resolution image. [1 image]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值