简体   繁体   English

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

[英]Extract horizontal and vertical lines from an image

I want to extract horizontal and vertical lines in 2 masks for the attached image.我想为附加图像提取 2 个蒙版中的水平线和垂直线。

I have tried morphological operations to do this,我尝试过形态学操作来做到这一点,

horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))

But the problem, It detects the line as a rectangle and then draws it in a form of 2 lines representing the 2 sides of the rectangle.但问题是,它将线检测为矩形,然后以2条线代表矩形2条边的形式绘制它。

Any ideas to fix that?有什么想法可以解决这个问题吗?

The image:图片:
图片

The horizontal result:横向结果:

H

The vertical result:垂直结果:

v


EDIT: That's another image I have:编辑:那是我的另一张图片:

图片

What did you do with your structuring elements?你对结构元素做了什么? Where's the rest of your code?你的其余代码在哪里?

I'd suggest to use morphological opening using cv2.morphologyEx , like so:我建议使用cv2.morphologyEx使用形态学开放,如下所示:

import cv2
import numpy as np
from skimage import io              # Only needed for web reading images

# Web read image; use cv2.imread(...) for local images
img = cv2.cvtColor(io.imread('https://i.stack.imgur.com/jnCvG.jpg'), cv2.COLOR_RGB2GRAY)

# Get rid of JPG artifacts
img = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY)[1]

# Create structuring elements
horizontal_size = 11
vertical_size = 11
horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontal_size, 1))
verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, vertical_size))

# Morphological opening
mask1 = cv2.morphologyEx(img, cv2.MORPH_OPEN, horizontalStructure)
mask2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, verticalStructure)

# Outputs
cv2.imshow('img', img)
cv2.imshow('mask1', mask1)
cv2.imshow('mask2', mask2)
cv2.waitKey(0)
cv2.destroyAllWindows()

I get the following two masks, which look quite good to me:我得到了以下两个面具,对我来说看起来很不错:

面具 1

面具 2

Hope that helps!希望有帮助!

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.1
NumPy:       1.18.1
OpenCV:      4.2.0
----------------------------------------

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

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