仅水平 - 垂直线

时间:2017-02-12 07:54:13

标签: matlab image-processing image-morphology

我是matlab的新手。我有一块图像,如下图所示: enter image description here

白色显示像素,其值等于1,而黑色显示像素,其值等于0

我想得到vertical only lines。这意味着应该删除水平线,如下所示:

enter image description here

我也希望得到horizontal only lines。这意味着应删除垂直线,如下图所示:

enter image description here

我如何在Matlab中执行此操作?我更喜欢形态学操作。

2 个答案:

答案 0 :(得分:1)

假设您的图片位于BW以下:

% detecting all connected regions:
B = bwboundaries(BW,4);

这导致一个包含所有"补丁的单元阵列B"通过连接值为1的相邻单元格来建立的,这些单元格从4个边中的一个连接,即不在对角线中。

B = 
    [11x2 double]
    [ 2x2 double]
    [ 3x2 double]
    [ 3x2 double]
    [ 2x2 double]
    [ 3x2 double]
    [ 2x2 double]
    [ 2x2 double]
    [ 3x2 double]
    [ 3x2 double]
    [ 2x2 double]
    [11x2 double]

例如:

>> B{6}
ans =
     3     7
     3     8
     3     7

每行是一个单元格坐标。第一列是它的'排,第二个'列,第一个和最后一个单元格始终相同。

现在我们需要遍历B中的单元格,找到它们中的哪些是水平线或垂直线,并将它们保存到新的矩阵中。

% matrices for horizontal and vertical lines:
BWh = zeros(size(BW)); % horizontal lines
BWv = zeros(size(BW)); % vertical lines
for k = 1:numel(B)
    % if the coordinates changes ONLY vertically:
    % a vertical line is where all the coulmn indecies are the same
    % and there are different row indices
    if all(B{k}(1,2)==B{k}(:,2)) && B{k}(1,1)~=B{k}(2,1) 
        BWv(sub2ind(size(BW),B{k}(:,1),B{k}(:,2))) = 1;
    end
    % if the coordinates changes ONLY horizontaly:
    % a vertical line is where all the row indecies are the same
    % and there are different column indices
    if all(B{k}(1,1)==B{k}(:,1)) && B{k}(1,2)~=B{k}(2,2)
        BWh(sub2ind(size(BW),B{k}(:,1),B{k}(:,2))) = 1;
    end
end
subplot 131
imagesc(BWh)
title('Horizontal lines')
subplot 132
imagesc(BWv)
title('Vertical lines')

"对角线边缘"在我们排除这些线之后剩下的是什么,所以我们可以只查找到目前为止我们找不到的内容:

subplot 133
imagesc(BW & ~BWv & ~BWh)
title('Diagonal edges')
colormap 'gray'

BWvh

此方法将忽略不是单格粗线的任何内容,因此,例如,下图中间的正方形将仅显示在对角线边缘图案中:

box demo

答案 1 :(得分:1)

有趣的问题,因为有很多方法可以做到这一点。 实质上,您需要取出特定维度的连续像素。 我认为解决此问题的一种方法是使用[1 1][1 1]'向量进行卷积,然后取出所有获得值2的元素。

bw(conv2(bw,[1 1],'same')==2)=0;

这仍然会留下您可以使用

轻松取出的单个像素
bw = bwareaopen(bw,2) ;

这只是主要的想法,你可能需要在边缘周围更加小心,或者用零填充以避免conv2可以产生的边缘伪影)......

另一个想法是,使用Hough transform来检测线条,只保留那些具有θ= 0或90度的线条......

相关问题