Demo entry 6625704

Zoom

   

Submitted by anonymous on Jun 21, 2017 at 19:40
Language: Matlab. Code size: 1.5 kB.

%Bilinear Interpolation to Zoom in or out the image
%Autor: XuLei
%Date: 2017/6/9
%Use Matlab Vector Operate to Speed Up !
clear;
clc;
scale = 1.0;%缩放尺度,大于1为放大,小于1为缩小.
img_raw = imread('..\resources\coffe.jpg');
[X_size,Y_size,D_size] = size(img_raw);%原始图像的大小
X_size_new = floor(X_size*scale);
Y_size_new = floor(Y_size*scale);

%增加一行一列,循环时候用,保证不会越界
resize_img_raw = zeros(X_size+1,Y_size+1,D_size);
resize_img_raw(1:X_size,1:Y_size,:) = img_raw(:,:,:);
resize_img_raw(X_size+1,1:Y_size,:) = img_raw(X_size,1:Y_size,:);
resize_img_raw(1:X_size,Y_size+1,:) = img_raw(:,Y_size,:);
resize_img_raw(X_size+1,Y_size+1,:) = img_raw(X_size,Y_size,:);

%新图像
new_img = zeros(X_size_new,Y_size_new,D_size);

%系数向量
temp_i = 1:X_size_new;
temp_j = 1:Y_size_new;

tic;%计时用

%新图像对应原始图像x,y数据
temp_i = (temp_i-ones(size(temp_i)))./scale+ones(size(temp_i));
temp_j = (temp_j-ones(size(temp_j)))./scale+ones(size(temp_j));

%整数部分
ii = floor(temp_i);
jj = floor(temp_j);

%小数部分
u = temp_i -ii;
v = temp_j -jj;

[U,V] = meshgrid(u,v);
%U为行号的小数部分,V为列数的小数部分,U按列来对应,V按行对应,所以需要转置.
for i=1:3
	temp_x1 = resize_img_raw(ii,jj,i) + U'.*(resize_img_raw(ii+ones(size(ii)),jj,i)-resize_img_raw(ii,jj,i));
	temp_x2 = resize_img_raw(ii,jj+ones(size(jj)),i) + U'.*(resize_img_raw(ii+ones(size(ii)),jj+ones(size(jj)),i)-resize_img_raw(ii,jj+ones(size(jj)),i));
	new_img(1:X_size_new,1:Y_size_new,i) =temp_x1 + V'.*(temp_x2-temp_x1);
end
toc;
new_img = uint8(new_img);
imshow(new_img);

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).