仅水平垂直线 [英] horizontal-vertical only lines

查看:102
本文介绍了仅水平垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Matlab的新手.我有一个图像块,如下图所示:

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

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

我也想获得horizontal only lines.这意味着应删除垂直线,如下所示:

如何在Matlab中执行此操作?为此,我更喜欢进行形态学操作.

解决方案

假设您的图片位于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'

此方法将忽略任何不是一个单元格粗线的内容,例如,下图中中间的正方形将仅以对角线模式显示:

I'm new in matlab. I have a block of image as illustrated below:

Whites show pixel that their values are equal to 1 and Blacks show pixel that their values are equal to 0,

I want to get vertical only lines. This means horizontal lines should be removed as illustrated below:

Also I want to get horizontal only lines. This means vertical lines should be removed as illustrated below:

How can I do it in Matlab? I prefer morphological operations for this.

解决方案

Assuming your image is BW below:

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

This results in a cell array B that contains all the "patches" that are made by connecting neighboring cells with value 1 that are connected from one of 4 sides, i.e. not in diagonal.

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]

For example:

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

Each row is one cell coordinates. The first column is its' row, the second its' column, and the first and last cells are always the same.

Now we need to loop through the cells in B, and find which of them are lines, either horizontal or vertical, and save them to new matrices.

% 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')

The "Diagonal edges" are what left after we exclude the lines, so we can just look for what we didn't find so far:

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

This method will ignore anything that is not a one-cell thick line, so for instance, the square in the middle in the image below will be shown only in the Diagonal edges pattern:

这篇关于仅水平垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