大家好,又见面了,我是你们的朋友全栈君。
双线性插值,是一种比较重要的插值方法,尤其在数字图像处理领域。本篇博文分为三个部分:一是双线性插值的算法推导,二是双线性插值的算法实现,三是算法的运行结果。
一 双线性插值的算法推导
二 代码实现(matlab)
function [out] = bilinearInterpolation(im, out_dims)
in_rows = size(im,1);
in_cols = size(im,2);
out_rows = out_dims(1);
out_cols = out_dims(2);
S_R = in_rows / out_rows;
S_C = in_cols / out_cols;
[cf, rf] = meshgrid(1 : out_cols, 1 : out_rows);
rf = rf * S_R;
cf = cf * S_C;
r = floor(rf);
c = floor(cf);
r(r < 1) = 1;
c(c < 1) = 1;
r(r > in_rows - 1) = in_rows - 1;
c(c > in_cols - 1) = in_cols - 1;
delta_R = rf - r;
delta_C = cf - c;
in1_ind = sub2ind([in_rows, in_cols], r, c);
in2_ind = sub2ind([in_rows, in_cols], r+1,c);
in3_ind = sub2ind([in_rows, in_cols], r, c+1);
in4_ind = sub2ind([in_rows, in_cols], r+1, c+1);
out = zeros(out_rows, out_cols, size(im, 3));
out = cast(out, class(im));
for idx = 1 : size(im, 3)
chan = double(im(:,:,idx)); %// Get i'th channel
%// Interpolate the channel
tmp = chan(in1_ind).*(1 - delta_R).*(1 - delta_C) + ...
chan(in2_ind).*(delta_R).*(1 - delta_C) + ...
chan(in3_ind).*(1 - delta_R).*(delta_C) + ...
chan(in4_ind).*(delta_R).*(delta_C);
out(:,:,idx) = cast(tmp, class(im));
end
三 双线性插值运行结果
>>I = imread(‘lena.jpg’); >> figure,imshow(I) >> S = bilinearInterpolation(I,[1000,1000]); >> figure,imshow(S)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133667.html原文链接:https://javaforall.cn