失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > matlab对图片进行放大和缩小

matlab对图片进行放大和缩小

时间:2021-01-23 15:49:39

相关推荐

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 %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 imagenew_lenth= length * scale;% New lenthnew_height = height * scale;% New heightnew_lenth = floor(new_lenth);% make it to intnew_height = floor(new_height);% make it to int%The code below is to find the nearest piontfor i = 1:new_lenth;for j = 1:new_height;remain_i = (i/scale) - floor(i/scale);% To see which side is nearer at x-labelif remain_i >= 0.5o_i = ceil(i/scale);elseo_i = floor(i/scale);% When scale>1 and i =1 ,then o_i = 0,which is wrong,so% make it equals 1if o_i == 0;o_i =1;endendremain_j = (j/scale) - floor(i/scale);% To see which side is nearer at y-labelif remain_j >= 0.5o_j = ceil(j/scale);elseo_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 1o_j =1;endendfor k =1:layerresized(i,j,k) = image(o_i,o_j,k);endendend endif strcmp(method, 'bilinear')==1;%else it means the 'bilinear' way[lenth,height,layer] = size(image);% the same asabovenew_lenth = lenth * scale;new_height = height * scale;new_lenth = floor(new_lenth);new_height = floor(new_height);for i= 1 : lenthfor j = 1 :heightfor k = 1: layertemp_image(i,j,k) = image(i,j,k);%temp_image has a row ans a column more than imageendendendfor i=1:lenthfor k =1:layertemp_image(i,height+1,k) = 0;% add a column to keep from getting out of matrixendendfor j =1:heightfor k=1:layertemp_image(lenth+1,j,k)=0;% add a row to keep from getting out of matrixendend% The code below use Bilinear to Calulate the value of the resizedfor i=1:new_lenthfor j =1:new_heighta = 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 Documentif o_i == 0;o_i = 1;a=0;endif o_j == 0;o_j =1;b=0;endfor k =1:layerresized(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);endendendend

测试与结果:

命令行敲入 I = imread(‘football.jpg’); MSH=myimresize(I,0.7,’nearest’); SH = imredize(I,0.7,’nearest’); imshow(MSH); figure,imshow(SH);

即可得到结果

左图是本段代码对“nearest”方式缩小0.7倍的测试,右边是库函数

如果觉得《matlab对图片进行放大和缩小》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。