解决这个问题的一个好方法是解决linear heat equation。你要做的是固定好区域像素的“温度”(强度),让热量流入坏像素。一个可以接受但有点慢的方法是重复平均图像,然后使用newImage(~badPixels) = myData(~badPixels); 将好的像素设置回其原始值。
我执行以下步骤:
- 找到图像为零的坏像素,然后扩大以确保我们得到所有内容
- 应用大模糊让我们更快地开始
- 平均图像,然后将好的像素设置回原来的位置
- 重复步骤 3
- 显示
您可以重复平均直到图像停止变化,并且您可以使用较小的平均内核以获得更高的精度——但这会产生良好的结果:
代码如下:
numIterations = 30;
avgPrecisionSize = 16; % smaller is better, but takes longer
% Read in the image grayscale:
originalImage = double(rgb2gray(imread('c:\temp\testimage.jpg')));
% get the bad pixels where = 0 and dilate to make sure they get everything:
badPixels = (originalImage == 0);
badPixels = imdilate(badPixels, ones(12));
%# Create a big gaussian and an averaging kernel to use:
G = fspecial('gaussian',[1 1]*100,50);
H = fspecial('average', [1,1]*avgPrecisionSize);
%# User a big filter to get started:
newImage = imfilter(originalImage,G,'same');
newImage(~badPixels) = originalImage(~badPixels);
% Now average to
for count = 1:numIterations
newImage = imfilter(newImage, H, 'same');
newImage(~badPixels) = originalImage(~badPixels);
end
%% Plot the results
figure(123);
clf;
% Display the mask:
subplot(1,2,1);
imagesc(badPixels);
axis image
title('Region Of the Bad Pixels');
% Display the result:
subplot(1,2,2);
imagesc(newImage);
axis image
set(gca,'clim', [0 255])
title('Infilled Image');
colormap gray
但您可以使用图像处理工具箱中的roifill 获得类似的解决方案,如下所示:
newImage2 = roifill(originalImage, badPixels);
figure(44);
clf;
imagesc(newImage2);
colormap gray
请注意,我正在使用之前定义的相同 badPixels。