收藏 分销(赏)

用matlab对图像进行缩放与旋转.doc

上传人:精*** 文档编号:3013549 上传时间:2024-06-13 格式:DOC 页数:7 大小:23KB 下载积分:6 金币
下载 相关 举报
用matlab对图像进行缩放与旋转.doc_第1页
第1页 / 共7页
用matlab对图像进行缩放与旋转.doc_第2页
第2页 / 共7页


点击查看更多>>
资源描述
· %======用matlab对图像进行缩放(双线性插值法) clear;   %此题是用双线性插值法实现图像缩放 I=imread('f.jpg'); %读入原图像,只需将此处的文件换成要变换的图片即可 %图像属性 %  Filename: 'f.jpg' %       FileModDate: '24-Aug-2008 16:50:30' %           FileSize: 20372 %             Format: 'jpg' %      FormatVersion: '' %              Width: 480 %             Height: 640 %           BitDepth: 8 %          ColorType: 'grayscale' %    FormatSignature: '' %    NumberOfSamples: 1 %       CodingMethod: 'Huffman' %      CodingProcess: 'Sequential' %            Comment: {} [rows,cols]=size(I); K1 = str2double(inputdlg('请输入行缩放倍数', 'INPUT scale factor', 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           % 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为整数,直接赋值        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));              % 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; imshow(Out); %===============使用matlab对图片进行缩放(最近邻域法) clear;  %此题是用最近邻域法实现图像缩放 I=imread('f.jpg');%读入图像 %图像属性 %  Filename: 'f.jpg' %       FileModDate: '24-Aug-2008 16:50:30' %           FileSize: 20372 %             Format: 'jpg' %      FormatVersion: '' %              Width: 480 %             Height: 640 %           BitDepth: 8 %          ColorType: 'grayscale' %    FormatSignature: '' %    NumberOfSamples: 1 %       CodingMethod: 'Huffman' %      CodingProcess: 'Sequential' %            Comment: {} [rows,cols]=size(I); K1 = str2double(inputdlg('请输入行缩放倍数', 'INPUT scale factor', 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 - 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));        else                                               a = double(round(oldX));                         b = double(round(oldY)); %若不是整数四舍五入后把临近值赋过去            im2(x,y) = I(a,b);                          end     end end imshow(I); %输出原图像 figure; imshow(im2); %输出缩放后图像 %====================用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 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(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     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');%保存旋转后图像 %======================用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 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(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     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');%保存旋转后图像
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 考试专区 > 中考

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服