简体   繁体   English

如何删除水平和垂直线条

[英]How to remove horizontal and vertical lines

I need to remove horizontal and vertical lines in a binary image. 我需要删除二进制图像中的水平和垂直线。 Is there any method for filtering these lines? 有没有过滤这些线的方法? bwareaopen() is not good method to remove these lines and also Dilation and Erosion are not good for these cases. bwareaopen()不是去除这些线的好方法,而且膨胀和侵蚀对这些情况也不好。 Does any one know a solution? 有人知道解决方案吗?

Example image: 示例图片:

例

EDIT:(added more example images: 编辑:(添加更多示例图像:

http://s1.upload7.ir/downloads/pPqTDnmsmjHUGTEpbwnksf3uUkzncDwr/example%202.png http://s1.upload7.ir/downloads/pPqTDnmsmjHUGTEpbwnksf3uUkzncDwr/example%202.png

source file of images: 图像源文件:

https://www.dropbox.com/sh/tamcdqk244ktoyp/AAAuxkmYgBkB8erNS9SajkGVa?dl=0 https://www.dropbox.com/sh/tamcdqk244ktoyp/AAAuxkmYgBkB8erNS9SajkGVa?dl=0

www.directexe.com/9cg/pics.rar www.directexe.com/9cg/pics.rar

Use regionprops and remove regions with high eccentricity (meaning the region is long and thin) and orientation near 0 or near 90 degrees (regions which are vertical or horizontal). 使用regionprops并移除具有高偏心率的区域(意味着区域长而细)并且在0或接近90度附近定向(垂直或水平的区域)。

Code: 码:

img = im2double(rgb2gray(imread('removelines.jpg')));

mask = ~im2bw(img);

rp = regionprops(mask, 'PixelIdxList', 'Eccentricity', 'Orientation');

% Get high eccentricity and orientations at 90 and 0 degrees
rp = rp([rp.Eccentricity] > 0.95 & (abs([rp.Orientation]) < 2 | abs([rp.Orientation]) > 88));

mask(vertcat(rp.PixelIdxList)) = false;

imshow(mask);

Output: 输出:

在此输入图像描述

If all of your images are the same where the horizontal and vertical lines are touching the border, a simple call to imclearborder will do the trick. 如果你的所有图像都是水平线和垂直线接触边界的图像,那么简单地调用imclearborder就可以了。 imclearborder removes any object pixels that are touching the borders of the image. imclearborder删除任何触摸图像边框的对象像素。 You'll need to invert the image so that the characters are white and the background is dark, then reinvert back, but I'm assuming that isn't an issue. 您需要反转图像,使字符为白色,背景为暗,然后重新转回,但我认为这不是问题。 However, to be sure that none of the actual characters get removed because they may also be touching the border, it may be prudent to artificially pad the top border of the image with a single pixel thickness, clear the border, then recrop. 但是,为了确保没有任何实际字符被删除,因为它们也可能触摸边框,因此用单个像素厚度人工填充图像的顶部边框,清除边框然后重新编写可能是谨慎的。

im = imread('http://i.stack.imgur.com/L1hUa.jpg'); %// Read image directly from StackOverflow

im = ~im2bw(im); %// Convert to black and white and invert
im_pad = zeros(size(im,1)+1, size(im,2)) == 1; %// Pad the image too with a single pixel border
im_pad(2:end,:) = im;

out = ~imclearborder(im_pad); %// Clear border pixels then reinvert
out = out(2:end,:); %// Crop out padded pixels

imshow(out); %// Show image

We get this: 我们得到这个:

在此输入图像描述

You can firstly find the horizontal and vertical lines. 您可以先找到水平和垂直线。 Since, the edge map will also be binary so you can perform a logical subtraction operation in between the images. 因为,边缘图也将是二进制的,因此您可以在图像之间执行逻辑减法运算。 To find vertical lines, you can use (in MATLAB) 要查找垂直线,可以使用(在MATLAB中)

BW = edge(I,'sobel','vertical');

For horizontal lines, you can use 对于水平线,您可以使用

% Generate horizontal edge emphasis kernel
h = fspecial('sobel');

% invert kernel to detect vertical edges
h = h';

J = imfilter(I,h);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM