相关的程序:
%{
时间:2014年9月24日19:51:39
整理:天空之恋
参考:http://blog.csdn.net/yangtrees/article/details/9210153
原文使用opencv编写的,这个地方稍微修改了一下改成MATLAB实现的
经过实际测试效果还是可以的。
%}
clc;
clear all;
src=imread('test.png');
subplot(121)
imshow(src),title('原始图像');
srcgray=rgb2gray(src);%首先进行灰度变换
[height,width] = size(srcgray);%获得图像的高度和宽度
centery=width/2;
centerx=height/2;
maxv=centerx*centerx+centery*centery;
msize=0.5;%改变这个值,可以改变羽化效果,羽化明显或者不明显
minv=(maxv*(1-msize));
diff=maxv-minv;
%ratio=width>height? height/width : width/height;
if width>height
ratio=(height/width);
else
ratio=(width/height);
end
height=height-1;
width=width-1;
for x=1:height
for y=1:width
r=src(x,y,1);
g=src(x,y,2);
b=src(x,y,3);
dy=centery-y;
dx=centerx-x;
dstsq=(dx*dx+dy*dy);
v=((dstsq/diff)*128); %原文这个地方是255,我们实际测试的时候
%发现这个地方应该改成128,否则效果会过于明显
r=r+v;
g=g+v;
b=b+v;
if r>255
r=255;
elseif r<0
r=0;
end
if g>255
g=255;
elseif g<0
g=0;
end
if b>255
b=255;
elseif b<0
b=0;
end
dst(x,y,1)=uint8(r);
dst(x,y,2)=uint8(g);
dst(x,y,3)=uint8(b);
end
end
subplot(122);
imshow(dst),title('羽化结果');