删除连接到右边框和底边框的对象

时间:2016-05-19 11:29:20

标签: matlab image-processing

我正在尝试删除连接到右边框和底边框的白色对象。我尝试imclearborder,但不知道如何排除连接到顶部和左边框的对象。

My Image

1 个答案:

答案 0 :(得分:0)

您可以使用imclearborder,但您应首先填充图像的左边和上边缘,以便它们不会触及边框(因此,不会被清除)。

<强>代码:

I = imread('gQKc8.png');         %// your image

J = zeros(size(I)+1,'uint8');    %// initialize a padded matrix
J = J(:,:,1:3);                  %// (we didn't need to pad the third-dimension)
J(2:end,2:end,:) = I;            %// assign I to the lower-right of J

C = imclearborder(J,4);          %// now run imclearborder, using connectedness option 4
C = C(2:end,2:end,:);            %// remove the padding

imclearborder中的连接选项4仅告诉函数仅检查四个二维相邻像素。有关详细信息,请参阅imclearborder文档。

绘制结果:

%// for comparison:
B=imclearborder(I,4);             %// imclearborder on the original
subplot(221); imshow(I);
subplot(222); imshow(B);
subplot(223); imshow(J);
subplot(224); imshow(C);

从左到右,从上到下:

  1. 原始图片
  2. 直接在原始
  3. 上使用imclearborder
  4. 填充图片
  5. 在填充图片上使用imclearborder所需的结果
  6. result compared to original

    注意:正如@beaker所评论的那样,因为右侧的两个小白色斑点连接到边框,它们不会被清除。如果你还想删除它们,你需要更准确地定义什么决定是否应该清除一个blob。