close all;
clc;
clear;
%--------------------------读入图像------------------------------------------
imgoriginal=imread('c1003.bmp'); %待处理的图片转化为矩阵,黑白2维矩阵,彩色三维,例a=imread(xxx.jpg),R=a(:,:,1); G=a(:,:,2); B=a(:,:,3);
figure(1); %figure创建图像,imshow显示图片,一起为避免覆盖
imshow(imgoriginal); %显示待处理的图片
[width,height] = size(imgoriginal); %size(),获取图片矩阵的行数为width、列数为height
img= double(imgoriginal); %转换成double精度
%--------------------------获取掩码图像(mask遮挡物)---------------------------------------------
mask = zeros(width,height/3,3);
for j = 1:height/3
for i = 1:width
if ((imgoriginal(i,j,1) >220)&&(imgoriginal(i,j,2) >220)&&(imgoriginal(i,j,3) >220))
mask(i,j,1) = 255; %灰度255=白 ,0=黑
mask(i,j,2) = 255;
mask(i,j,3) = 255;
else
mask(i,j,1) = 0;
mask(i,j,2) = 0;
mask(i,j,3) = 0;
end
end
end
figure(2);
imshow(mask); %显示遮挡物
%--------------------------更新原图修复区域内每点值------------------------------------------
a=zeros(width,height); %生成全0矩阵
I=cat(3,a,2*a,3*a);
J=cat(3,a,2*a,3*a);
n = 1;
itertimes=1000; %迭代时间 iteration迭代
tic; %tic与toc,记录命令执行的时间
while n <= itertimes
for i = 2:width-1
for j = 2:height/3-1
if (mask(i,j+1,1) == 255)||(mask(i,j-1,1) == 255)||(mask(i+1,j,1) == 255)||(mask(i-1,j,1) == 255)
for k=1:3
grid_w(k) = (img(i,j,k)-img(i-1,j,k))^2+(1.0/16)*(img(i-1,j+1,k)+img(i,j+1,k)-img(i-1,j-1,k)-img(i,j-1,k))^2;
grid_e(k) = (img(i,j,k)-img(i+1,j,k))^2+(1.0/16)*(img(i,j+1,k)+img(i+1,j+1,k)-img(i,j-1,k)-img(i+1,j-1,k))^2;
grid_s(k) = (img(i,j,k)-img(i,j-1,k))^2+(1.0/16)*(img(i+1,j,k)+img(i+1,j-1,k)-img(i-1,j,k)-img(i-1,j-1,k))^2;
grid_n(k) = (img(i,j,k)-img(i,j+1,k))^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(1+grid_n(k))+1;
img(i,j,k) =(w1(k)*img(i-1,j,k)+w2(k)*img(i+1,j,k)+w3(k)*img(i,j-1,k)+w4(k)*img(i,j+1,k))/(w1(k)+w2(k)+w3(k)+w4(k));
end
end
end
end
n = n+1;
end
%--------------------------输出修复后图像------------------------------------
img = uint8(floor(img));
toc;
figure(3);
imshow(img,[]);
imwrite(img,'CDD修复后.bmp'); %将图片保存到当前文件夹