[英]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.