enzoyang
2021-04-24 16:37:52 +08:00
谢谢大家的提示,我试验了一下,边缘检测加判断图中竖直的边缘应该够用了
```
def detect_horizontal_edges(cv_img) -> list:
"""找出图片中的竖线边界"""
if isinstance(cv_img, str):
cv_img = cv2.imread(cv_img)
# 转成灰度
gray_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY)
# 边缘检测
canny = cv2.Canny(gray_img, 20, 100)
# 加粗边缘
kernal = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
dilated = cv2.dilate(canny, kernal)
# 判断竖线
h, w = dilated.shape[0], dilated.shape[1]
threshold = int(h * 0.75)
edge_positions = []
for x in range(w):
white_dots = 0
for y in range(h):
if dilated[y, x] == 255:
white_dots += 1
if white_dots > threshold:
edge_positions.append(x)
return edge_positions
```