2012-07-05 195 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