fra*_*acv -1 python opencv image-morphology
我想为附加图像提取 2 个蒙版中的水平线和垂直线。
我尝试过形态学操作来做到这一点,
horizontalStructure = cv.getStructuringElement(cv.MORPH_RECT, (horizontal_size, 1))
verticalStructure = cv.getStructuringElement(cv.MORPH_RECT, (1, verticalsize))
Run Code Online (Sandbox Code Playgroud)
但问题是,它将线检测为矩形,然后以2条线代表矩形2条边的形式绘制它。
有什么想法可以解决这个问题吗?
横向结果:
垂直结果:
编辑:那是我的另一张图片:
你对结构元素做了什么?你的其余代码在哪里?
我建议使用形态学开放 using 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()
Run Code Online (Sandbox Code Playgroud)
我得到了以下两个面具,对我来说看起来很不错:
希望有帮助!
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.1
NumPy: 1.18.1
OpenCV: 4.2.0
----------------------------------------
Run Code Online (Sandbox Code Playgroud)