Skip to content

Instantly share code, notes, and snippets.

@euclidr
Last active April 25, 2021 02:03
Show Gist options
  • Save euclidr/3ee84361d67433f15da1b9212f14e182 to your computer and use it in GitHub Desktop.
Save euclidr/3ee84361d67433f15da1b9212f14e182 to your computer and use it in GitHub Desktop.
找出图片中的竖线边界
# -*- coding: utf-8 -*-
import cv2
def detect_vertical_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment