【问题标题】:remove the holes in an image by average values of surrounding pixels通过周围像素的平均值去除图像中的孔
【发布时间】:2012-10-08 19:03:41
【问题描述】:

谁能帮我用取自相邻非零像素的值来填充这些黑洞。 谢谢

【问题讨论】:

标签: matlab image-processing computer-vision interpolation


【解决方案1】:

解决这个问题的一个好方法是解决linear heat equation。你要做的是固定好区域像素的“温度”(强度),让热量流入坏像素。一个可以接受但有点慢的方法是重复平均图像,然后使用newImage(~badPixels) = myData(~badPixels); 将好的像素设置回其原始值。

我执行以下步骤:

  1. 找到图像为零的坏像素,然后扩大以确保我们得到所有内容
  2. 应用大模糊让我们更快地开始
  3. 平均图像,然后将好的像素设置回原来的位置
  4. 重复步骤 3
  5. 显示

您可以重复平均直到图像停止变化,并且您可以使用较小的平均内核以获得更高的精度——但这会产生良好的结果:

代码如下:

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。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      要填充一个黑色区域,请执行以下操作:

      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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-12
        • 1970-01-01
        • 2015-06-26
        • 2018-08-16
        • 1970-01-01
        • 1970-01-01
        • 2015-05-15
        • 1970-01-01
        相关资源
        最近更新 更多