简体   繁体   English

如何从图像中提取垂直和水平线

[英]How to extract vertical and horizontal lines from the image

Task:任务:

I have a set of images where every image looks like this:我有一组图像,其中每个图像看起来像这样:

在此处输入图像描述

I'd like to extract all horizontal and all vertical lines from this image.我想从此图像中提取所有水平线和所有垂直线。

Desired results:期望的结果:

在此处输入图像描述

在此处输入图像描述

Current approach:目前的做法:

import cv2


image = cv2.imread('img.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=1)

Here's the result:结果如下:

在此处输入图像描述

Problem:问题:

It's clear that the current kernel is too narrow to bypass this thick vertical line on the left.很明显,当前的 kernel 太窄,无法绕过左边这条粗竖线。 This line is 41-pixel thick, so the kernel (42, 1) works fine, but I lose true horizontal lines that a shorter than 41 pixel:这条线是 41 像素厚,所以 kernel (42, 1) 工作正常,但我失去了小于 41 像素的真正水平线:

在此处输入图像描述

Are there any flawless techniques for solving this problem?有没有完美的技术来解决这个问题?

The idea is to bring all those lines to the same "Size" by Skeletonization before applying a morphological filter & use a smaller filter Size这个想法是在应用形态过滤器之前通过骨架化将所有这些线带至相同的“大小”并使用较小的过滤器大小

image = cv2.imread('Your_ImagePath' , cv2.IMREAD_COLOR)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

thresh = skeletonize(thresh)
cv2.imshow("Skelton",thresh)
cv2.waitKey()

horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 1))
horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, 
iterations=1)

cv2.imshow("Horizontal Lines" , horizontal)
v2.waitKey()

OUTPUT IMAGE: OUTPUT 图片:

在此处输入图像描述

NOTE: Skeletonization code from skeletonize.py注意:skeletonize.py 中的骨架化代码

I would be thinking about doing a "Morphological Opening" with a tall, thin structuring element (ie a vertical line 15 pixels tall and 1 pixel wide) to find the vertical bars and with a horizontal line 15 pixels long and 1 pixel high to find the horizontal bars.我会考虑用一个又高又的结构元素(即一条 15 像素高和 1 像素宽的垂直线)来寻找垂直条,并用一条 15 像素长和 1 像素高的水平线来寻找水平条。

You can do it just the same with OpenCV .你可以用OpenCV做同样的事情。 but I am just doing it here with ImageMagick in the Terminal because I am quicker at that: Here's a command for the vertical work - try varying the 15 to get different results:但我只是在终端中使用ImageMagick执行此操作,因为我更快:这是垂直工作的命令 - 尝试改变 15 以获得不同的结果:

magick shapes.png -morphology open rectangle:1x15 result.png

Here's an animation of how the result changes as you vary the 15 (the length of the line):这是 animation 的结果如何随着您改变 15(线的长度)而变化:

在此处输入图像描述

And here is how it looks when you make the structuring element a horizontal line:这是将结构元素设为水平线时的外观:

magick shapes.png -morphology open rectangle:15x1 result.png

在此处输入图像描述

If you are new to morphology, there is an excellent description by Anthony Thyssen here .如果您不熟悉形态学,这里有 Anthony Thyssen 的精彩描述。 Note that it may be explained in terms of ImageMagick but the principles are equally applicable in Python OpenCV - see here .请注意,它可以用ImageMagick来解释,但这些原理同样适用于 Python OpenCV - 请参见此处

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

相关问题 从图像中提取水平线和垂直线 - Extract horizontal and vertical lines from an image 如何从图像中删除水平和垂直线条 - How to remove horizontal and vertical lines from an image Python如何使用OpenCV的HoughLines检测图像中的垂直和水平线? - Python How to detect vertical and horizontal lines in an image with HoughLines with OpenCV? 如何在不降低 python 中的图像质量的情况下去除水平和垂直线条 - How to remove horizontal and vertical lines without degrading the image quality in python 将图像上的线条大致分类为垂直或水平 - Approximately classify lines on image as vertical or horizontal 如何在QTableWidget中隐藏垂直/​​水平线 - How to hide vertical/horizontal lines in QTableWidget 使用 cv2 Python 从图像中删除不均匀的垂直和水平线 - Remove uneven vertical and horizontal lines from an image using cv2 Python 如何将“垂直”图像转换为“水平”图像? - How to transform a "vertical" image into a "horizontal" image? 如何使用 Tesseract OCR 从具有水平线的图像中提取文本? - How to extract at text from an image with horizontal line using Tesseract OCR? 如何使用python从网格的坐标中沿水平和垂直方向提取最大值? - How can I extract the maximum values along the horizontal and vertical from the coordinates of the grid using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM