function f = my_clean( g,n )
% n 为操作次数,默认为1,g为原图
g2 = g;
if( nargin == 1 )
n = 1;
end
%得到原图像的大小
[ M,N,~ ] = size( g );
while( n ~=0 )
n = n - 1;
%进行交互选择处理区域
mask = roipoly( g2 );
x1 = immultiply( mask,g2( :,:,1 ) );
x2 = immultiply( mask,g2( :,:,2 ) );
x3 = immultiply( mask,g2( :,:,3 ) );
x = cat( 3,x1,x2,x3 );
%预分配内存,f1,f2,f3存储三个通道的运算结果
f1 = zeros( M,N );
f2 = zeros( M,N );
f3 = zeros( M,N );
%跳出双重循环设置flag
flag = 0;
%找到第一个像素值不为0的点,得到该点像素值,作为采样后填充的像素
for i = 1:M
for j = 1:N
if( x1( i,j ) ~= 0 )
r = x( i,j,: );
flag = 1;
break
end
if( flag == 1 )
break
end
end
end
%随机产生填充图像
y = zeros(3,3,3);
y( :,:,1 ) = randi([r(1)-5,r(1)+5],[3,3]);
y( :,:,2 ) = randi([r(2)-5,r(2)+5],[3,3]);
y( :,:,3 ) = randi([r(3)-5,r(3)+5],[3 3]);
%类型转换
y = double(y);
%对于三个通道分别进行处理,用采样得到的像素点取代原来的像素点
for i = 2:3:M-1
for j = 2:3:N-1
f1( i-1:i+1,j-1:j+1 ) = mask( i-1:i+1,j-1:j+1 ).* y( :,:,1 );
f2( i-1:i+1,j-1:j+1 ) = mask( i-1:i+1,j-1:j+1 ).* y( :,:,2 );
f3( i-1:i+1,j-1:j+1 ) = mask( i-1:i+1,j-1:j+1 ).* y( :,:,3 );
end
end
%将三个通道连接在一起
f = cat( 3,f1,f2,f3 );
%类型转换
f = uint8( f );
%得到处理后图像
a = g2 - x;
f = f + a;
g2 = f;
end
%图像滤波处理
f = double(f);
b = double( imguidedfilter( uint8( f ) ) ) - f + 128 ;
t = imfilter( b, fspecial( 'gaussian',[5 5]) );
f = ( f*50 + ( f+2*t-256 )*50 )/100;
f = uint8(f);
%显示图像
subplot( 1,2,1 ),imshow( g ),title('原图');
subplot( 1,2,2 ),imshow( f ),title('填充处理后图像');
end