利用matlab 对数字图片进行放大缩小是matlab在数字图像处理上的一个简单的应用
% 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
[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
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”,根据数学推导,很容易得知:
源代码:
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
% 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;
end
if strcmp(method, 'bilinear')==1; % else it means the 'bilinear' way
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倍的测试,右边是库函数