clc;
    clear all;
    close all;

    addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm');

    I=imread('4.jpg');
    I=double(I);
    Image=I/255;

    [height, width, depth]=size(Image);

     a = 200;
     b = 200;
     centreX = 0.5;
     centreY = 0.5;
     refractionIndex = 1.25;  % >1

     icentreX=width * centreX;
     icentreY=height * centreY;

     if (a==0)
         a=width/2;
     end

     if (b==0)
         b=height/2;
     end

     a2=a*a;
     b2=b*b;

     Image_new=Image;

     for ii=1:height
        for jj=1:width

            dx=jj-icentreX;
            dy=ii-icentreY;

            x2=dx*dx;
            y2=dy*dy;

            if (y2 >= (b2 - (b2*x2)/a2)) 
                x=jj;
                y=ii;
            else
                rRefraction = 1.0/ refractionIndex;
                z=sqrt((1.0 - x2/a2 - y2/b2) * (a*b));
                z2=z*z;
                xAngle = acos(dx /sqrt(x2+z2));
                angle_1=pi/2-xAngle;
                angle_2=asin(sin(angle_1));
                angle_2=pi/2-xAngle - angle_2;
                x=jj - tan(angle_2)*z;

                yAngle = acos(dy /sqrt(y2+z2));
                angle_1=pi/2-yAngle;
                angle_2=asin(sin(angle_1)*rRefraction);
                angle_2=pi/2-yAngle - angle_2;
                y=ii - tan(angle_2)*z;

            end

    % %         if (x<=1)     x=1;  end
    % %         if (x>=width)   x=width-1; end;
    % %         if (y>=height)  y=height-1; end;
    % %         if (y<1)  y=1;     end;
    % %         

            if (x<=1)     continue;  end
            if (x>=width)   continue; end;
            if (y>=height)  continue; end;
            if (y<1)  continue;     end;

            x1=floor(x);
            y1=floor(y);
            p=x-x1;
            q=y-y1;

            Image_new(ii,jj,:)=(1-p)*(1-q)*Image(y1,x1,:)+p*(1-q)*Image(y1,x1+1,:)...
                +q*(1-p)*Image(y1+1,x1,:)+p*q*Image(y1+1,x1+1,:); 

        end
     end

     imshow(Image_new);
     imwrite(Image_new, 'out.jpg');

参考来源:http://www.jhlabs.com/index.html

原图:
这里写图片描述

效果图:

这里写图片描述

posted on 2015-10-10 10:15  未雨愁眸  阅读(832)  评论(0编辑  收藏  举报