Here , we are going to use predesign matlab code for bilinear interpolation that i have been found from web. After that , we will call this matlab code in our upsampling program.Create that following code as bilinearInterpolation.m in your workplace that you will write upsampling program. They should be in the same directory.
function[out]=bilinearInterpolation(im,out_dims)
%// Get some necessary variables first
in_rows=size(im,1);
in_cols=size(im,2);
out_rows=out_dims(1);
out_cols=out_dims(2);
%// Lets S_R = R/R'
S_R = in_rows / out_rows;
%// Lets S_C = C/C'
S_C = in_cols / out_cols;
%// Define grid of co-ordinates in our image
%// Generate (x,y) pairs for each point in our image
[cf,rf]=meshgrid(1:out_cols,1:out_rows);
rf=rf*S_R;
cf=cf*S_C;
r=floor(rf);
c=floor(cf);
%// Any values out of range , cap
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;
% Final line algorithm
%get column major indices for each point we wish to access
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);
% now interpolate
% go through each channel for the case of colour
% create output image that is the same class as input
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
and finally following code is the upsampling matlab code , there is a scaled result of the image that i have chosen for my study called space elevator.
clear
close all
im=imread('SpaceElevator.jpg');
Rim = im(:,:,1);
Gim = im(:,:,2);
Bim = im(:,:,3);
% Applying upsampling in levels 2,4 to our test image
% Determine the dimensions of the source image
[j k]=size(Rim);
% Determine how much larger we want the new image(should be
% an integer)
scale=2;
Rimup=bilinearInterpolation(Rim,[j*scale k*scale]);
Gimup=bilinearInterpolation(Gim,[j*scale k*scale]);
Bimup=bilinearInterpolation(Bim,[j*scale k*scale]);
imup1=cat(3,Rimup,Gimup,Bimup);
scale=4;
Rimup=bilinearInterpolation(Rim,[j*scale k*scale]);
Gimup=bilinearInterpolation(Gim,[j*scale k*scale]);
Bimup=bilinearInterpolation(Bim,[j*scale k*scale]);
imup2=cat(3,Rimup,Gimup,Bimup);
figure(45);
subplot(3,1,1);imagesc(im);
subplot(3,1,2);imagesc(imup1);
subplot(3,1,3);imagesc(imup2);
Hiç yorum yok:
Yorum Gönder
konuyla ilgili yorumlarınızı bu alana yazabilirsiniz.
you can write your comments to this area about the topic.
potete scrivere i vostri commenti a questa zona circa argomento