Felix

技术源于积累,成功始于执着! 个人邮箱:justlxy@mail.dhu.edu.cn QQ:1576109464

图像插值算法研究——算法效果比较(Matlab验证)

0
阅读(3333)

之前写过两篇博文,介绍了最近邻插值,双线性插值和9点插值算法的基本理论,今天用Matlab来验证,并简单对比一下效果。

之前的博文地址为:

http://blog.chinaaet.com/justlxy/p/5100052604

http://blog.chinaaet.com/justlxy/p/5100052609

考虑到ChinaAET会压缩上传的图片,所以博文中的图片仅供参考,具体可以参考本文最后的附件!

一、首先比较压缩效果

原图为1000*1000:压缩为500*500

blob.png

1、最近邻插值:

blob.png

2、双线性插值:

blob.png

3、9点插值法(模式一):

blob.png

4、9点插值法(模式二):

blob.png

5、9点插值法(模式三):

blob.png

6、使用Win7自带画图工具:

blob.png

 

二、下面比较放大效果:

原图为300*300

blob.png

1、 最近邻插值法:

blob.png

2、 双向性插值法:

blob.png

3、 9点插值法(模式一):

blob.png

4、 9点插值法(模式二):

blob.png

5、 9点插值法(模式三):

blob.png

6、 Win7系统画图工具效果:

blob.png

附:Matlab源码:


