二、photoshop图像滤镜——素描算法
素描的算法,网络上有多种,但是效果并不是特别理想。熟悉photoshop的朋友都知道,把彩色图片打造成素描的效果仅仅需要几步操作:1、去色;2、复制去色图层,并且反色;3、对反色图像进行高斯模糊;4、模糊后的图像叠加模式选择颜色减淡效果。
图像的去色较为简单,就不多解释。假设原图像为X,处理后的图像为Y ,也就是说,对于坐标为(i,j)点,反色为Y(i,j)=255-X(i,j)。高斯模糊相当与一个低通滤波器,朋友们可以找找和高斯模糊有关的资料。颜色减淡的算法是这样的:C =MIN( A +(A×B)/(255-B),255),其中C为混合结果,A为源像素点,B为目标像素点。
matlab代码如下:
I=imread(\'a1.jpg\',\'jpg\'); imshow(I); figure(); info_size=size(I); height=info_size(1); width=info_size(2); N=zeros(height,width); g=zeros(height,width); imggray=rgb2gray(I); out=zeros(height,width); spec=zeros(height,width,3); for i=1:height for j=1:width N(i,j)=255-imggray(i,j); end end for i=2:height-1 for j=2:width-1 sum=0; sum=1*double(N(i-1,j-1))+2*double(N(i-1,j))+1*double(N(i-1,j+1)); sum=sum+2*double(N(i,j-1))+4*double(N(i,j))+2*double(N(i,j+1)); sum=sum+1*double(N(i+1,j-1))+2*double(N(i+1,j))+1*double(N(i+1,j+1)); sum=sum/16; g(i,j)=sum; end end for i=1:height for j=1:width b=double(g(i,j)); a=double(imggray(i,j)); temp=a+a*b/(256-b); out(i,j)=uint8(min(temp,255)); end end imshow(out/255);
处理后图像如下:
C代码如下:
void rgb2gray(unsigned char r[1000][1000],unsigned char g[1000][1000],unsigned char b[1000][1000],unsigned char gray[1000][1000],int height,int width) { int i,j; for(i=0;i<height;i++) for(j=0;j<width;j++) { gray[i][j]=r[i][j]/3+g[i][j]/3+b[i][j]/3; } } void sketch(unsigned char output_r[1000][1000],unsigned char output_g[1000][1000],unsigned char output_b[1000][1000],unsigned char gray[1000][1000],int height,int width) { int i,j,sum; int a,b,temp; float ex; unsigned char inverse[1000][1000],guass_blur[1000][1000]; for(i=0;i<height;i++) for(j=0;j<width;j++) { inverse[i][j]=255-gray[i][j]; } for(i=1;i<height-1;i++) for(j=1;j<width-1;j++) { sum= inverse[i-1][j-1]+2*inverse[i-1][j ]+ inverse[i-1][j+1]+ 2*inverse[i ][j-1]+4*inverse[i ][j ]+2*inverse[i ][j+1]+ inverse[i+1][j-1]+2*inverse[i+1][j ]+ inverse[i+1][j+1]; sum=sum/16; guass_blur[i][j]=sum; } for(i=0;i<height;i++) for(j=0;j<width;j++) { if((i==0)||(i==height-1)||(j==0)||(j==width-1)) { guass_blur[i][j]=0; } } for(i=0;i<height;i++) for(j=0;j<width;j++) { b=guass_blur[i][j]; a=gray[i][j]; temp=a+a*b/(256-b); ex=temp*temp*1.0/255/255; temp=temp*ex; a=min(temp,255); output_r[i][j]=a; output_g[i][j]=a; output_b[i][j]=a; } }