[英]Extract horizontal and vertical lines from an image
你對結構元素做了什么? 你的其余代碼在哪里?
我建議使用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()
我得到了以下兩個面具,對我來說看起來很不錯:
希望有幫助!
----------------------------------------
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.