0

我有一张图片:

输入.png

我必须检测两者之间的空白并基本上将其划分为这样的两部分-

预期输出.png

这是我到目前为止所编码的......但它确实只检测到黑线而不是中间的白色区域。

import numpy as np
import cv2

img = cv2.imread('12.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
median = cv2.medianBlur(gray,5)
minLineLength = 250
maxLineGap = 100
lines = cv2.HoughLinesP(edges,0.3,np.pi/180,250,minLineLength,maxLineGap)
for line in lines:
    x1,y1,x2,y2 =line[0]
    cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imwrite('newwhite.png',img)
4

1 回答 1

1

我有一个基于轴平均值的简单解决方案。我更喜欢 scikit-image 反对 opencv,但你可以使用 cv2。

import matplotlib.pyplot
import numpy as np
import skimage.io
import skimage.color
import skimage.morphology
import scipy.signal

img = skimage.io.imread('12.png')
gray = skimage.color.rgb2gray(img)

# Create some large dark area with the text, 10 is quite big!
eroded = skimage.morphology.erosion(gray, skimage.morphology.square(5))

# Compute mean values along axis 0 or 1
hist = np.mean(eroded, axis=0)

# Search large (here 3% of dimension size) and distant (here 20% of dimension size) peaks
scipy.signal.find_peaks(hist, width=len(hist)*3//100, distance=len(hist)*20//100)

然后每个峰代表图像一维中的一条白线

于 2020-07-05T19:25:07.087 回答