概述
本文介绍图像插值一般性原理,并且对常用插值函数的插值效果进行对比。
插值一般原理
原图像可以视为整数像素下的二维矩阵,所谓插值,就是在分数坐标处给原图插入新的值。这个新值根据一定的计算方法,由原图在其周围的有限范围(插值函数作用域)内的所有整数像素下的值组合生成,与其领域内的原图像素具有强关联性。
以下是插值的一般公式:
附一张插值原理详细的解释图:
插值窗函数 w(x,y)
MATLAB代码:
定义HamSinc插值方法:- function img_out = hammingSincInterpolation(img_in, R, HWS)
- % [列,行]: (i, j) (x, y) (m,n)
- n_row0 = size(img_in, 1);
- n_col0 = size(img_in, 2);
- n_row = R * n_row0;
- n_col = R * n_col0;
- img_out = zeros(n_row, n_col);
- for j = 1 : n_row
- for i = 1 : n_col
- interp_val = 0;
- sum_w = 0;
- for n = -HWS:HWS
- for m = -HWS:HWS
- % 超过原始边界则路过:
- if (floor((j-1)/R) + n) < 1 || (floor((j-1)/R) + n) > n_row0 || (floor((i-1)/R) + m) < 1 || (floor((i-1)/R) + m) > n_col0
- continue;
- end
- dx = (i-1)/R - (floor((i-1)/R) + m); % 插值点(i,j)到第(m,n)个格点的距离(亚像素精度)
- dy = (j-1)/R - (floor((j-1)/R) + n);
- w_ham_x = weight_Hamming(dx, HWS); % 权重计算使用亚像素精度(dx, dy)
- w_ham_y = weight_Hamming(dy, HWS);
- w_sinc_x = weight_Sinc(dx);
- w_sinc_y = weight_Sinc(dy);
- w = w_ham_x * w_ham_y * w_sinc_x * w_sinc_y;
- interp_val = interp_val + img_in(floor((j-1)/R) + n, floor((i-1)/R) + m) * w; % 函数值计算使用整数像素精度floor(),加权求和
- sum_w = sum_w + w;
- end
- end
- if sum_w > 0
- img_out(j, i) = interp_val / sum_w; % 权重归一化
- end
- end
- end
- end
- function w = weight_Hamming(x, half_window_size)
- % 返回指定Half Window Size的Hamming窗函数,在距离窗中心原点x(单位为1倍像素,x可以是小数)处的权重值
- % x取值范围为[-half_window_size, half_window_size], 可以是亚像素精度
- % Hamming窗的总宽度N=2*half_window_size + 1 (单位:1倍像素)
- N = 2*half_window_size + 1;
- w = 0.54 + 0.46 * cos(2 * pi * x / (N - 1)); % x从-N/2到N/2
- end
- function w = weight_Sinc(x)
- % 返回Sinc窗函数,在距离窗中心原点x(单位为1倍像素,x可以是小数)处的权重值
- % x取值范围为[-half_window_size, half_window_size], 可以是亚像素精度
- if x == 0
- w = 1;
- else
- w = sin(pi*x)./(pi*x);
- end
- end
复制代码 MATLAB代码画窗函数示例:- % 画出Hamming, Sinc, HamSinc窗函数的权重曲线
- close all;
- clear all;
- HWS_list = 3:9;
- h0 = figure;
- for HWS = HWS_list
- xlist = -HWS:0.1:HWS;
- w_ham = weight_Hamming(xlist, HWS);
- w_sinc = weight_Sinc(xlist);
- w_hamsinc = w_ham.*w_sinc;
- figure(h0);
- subplot(3,1,1);
- hold on;
- plot(xlist, w_ham, 'DisplayName', ['HWS=' num2str(HWS)]);
- hold off
- title('Hamming function weight');
- legend;
- grid on;
- subplot(3,1,2);
- hold on;
- plot(xlist, w_sinc, 'DisplayName', ['HWS=' num2str(HWS)]);
- hold off
- title('Sinc function weight');
- legend;
- grid on;
- subplot(3,1,3);
- hold on;
- plot(xlist, w_hamsinc, 'DisplayName', ['HWS=' num2str(HWS)]);
- hold off
- title('HamSinc function weight');
- xlabel('到kernel中心点的距离x,单位:1倍像素')
- legend;
- grid on;
- end
复制代码 比较HamSinc的不同参数组合[R, HWS]的插值效果
比较HamSinc与同分辨率下的bicubic, bilinear插值效果:
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |