matlab对图⽚进⾏放⼤和缩⼩
利⽤matlab 对数字图⽚进⾏放⼤缩⼩是matlab在数字图像处理上的⼀个简单的应⽤
matlab库函数imresize()的功能就是这个,那么imresize具体怎么实现的呢,我们可以⾃⼰写⼀个myimresize()
imresize()的⽤法请查看matlab的HELP,搜索“imresize”
⾸先我们必须知道彩⾊数字图像其实是⼀个m*n*3的数字矩阵组成的,其中的m*n表⽰图⽚在宽度和⾼度上的像素⼤⼩,我们通常说的320*240的普通MP3的图⽚格式就是由宽度上320个像素点和⾼度上240个像素点组成。⽽之所以乘3,是因为彩⾊采⽤的rgb(red,green,blue)的⽅式。对于图⽚上的每个点的颜⾊,都由3个数字(r,g,b)来决定,如(255,0,0)为红⾊,(0,255,0)为绿⾊。每个数字介于0~255之间(8位表⽰法)。
对图⽚的放⼤和缩⼩,也就是说根据原来的图⽚矩阵来产⽣新矩阵。对于新矩阵的每个像素点,其取值有两种⽅式:
⼀种是取对应原来位置最近的那个点的像素,这种做法失真⽐较⾼,我们称这种做法为“nearest”;
另⼀种做法是取原来位置周围四个点的加权平均值,这种做法得到的图⽚⽐较柔和,我们称这种做法为双线性,“bilinear”,根据数学推导,很容易得知:
f(x0,y0) = (1-b)*[a*f(x+1,y)+(1-a)*f(x,y)] + b*[a*f(x+1,y+1)+(1-a)*f(x,y+1)]
源代码:
function resized = myimresize(image,scale,method);
% A funciton to resize a image
% 'resized' is the result of the function, which means change the size of
% 'image' of 'scale' times with method 'method'
%'image' is the source image
% 'scale' if 'scale'>1 it means to amplify the image with 'scale' times
resized%      if 'scale' <1 it means to shrink the image with 'scale' times
% 'method' if method = 'nearest' it will find the nearest point of 'image' to
% write into resized
%    else it means the 'bilinear' way
%    All right reserved by Pengxc
if strcmp(method, 'nearest')==1;
% the first method
[length,height,layer] = size(image);  % get the basic size of the image
new_lenth  = length * scale;          % New lenth
new_height = height * scale;        % New height
new_lenth = floor(new_lenth);        % make it to int
new_height = floor(new_height);      % make it to int
%The code below is to find the nearest piont
for i = 1:new_lenth;
for j = 1:new_height;
remain_i = (i/scale) - floor(i/scale);
% To see which side is nearer at x-label
if remain_i >= 0.5
o_i = ceil(i/scale);
else
o_i = floor(i/scale);
% When scale>1 and i =1 ,then o_i = 0,which is wrong,so
% make it equals 1
if o_i == 0;
o_i =1;
end
end
remain_j = (j/scale) - floor(i/scale);
% To see which side is nearer at y-label
if remain_j >= 0.5
o_j = ceil(j/scale);
else
o_j = floor(j/scale);
if o_j == 0;
% When scale>1 and i =1 ,then o_i = 0,which is wrong,so
% make it equals 1
o_j =1;
end
end
for k =1:layer
resized(i,j,k) = image(o_i,o_j,k);
end
end
end
end
if strcmp(method, 'bilinear')==1;  %    else it means the 'bilinear' way
[lenth,height,layer] = size(image);  % the same as  above
new_lenth = lenth * scale;
new_height = height * scale;
new_lenth = floor(new_lenth);
new_height = floor(new_height);
for i= 1 : lenth
for j = 1 :height
for k = 1: layer
temp_image(i,j,k) = image(i,j,k);
%temp_image has a row ans a column more than image
end
end
end
for i=1:lenth
for k =1:layer
temp_image(i,height+1,k) = 0;
% add a column to keep from getting out of matrix
end
end
for j =1:height
for k=1:layer
temp_image(lenth+1,j,k)=0;
% add a row to keep from getting out of matrix
end
end
% The code below use Bilinear to Calulate the value of the resized
for i=1:new_lenth
for j =1:new_height
a = 0;
b = 0;
o_i = floor(i/scale);
o_j = floor(j/scale);
a = (i/scale) - floor(i/scale);
b = (j/scale) - floor(j/scale);
%a,b is the parameter, which will be detailly written in the Document
if o_i == 0;
o_i = 1;a=0;
end
if o_j == 0;
o_j =1;b=0;
end
for k =1:layer
resized(i,j,k) = (1-a)*(1-b)*temp_image(o_i,o_j,k) +(1-a)*b*temp_image(o_i,o_j+1,k) + a*(1-b)*temp_image(o_i+1,o_j,k) +a*b*temp_image(o_i+1,o_j+1,k);
end
end
end
end
测试与结果:
命令⾏敲⼊
I = imread(‘football.jpg’);
MSH=myimresize(I,0.7,’nearest’);
SH = imredize(I,0.7,’nearest’);
imshow(MSH);
figure,imshow(SH);
即可得到结果
左图是本段代码对“nearest”⽅式缩⼩0.7倍的测试,右边是库函数