Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
找出图片中的竖线边界
# -*- 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