1、· %======用matlab对图像进行缩放(双线性插值法) clear; %此题是用双线性插值法实现图像缩放 I=imread('f.jpg'); %读入原图像,只需将此处的文件换成要变换的图片即可 %图像属性 % Filename: 'f.jpg' % FileModDate: '24-Aug-2008 16:50:30' % FileSize: 20372 % Format: 'jpg' % FormatVersion: '' % Width: 480 %
2、 Height: 640 % BitDepth: 8 % ColorType: 'grayscale' % FormatSignature: '' % NumberOfSamples: 1 % CodingMethod: 'Huffman' % CodingProcess: 'Sequential' % Comment: {} [rows,cols]=size(I); K1 = str2double(inputdlg('请输入行缩放倍数', 'INPUT scale fa
3、ctor', 1, {'0.5'}));%行默认变为原来的0.5倍 K2 = str2double(inputdlg('请输入列缩放倍数', 'INPUT scale factor', 1, {'0.4'}));%列默认变为原来的0.4倍 width = K1 * rows; height = K2 * cols; Out = uint8(zeros(width,height)); %创建输出图像矩阵 widthScale = rows/width; heightScale = cols/height; for x = 6:width - 6
4、 % 6是为了防止矩阵超出边界溢出 for y = 6:height - 6 oldX = x * widthScale; % oldX,oldY为原坐标,x,y为新坐标 oldY = y * heightScale; if (oldX/double(uint16(oldX)) == 1.0) & (oldY/double(uint16(oldY)) == 1.0) Out(x,y) = I(int16(oldX),int16(oldY));%若oldX,oldY为整数,直
5、接赋值 else a = double(uint16(oldX)); b = double(uint16(oldY)); 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));
6、 % x21 赋值为 I(a+1,b) x22 = double(I(a+1,b+1)); % x22 赋值为 I(a+1,b+1) Out(x,y) = uint8( (b+1-oldY) * ((oldX-a)*x21 + (a+1-oldX)*x11) + (oldY-b) * ((oldX-a)*x22 +(a+1-oldX) * x12) ); % 用双线性插值计算公式计算 end end end imshow(I); figure; im
7、show(Out); %===============使用matlab对图片进行缩放(最近邻域法) clear; %此题是用最近邻域法实现图像缩放 I=imread('f.jpg');%读入图像 %图像属性 % Filename: 'f.jpg' % FileModDate: '24-Aug-2008 16:50:30' % FileSize: 20372 % Format: 'jpg' % FormatVersion: '' % Width: 480 %
8、 Height: 640 % BitDepth: 8 % ColorType: 'grayscale' % FormatSignature: '' % NumberOfSamples: 1 % CodingMethod: 'Huffman' % CodingProcess: 'Sequential' % Comment: {} [rows,cols]=size(I); K1 = str2double(inputdlg('请输入行缩放倍数', 'INPUT scale fact
9、or', 1, {'0.6'}));%行默认变为原来的0.6倍 K2 = str2double(inputdlg('请输入列缩放倍数', 'INPUT scale factor', 1, {'0.4'}));%列默认变为原来的0.4倍 width = K1 * rows; height = K2 * cols; im2 = uint8(zeros(width,height)); %定义输出图像矩阵 widthScale = rows/width; heightScale = cols/height; for x = 6:width
10、 6 %为防止矩阵溢出而选择的参数6 for y = 6:height - 6 oldX = x * widthScale; %oldX,oldY为原坐标,x,y为新坐标 oldY = y * heightScale; if (oldX/double(uint16(oldX)) == 1.0) & (oldY/double(uint16(oldY)) == 1.0) im2(x,y) = I(int16(oldX),int16(oldY));
11、 else a = double(round(oldX)); b = double(round(oldY)); %若不是整数四舍五入后把临近值赋过去 im2(x,y) = I(a,b); end end end imshow(I); %输出原图像 figure; imshow(im2); %输出缩放后图像 %==============
12、用matlab对图像进行旋转(双线性插值法) clear;%此题是用最近邻域法实现图像旋转 im1=imread('b.jpg'); [m,n,p]=size(im1); % 将图像旋转30度 a=0.5; %a=sin30=0.5 b=0.866; %b=cos30=0.866 row=n*a+m*b; col=n*b+m*a; for i=1:row %先把图象填充成全黑 for j=1:col im2(i,j,:)=uint8(0); end end
13、for i=1:m %把原图象像素点旋转后变为新图象点 for j=1:n xx=round(abs((i-m/2)*b-(j-n/2)*a+row/2)); yy=round(abs((i-m/2)*a+(j-n/2)*b+col/2)); for k=1:3 im2(xx,yy,k)=im1(i,j,k); end end end temp1=uint8(0); temp2=uint8(
14、0); temp3=uint8(0); for i=1:row %把画面上的空点按照最近邻插值法填充 temp1=uint8(0); temp2=uint8(0); temp3=uint8(0); for j=1:col %找到最右的图象边界点 if (im2(i,j,:)==uint8(0)) else kk=j; end
15、 end for j=1:kk if (im2(i,j,:)==uint8(0)) im2(i,j,1)=temp1; im2(i,j,2)=temp2; im2(i,j,3)=temp3; else temp1=im2(i,j,1); temp2=im2(i,j,2); temp3=im2(i,j,3); end end end im
16、show(im1); figure; imwrite(im1,'5.jpg'); %保存原图像 imshow(im2); imwrite(im2,'6.jpg');%保存旋转后图像 %======================用matlab对图片进行旋转(最近邻域法) clear;%此题是用最近邻域法实现图像旋转 im1=imread('b.jpg'); [m,n,p]=size(im1); % 将图像旋转30度 a=0.5; %a=sin30=0.5 b=0.866; %b=cos30=0.866 row=n*a+m*b; col=n*b+m*a; for
17、 i=1:row %先把图象填充成全黑 for j=1:col im2(i,j,:)=uint8(0); end end for i=1:m %把原图象像素点旋转后变为新图象点 for j=1:n xx=round(abs((i-m/2)*b-(j-n/2)*a+row/2)); yy=round(abs((i-m/2)*a+(j-n/2)*b+col/2));
18、 for k=1:3 im2(xx,yy,k)=im1(i,j,k); end end end temp1=uint8(0); temp2=uint8(0); temp3=uint8(0); for i=1:row %把画面上的空点按照最近邻插值法填充 temp1=uint8(0); temp2=uint8(0); temp3=uint8(0); for j=1:col
19、 %找到最右的图象边界点 if (im2(i,j,:)==uint8(0)) else kk=j; end end for j=1:kk if (im2(i,j,:)==uint8(0)) im2(i,j,1)=temp1; im2(i,j,2)=temp2; im2(i,j,3)=temp3; else temp1=im2(i,j,1); temp2=im2(i,j,2); temp3=im2(i,j,3); end end end imshow(im1); figure; imwrite(im1,'5.jpg'); %保存原图像 imshow(im2); imwrite(im2,'6.jpg');%保存旋转后图像






