找回密码
 立即注册
首页 业界区 安全 图像插值原理概述与HamSinc插值方法示例

图像插值原理概述与HamSinc插值方法示例

岑韬哎 7 天前
概述

本文介绍图像插值一般性原理,并且对常用插值函数的插值效果进行对比。
插值一般原理

原图像可以视为整数像素下的二维矩阵,所谓插值,就是在分数坐标处给原图插入新的值。这个新值根据一定的计算方法,由原图在其周围的有限范围(插值函数作用域)内的所有整数像素下的值组合生成,与其领域内的原图像素具有强关联性。
以下是插值的一般公式:
1.png

2.png

3.png

附一张插值原理详细的解释图:
4.png

插值窗函数 w(x,y)

5.png

6.png

7.png

MATLAB代码:
定义HamSinc插值方法:
  1. function img_out = hammingSincInterpolation(img_in, R, HWS)
  2.     % [列,行]: (i, j)  (x, y)  (m,n)
  3.     n_row0 = size(img_in, 1);
  4.     n_col0 = size(img_in, 2);
  5.     n_row = R * n_row0;
  6.     n_col = R * n_col0;
  7.     img_out = zeros(n_row, n_col);
  8.     for j = 1 : n_row
  9.         for i = 1 : n_col
  10.             interp_val = 0;
  11.             sum_w = 0;
  12.             for n = -HWS:HWS
  13.                 for m = -HWS:HWS
  14.                     % 超过原始边界则路过:
  15.                     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
  16.                         continue;
  17.                     end
  18.                     dx = (i-1)/R - (floor((i-1)/R) + m);  % 插值点(i,j)到第(m,n)个格点的距离(亚像素精度)
  19.                     dy = (j-1)/R - (floor((j-1)/R) + n);
  20.                     w_ham_x = weight_Hamming(dx, HWS);   % 权重计算使用亚像素精度(dx, dy)
  21.                     w_ham_y = weight_Hamming(dy, HWS);
  22.                     w_sinc_x = weight_Sinc(dx);
  23.                     w_sinc_y = weight_Sinc(dy);
  24.                     w = w_ham_x * w_ham_y * w_sinc_x * w_sinc_y;
  25.                     interp_val = interp_val + img_in(floor((j-1)/R) + n, floor((i-1)/R) + m) * w; % 函数值计算使用整数像素精度floor(),加权求和
  26.                     sum_w = sum_w + w;
  27.                 end
  28.             end
  29.             if sum_w > 0
  30.                 img_out(j, i) = interp_val / sum_w;   % 权重归一化
  31.             end
  32.         end
  33.     end
  34. end
  35. function w = weight_Hamming(x, half_window_size)
  36.     % 返回指定Half Window Size的Hamming窗函数,在距离窗中心原点x(单位为1倍像素,x可以是小数)处的权重值
  37.     % x取值范围为[-half_window_size, half_window_size], 可以是亚像素精度
  38.     % Hamming窗的总宽度N=2*half_window_size + 1 (单位:1倍像素)
  39.     N = 2*half_window_size + 1;
  40.     w = 0.54 + 0.46 * cos(2 * pi * x / (N - 1));    % x从-N/2到N/2
  41. end
  42. function w = weight_Sinc(x)
  43.     % 返回Sinc窗函数,在距离窗中心原点x(单位为1倍像素,x可以是小数)处的权重值
  44.     % x取值范围为[-half_window_size, half_window_size], 可以是亚像素精度
  45.     if x == 0
  46.         w = 1;
  47.     else
  48.         w = sin(pi*x)./(pi*x);
  49.     end
  50. end
复制代码
MATLAB代码画窗函数示例:
  1. % 画出Hamming, Sinc, HamSinc窗函数的权重曲线
  2. close all;
  3. clear all;
  4. HWS_list = 3:9;
  5. h0 = figure;
  6. for HWS = HWS_list
  7.     xlist = -HWS:0.1:HWS;
  8.     w_ham = weight_Hamming(xlist, HWS);
  9.     w_sinc = weight_Sinc(xlist);
  10.     w_hamsinc = w_ham.*w_sinc;
  11.     figure(h0);
  12.     subplot(3,1,1);
  13.     hold on;
  14.     plot(xlist, w_ham, 'DisplayName', ['HWS=' num2str(HWS)]);
  15.     hold off
  16.     title('Hamming function weight');
  17.     legend;
  18.     grid on;
  19.     subplot(3,1,2);
  20.     hold on;
  21.     plot(xlist, w_sinc, 'DisplayName', ['HWS=' num2str(HWS)]);
  22.     hold off
  23.     title('Sinc function weight');
  24.     legend;
  25.     grid on;
  26.     subplot(3,1,3);
  27.     hold on;
  28.     plot(xlist, w_hamsinc, 'DisplayName', ['HWS=' num2str(HWS)]);
  29.     hold off
  30.     title('HamSinc function weight');
  31.     xlabel('到kernel中心点的距离x,单位:1倍像素')
  32.     legend;
  33.     grid on;
  34. end
复制代码
比较HamSinc的不同参数组合[R, HWS]的插值效果

8.png

比较HamSinc与同分辨率下的bicubic, bilinear插值效果:

9.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册