
注册领666大礼包
谢谢啊 我要了
好
好
偶然遇到,送给你!
很多有关图像处理的书都会讲到双线性插值法,这个算法是图像处理中最基本的一个算法.因为一位大四姐姐的毕业设计要涉及到这个算法,而且又不能直接使用MATLAB本身现成的函数,于是我在MATLAB上实现了一个,所用的MATLAB版本是6.1,输入图像是一幅tif格式256x256的美女图,文件名为"mm.tif",输出图像大小为512x512,当然也可以是其它的比例.最大允许比例是放大5倍. 为了便于查看,注释都用浅色突出显示.其文曰:
% THIS PROGRAMME IS WRITTEN BY Rockins
% ITS FEATURE IS INTERPLOT THE SOUCE-IMAGE TO GET A DESTINATE-IMAGE
% THE MAXIMUM SCALOR == 5
% Copyright 2005,All Copyright Reserved by Rockins
% You can redistibute this programme under the GNU Less GPL license
% If you have any question about this programme,please contract to ybc2084@163.com
% output image width and height are both 512 pixel
width = 512; % well,512x512 is just for convenience,you can let it to be 300x300,400x400,or even 512x1024,etc.
height = 512;
J = uint8(zeros(width,height));
% read image 'mm.tif' into memory,and get the primitive rows and cols
I=imread('mm.tif');
imshow(I);
[nrows,ncols]=size(I);
% width scalor and height scalor
widthScale = nrows/width;
heightScale = ncols/height;
% double-linear interplot
for x = 5:width - 5 % this index range is to avoid exceeding the permitted matrix index
for y = 5:height - 5
xx = x * widthScale; % xx and yy are the source ordinate,while x and y are the destinate ordinate
yy = y * heightScale;
if (xx/double(uint16(xx)) == 1.0) & (yy/double(uint16(yy)) == 1.0) % if a and b is integer,then J(x,y) <- I(x,y)
J(x,y) = I(int16(xx),int16(yy));
else % a or b is not integer
a = double(uint16(xx)); % (a,b) is the base-dot
b = double(uint16(yy));
x11 = double(I(a,b)); % x11 <- I(a,b)
x12 = double(I(a,b+1)); % x12 <- I(a,b+1)
x21 = double(I(a+1,b)); % x21 <- I(a+1,b)
x22 = double(I(a+1,b+1)); % x22 <- I(a+1,b+1)
J(x,y) = uint8( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12) ); % calculate J(x,y)
end
end
end
% show the interplotted image
figure;
imshow(J);
因为最近问这个的人比较多,我干脆把最近邻域也贴上来:
% THIS PROGRAMME IS WRITTEN BY Rockins
% THE FEATURE IS BILINEAR-INTERPLOT THE SOUCE-IMAGE TO GET A DESTINATE-IMAGE
% THE MAXIMUM SCALOR == 5.0, THE MINIMUM SCALOR == 0.2
% Copyright 2006,All Copyrights Reserved by Rockins
% You can redistibute this programme under the GNU Less GPL license
% If you have any question about this programme,please contact to ybc2084@163.com
% read source image into memory,and get the primitive rows and cols
I=imread('lena.jpg');
[nrows,ncols]=size(I);
% Next line is the scale-factor,the range is 0.2-5.0
K = str2double(inputdlg('please input scale factor (must between 0.2 - 5.0)', 'INPUT scale factor', 1, {'0.5'}));
% Validating
if (K < 0.2) | (K > 5.0)
errordlg('scale factor beyond permitted range(0.2 - 5.0)', 'ERROR');
error('please input scale factor (must between 0.2 - 5.0)');
end
% display source image
imshow(I);
% output image width and height are both scaled by factor K
width = K * nrows; % well,512x512 is just for convenience,you can let it to be 300x300,400x400,or even 512x1024,etc.
height = K * ncols;
J = uint8(zeros(width,height));
% width scalor and height scalor
widthScale = nrows/width;
heightScale = ncols/height;
% bilinear interplot
for x = 5:width - 5 % this index range is to avoid exceeding the permitted matrix index
for y = 5:height - 5
xx = x * widthScale; % xx and yy are the source ordinate,while x and y are the destinate ordinate
yy = y * heightScale;
if (xx/double(uint16(xx)) == 1.0) & (yy/double(uint16(yy)) == 1.0) % if a and b is integer,then J(x,y) <- I(x,y)
J(x,y) = I(int16(xx),int16(yy));
else % a or b is not integer
a = double(round(xx)); % (a,b) is the base-dot
b = double(round(yy));
J(x,y) = I(a,b); % calculate J(x,y)
end
end
end
% show the interplotted image
imwrite(J, 'lena2.jpg', 'jpg');
figure;
imshow(J);
附件中有源代码和测试图像,如果不能下载请发邮件给我:ybc2084@163.com
感谢Rockins网友!
是的,写出来就给分。谢谢了。
TOP