打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用matlab对图像进行缩放与旋转

%======用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('请输入列缩放倍数', 'INPUTscale 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('请输入列缩放倍数', 'INPUTscale 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;
fori=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;
fori=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');%保存旋转后图像





本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
VB MSHFLEXGRID MSHFLEXGRID 绑定 CHECKBOX
投资大师的二十三个习惯
霖: Matlab中的图像数据类型转换
matlab读取写入图像数据格式uint8,double
【FLASH教程】蝶儿蝶儿满天飞
Lua代码编写规范
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服