数字图像处理实验(4):PROJECT 02-04 [Multiple Uses],Zooming and Shrinking Images by Bilinear Interpolation 标签: 图像处理MATLAB

实验要求:

Zooming and Shrinking Images by Bilinear Interpolation
Objective
To manipulate another technique of zooming and shrinking images by bilinear interpolation.
Main requirements:
Ability of programming with C, C++, or Matlab.
Instruction manual:
(a) Write a computer program capable of zooming and shrinking an image by bilinear interpolation. The input to your program is the desired size of the resulting image in the horizontal and vertical direction. You may ignore aliasing effects.
(b) Download Fig. 2.19(a) and use your program to shrink this image from 1024 x 1024 to 256 x 256 pixels.
(c) Use your program to zoom the image in (b) back to 1024 x 1024. Explain the reasons for their differences.

这次实验通过自己编写双线性内插法的程序来对图像进行缩放。
简要介绍一下双线性内插法的原理:
这里写图片描述
第一种情况:

  1. 点(i-b, j-a)与点(i-b, j+rate-a)插值得到点(1-b, j)处的灰度值;
  2. 点(i+rate-b, j-a)与点(i+rate-b, j+rate-a)插值得到点(i+rate-b, j)处的灰度值;
  3. 使用前面得到的结果,即点(i-b, j)与点(i+rate-b, j)进行插值得到点(i,j)处的灰度值。

第二种情况:

  1. 点(i-b, j-a)与点(i+rate-b, j-a)插值得到点(i, j-a)处的灰度值;
  2. 点(i-b, j+rate-a)与点(i+rate-b, j+rate-a)插值得到点(i, j+rate-a)处的灰度值;
  3. 使用前面得到的结果,即点(i, j-a)与点(i, j+rate-a)进行插值得到点(i,j)处的灰度值。

从示意图来看比较好理解了,详细的资料还可以参看下面的资料链接:
百度百科:双线性插值
双线性插值(Bilinear Interpolation)

代码部分:

matlab 函数文件名: Bilinear_Interpolation.m

%%

function img=Bilinear_Interpolation(a,rate) 
[m,n]=size(a); 
ratex=rate-1;  
img=zeros(m+ratex*(m-1),n+ratex*(n-1)); 
for i=1:m                        
    for j=1:n         
        img(i+ratex*(i-1),j+ratex*(j-1))=a(i,j);  
    end;
end; 
img=double(img); 
for i=1:m+ratex*(m-1) 
    for j=1:n+ratex*(n-1)    
        a=mod(j-1,rate);         
        b=mod(i-1,rate);         
        if a~=0 && b==0         
            img(i,j)=round((a*img(i,j+(rate-a))+(rate-a)*img(i,j-a))/rate);   
        end;      
        if a==0 && b~=0       
            img(i,j)=round((b*img(i+(rate-b),j)+(rate-b)*img(i-b,j))/rate);   
        end;       
        if a~=0 && b~=0           
            img(i,j)=round((b*(a*img(i-b,j+(rate-a))+(rate-a)*img(i-b,j-a))/rate+(rate-b)*(a*img(i+(rate-b),j+(rate-a))+(rate-a)*img(i+(rate-b),j-a))/rate)/rate);       
        end;    
    end; 
end;
img=uint8(img);

程序中调用编写好的Bilinear_Interpolation函数,查看结果:

%%
    clear all;
    clc;
    close all;

%%
    img_name = 'general_img_1024.jpg'
    img = imread(img_name);
    figure(1)
    imshow(img)
    title('原图像');

    img1 = imresize(img, [256, 256]);
    figure(2)
    imshow(img1)

    img2 = Bilinear_Interpolation(img1, 4);

    figure(3)
    imshow(img2)
    title('双线性内插法放大')

 %%
    [a, b] = size(img);
    n = max(a, b);
    c = 1024 / n;
    img3 = Bilinear_Interpolation(img1, c);
    figure(4)
    imshow(img3)
    title('双线性内插法缩小')

运行结果:
这里写图片描述

这里写图片描述

原文链接: https://www.cnblogs.com/xuhongbin/p/7134170.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    数字图像处理实验(4):PROJECT 02-04 [Multiple Uses],Zooming and Shrinking Images by Bilinear Interpolation 标签: 图像处理MATLAB

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/252947

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午6:29
下一篇 2023年2月14日 上午6:31

相关推荐