【图像处理】双三次插值(Bicubic interpolation)原理及matlab简易版代码

先简单写下。

双线性插值:缩放后图像矩阵(简称TI)像素坐标映射到原图像矩阵(简称OI)中得坐标点P(x,y),P点临近四个坐标点像素值的线性加权求和即P点像素值。

“双”指的图像为二维矩阵,则在x方向和y方向都线性加权求和,顺序无所谓。

双三次插值

以下为双三次插值的公式,其中s(x)为插值核。f(M)为对应缩放后矩阵坐标点的像素值。

 

双三次插值matlab简易版代码

        以下代码非常简洁,仅仅是将公式编码完成了最基本的计算,给大家做个参考,最终运行时间为23.200182 秒,可长了...

     (话说matlab自带的imresize方法进行双三次插值运行0.851554 秒 = =)

% Author: Dabao
% Time: 2019.03.28 16:01
 
    tic; % calculate running time
    
    % read original image I
    I = imread('/Users/apple/Downloads/IMG_3331.JPG');
    I = double(I);
    [oh,ow,od] = size(I);
    zmf = 2; %缩放因子
    
    % initial target image TI
    th = round(oh*zmf);
    tw = round(ow*zmf);
    TI = zeros(th,tw,od); %预分配内存提高计算速度
    
    % add original image with 2 rows and 2 cols
    % expand the border to prevent calculation overflow
    a = I(1,:,:); b = I(oh,:,:);
    temp_I = [a;a;I;b;b];
    c = temp_I(:,1,:); d = temp_I(:,ow,:);
    FI = [c,c,temp_I,d,d];
    
    % fill target image with new pixels
    for w = 1:tw
        j = floor(w/zmf)+2; v = rem(w,zmf)/zmf;
        for h = 1:th
            i = floor(h/zmf)+2;  u = rem(h,zmf)/zmf; 
            A = [s(u+1),s(u),s(u-1),s(u-2)];
            C = [s(v+1);s(v);s(v-1);s(v-2)];
            for d = 1:od   % image's 3 channels    
               B = FI(i-1:i+2,j-1:j+2,d);
               TI(h,w,d) = A*B*C;    
            end
        end
    end
    
    figure;
    imshow(uint8(TI));
    toc;
% 插值核函数
function w = s(wx)
    wx = abs(wx);
    if wx<1
        w = 1 - 2*wx^2 + wx^3;
    elseif wx>=1 && wx<2
        w = 4 - 8*wx + 5*wx^2 - wx^3;
    else
        w = 0;
    end

 

  • 9
    点赞
  • 70
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
以下是双三次插值图像超分辨率的MATLAB代码: ```matlab clear all; clc; close all; %% Load the low resolution image % Replace 'LR_image.png' with the name of your low resolution image LR_image = imread('LR_image.png'); %% Convert to double precision LR_image = im2double(LR_image); %% Set the scaling factor % Replace 'scale_factor' with the scaling factor you desire scale_factor = 2; %% Create a bicubic interpolation kernel % This will be used to generate the high resolution image kernel = 'cubic'; %% Upsample the image using bicubic interpolation % This will be used as the initial estimate for the high resolution image HR_image_bicubic = imresize(LR_image, scale_factor, kernel); %% Display the low resolution and initial high resolution images figure(); subplot(1,2,1); imshow(LR_image); title('Low Resolution Image'); subplot(1,2,2); imshow(HR_image_bicubic); title('Initial High Resolution Image (Bicubic Interpolation)'); %% Set the parameters for the iterative optimization num_iterations = 10; % Replace with the number of iterations you desire lambda = 0.01; % Replace with the value of lambda you desire %% Perform the iterative optimization for i = 1:num_iterations % Compute the gradient of the objective function grad = compute_gradient(LR_image, HR_image_bicubic, lambda); % Update the high resolution image estimate HR_image_bicubic = HR_image_bicubic - grad; % Clip the high resolution image estimate to the valid range of intensities HR_image_bicubic(HR_image_bicubic < 0) = 0; HR_image_bicubic(HR_image_bicubic > 1) = 1; % Display the current high resolution image estimate figure(); imshow(HR_image_bicubic); title(['High Resolution Image Estimate (Iteration ', num2str(i), ')']); end %% Display the final high resolution image estimate figure(); imshow(HR_image_bicubic); title('Final High Resolution Image Estimate'); %% Compute the gradient of the objective function function grad = compute_gradient(LR_image, HR_image, lambda) % Compute the Laplacian of the high resolution image laplacian = del2(HR_image); % Compute the difference between the low resolution image and the downsampled high resolution image diff = imresize(HR_image, size(LR_image), 'bicubic') - LR_image; % Compute the gradient of the objective function grad = lambda * laplacian + 2 * diff; end ``` 请注意,这只是一个示例代码,并且可能需要根据您的需求进行修改。此代码使用双三次插值和梯度下降优化来生成高分辨率图像的估计值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值