2012-07-05 77 views
9

Gray Scale Image with Holes由平均值除去周围像素

的图像中的孔可以任何一个请帮我从邻近的非零像素值取填补这些黑洞。 感谢

+0

在DSP上重复:http://dsp.stackexchange.com/q/2803/590 – Chris

+0

解决此问题:http://cs.stackexchange.com/questions/23794/interpolation-optimization-problem会做.. – Royi

回答

5

有一个关于Matlab文件交换的文件, - inpaint_nans正是你想要的。作者解释了为什么以及在哪些情况下比Delaunay三角测量更好。

+0

谢谢你,这给人满意的结果 –

2

为了填充一个黑色区域中,执行以下操作:

1)确定含有所述黑色区域的子区域,越小越好。最好的情况就是黑洞的边界点。

2)通过在子区域内创建非黑色点的Delaunay三角剖分:

tri = DelaunayTri(x,y); %# x, y (column vectors) are coordinates of the non-black points. 

3)确定的黑点,其中德洛奈三角形由:

[t, bc] = pointLocation(tri, [x_b, y_b]); %# x_b, y_b (column vectors) are coordinates of the black points 
tri = tri(t,:); 

4)插值:

v_b = sum(v(tri).*bc,2); %# v contains the pixel values at the non-black points, and v_b are the interpolated values at the black points. 
+0

似乎它会工作让我试试。谢谢你的努力 –

8

这样做的一个好方法是解决linear heat equation。你要做的是修正好区域像素的“温度”(强度),让热量流入坏像素。一个可以通过,但有点慢,是这样做的是重复平均图像,然后设置好的像素回到其原始值与newImage(~badPixels) = myData(~badPixels);

我做以下步骤:

  1. 找到坏像素在图像是零,然后扩张,以确保我们得到的一切
  2. 应用一个大的模糊让我们开始更快
  3. 平均的图像,然后设定好像素回其原始
  4. 重复步骤3
  5. 显示

你可以重复平均,直至图像停止变化,你可以使用更高的精度更小的平均内核---但是这给了良好的效果:

enter image description here

的代码如下:

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。

+0

http://cs.stackexchange.com/questions/23794/interpolation-optimization-problem – Royi