function [ im_out ] = interpolation( im_in, dstWidth, dstHeight, mode )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
    [srcWidth,srcHeight,temp] = size(im_in);
 
    if(mode == 1)   %nearest_neighbor
        for dstX = 1:1:dstWidth
            for dstY = 1:1:dstHeight
                srcX = dstX * (srcWidth / dstWidth);
                srcY = dstY * (srcHeight / dstHeight);
                if(srcX < 1)
                 srcX = 1;
                end
                if(srcX > srcWidth)
                 srcX = srcWidth;
                end
                if(srcY < 1)
                 srcY = 1;
                end
                if(srcY > srcWidth)
                 srcY = srcWidth;
                end
                im_out(int16(dstX),int16(dstY),:) = im_in(int16(srcX),int16(srcY),:);
            end
        end
    end
    if(mode == 2)   %bilinearity
        for dstX = 1:1:dstWidth
            for dstY = 1:1:dstHeight
                srcX = dstX * (srcWidth / dstWidth);
                srcY = dstY * (srcHeight / dstHeight);
                if(srcX < 1)
                    srcX = 1;
                end
                if(srcX >= srcWidth)
                    srcX = srcWidth - 1;
                end
                if(srcY < 1)
                    srcY = 1;
                end
                if(srcY >= srcWidth)
                    srcY = srcWidth - 1;
                end
                srcX_i = fix(srcX);
                srcX_f = srcX - srcX_i;
                srcY_i = fix(srcY);
                srcY_f = srcY - srcY_i;
                im_out(int16(dstX),int16(dstY),:) = (1 - srcX_f) * (1 - srcY_f) * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                    (1 - srcX_f) * srcY_f * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                    srcX_f * (1 - srcY_f) * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                    srcX_f * srcY_f * im_in(int16(srcX_i + 1),int16(srcY_i + 1),:);
               
            end
        end
    end
    if(mode == 3) % 9-point interpolation mode 1
        for dstX = 1:1:dstWidth
            for dstY = 1:1:dstHeight
                srcX = dstX * (srcWidth / dstWidth);
                srcY = dstY * (srcHeight / dstHeight);
                if(srcX < 1)
                    srcX = 1;
                end
                if(srcX >= srcWidth)
                    srcX = srcWidth - 1;
                end
                if(srcY < 1)
                    srcY = 1;
                end
                if(srcY >= srcWidth)
                    srcY = srcWidth - 1;
                end
                srcX_i = fix(srcX);
                srcX_f = srcX - srcX_i;
                srcY_i = fix(srcY);
                srcY_f = srcY - srcY_i;
                if(srcX_f <= 0.5) 
                    if(srcY_f <= 0.5) %Area 1
                        im_out(int16(dstX),int16(dstY),:) = 0.5625 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0.1875 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0.1875 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0.0625 * im_in(int16(srcX_i),int16(srcY_i),:);
                    else %Area 3
                        im_out(int16(dstX),int16(dstY),:) = 0.1875 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0.0625 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0.5625 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0.1875 * im_in(int16(srcX_i),int16(srcY_i),:);
                    end
                else
                    if(srcY_f <= 0.5) %Area 2
                        im_out(int16(dstX),int16(dstY),:) = 0.1875 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0.5625 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0.0625 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0.1875 * im_in(int16(srcX_i),int16(srcY_i),:);
                    else %Area 4
                        im_out(int16(dstX),int16(dstY),:) = 0.0625 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0.1875 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0.1875 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0.5625 * im_in(int16(srcX_i),int16(srcY_i),:);
                    end
                end
            end
        end
    end
    if(mode == 4) % 9-point interpolation mode 2
        for dstX = 1:1:dstWidth
            for dstY = 1:1:dstHeight
                srcX = dstX * (srcWidth / dstWidth);
                srcY = dstY * (srcHeight / dstHeight);
                if(srcX < 1)
                    srcX = 1;
                end
                if(srcX >= srcWidth)
                    srcX = srcWidth - 1;
                end
                if(srcY < 1)
                    srcY = 1;
                end
                if(srcY >= srcWidth)
                    srcY = srcWidth - 1;
                end
                srcX_i = fix(srcX);
                srcX_f = srcX - srcX_i;
                srcY_i = fix(srcY);
                srcY_f = srcY - srcY_i;
                if(srcX_f <= 0.5) 
                    if(srcY_f <= 0.5) %Area 1
                        im_out(int16(dstX),int16(dstY),:) = 0.5 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0.25 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0.25 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0 * im_in(int16(srcX_i),int16(srcY_i),:);
                    else %Area 3
                        im_out(int16(dstX),int16(dstY),:) = 0.25 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0.5 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0.25 * im_in(int16(srcX_i),int16(srcY_i),:);
                    end
                else
                    if(srcY_f <= 0.5) %Area 2
                        im_out(int16(dstX),int16(dstY),:) = 0.25 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0.5 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0.25 * im_in(int16(srcX_i),int16(srcY_i),:);
                    else %Area 4
                        im_out(int16(dstX),int16(dstY),:) = 0 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                                            0.25 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                                            0.25 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                                            0.5 * im_in(int16(srcX_i),int16(srcY_i),:);
                    end
                end
            end
        end
    end
    if(mode == 5) % 9-point interpolation mode 3
        for dstX = 1:1:dstWidth
            for dstY = 1:1:dstHeight
                srcX = dstX * (srcWidth / dstWidth);
                srcY = dstY * (srcHeight / dstHeight);
                if(srcX < 1)
                    srcX = 1;
                end
                if(srcX >= srcWidth)
                    srcX = srcWidth - 1;
                end
                if(srcY < 1)
                    srcY = 1;
                end
                if(srcY >= srcWidth)
                    srcY = srcWidth - 1;
                end
                srcX_i = fix(srcX);
                srcX_f = srcX - srcX_i;
                srcY_i = fix(srcY);
                srcY_f = srcY - srcY_i;
                %Point Number Definition
                % 1   2   3
                % 4   5   6
                % 7   8   9
                if(srcX_f <= 0.5) 
                    if(srcY_f <= 0.5) %Area 1
                        if(srcX_f <= 0.25)
                            if(srcY_f <= 0.25)  %Point No.1
                                point = 1;
                            else                %Ponit No.4
                                point = 4;
                            end
                        else
                            if(srcY_f <= 0.25)  %Point No.2
                                point = 2;
                            else                %Ponit No.5
                                point = 5;
                            end
                        end
                    else %Area 3
                        if(srcX_f <= 0.25)
                            if(srcY_f <= 0.75)  %Point No.4
                                point = 4;
                            else                %Ponit No.7
                                point = 7;
                            end
                        else
                            if(srcY_f <= 0.75)  %Point No.5
                                point = 5;
                            else                %Ponit No.8
                                point = 8;
                            end
                        end
                    end
                else
                    if(srcY_f <= 0.5) %Area 2
                        if(srcX_f <= 0.75)
                            if(srcY_f <= 0.25)  %Point No.2
                                point = 2;
                            else                %Ponit No.5
                                point = 5;
                            end
                        else
                            if(srcY_f <= 0.25)  %Point No.3
                                point = 3;
                            else                %Ponit No.6
                                point = 6;
                            end
                        end
                    else %Area 4
                        if(srcX_f <= 0.75)
                            if(srcY_f <= 0.75)  %Point No.5
                                point = 5;
                            else                %Ponit No.8
                                point = 8;
                            end
                        else
                            if(srcY_f <= 0.75)  %Point No.6
                                point = 6;
                            else                %Ponit No.9
                                point = 9;
                            end
                        end
                    end
                end
               
                switch(point)
                    case 1
                        im_out(int16(dstX),int16(dstY),:) = im_in(int16(srcX_i),int16(srcY_i),:);
                    case 2
                        im_out(int16(dstX),int16(dstY),:) = 0.5 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                            0.5 * im_in(int16(srcX_i + 1),int16(srcY_i),:);
                    case 3
                        im_out(int16(dstX),int16(dstY),:) = im_in(int16(srcX_i + 1),int16(srcY_i),:);
                    case 4
                        im_out(int16(dstX),int16(dstY),:) = 0.5 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                            0.5 * im_in(int16(srcX_i),int16(srcY_i + 1),:);
                    case 5
                        im_out(int16(dstX),int16(dstY),:) = 0.25 * im_in(int16(srcX_i),int16(srcY_i),:) + ...
                                                            0.25 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                            0.25 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                            0.25 * im_in(int16(srcX_i),int16(srcY_i),:);
                    case 6
                        im_out(int16(dstX),int16(dstY),:) = 0.5 * im_in(int16(srcX_i + 1),int16(srcY_i),:) + ...
                                                            0.5 * im_in(int16(srcX_i + 1),int16(srcY_i + 1),:);
                    case 7
                        im_out(int16(dstX),int16(dstY),:) = im_in(int16(srcX_i),int16(srcY_i + 1),:);
                    case 8
                        im_out(int16(dstX),int16(dstY),:) = 0.5 * im_in(int16(srcX_i),int16(srcY_i + 1),:) + ...
                                                            0.5 * im_in(int16(srcX_i + 1),int16(srcY_i + 1),:);
                    case 9
                        im_out(int16(dstX),int16(dstY),:) = im_in(int16(srcX_i + 1),int16(srcY_i + 1),:);
                    otherwise
                        im_out(int16(dstX),int16(dstY),:) = [0,0,0];
                end
            end
        end
    end
    imshow(im_out);
end

附件:

插值算法效果比较.docx