过滤二进制图像以仅保留线条

问题描述

我有以下图像 initial image,在将其转换为二进制 binary image 后,我希望以某种方式仅保留显示的 3 行并删除其他对象。我已经在 Matlab 中使用区域道具等尝试了几件事,但我没有设法过滤掉这些区域。有什么想法吗?

解决方法

霍夫变换可能是第一种引用方式,但如果您对结果不满意,您可以尝试使用形态学开放,以线条作为结构元素。由于您的线条指向几个不同的方向,您应该尝试任何可能的方向,然后合并产生的图像。如果您对方向有先验知识,则可以利用它并按方向过滤。

示例代码块可能如下所示:

my_img = imread('img.png'); % get your image here
opened=[];
se_length = 25; % chose in pixels,according to your need
for derece = 50:2:70 % use 0:3:180 for all directions
    se_line = strel('line',se_length,derece);
    opened = cat(3,opened,imopen(my_img,se_line));
end

max_opened = max(opened,[],3); % merge images based on maximum (or your own method)

形态学开口可用于过滤多种形状。只需根据您的需要选择结构元素。上述脚本为您的特定图像示例提供以下结果:

opened and maximized image