OrionVision/utils/path_detect.py

38 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cv2
import numpy as np
# https://www.cnblogs.com/meikaiyuan/p/16805539.html
def read_image(path):
raw = cv2.imread(path)
# 设定颜色HSV范围假定为红色
color_lower = np.array([0, 0, 150])
color_upper = np.array([255, 255, 209])
# 将图像转化为HSV格式
hsv = cv2.cvtColor(raw, cv2.COLOR_BGR2HSV)
# 去除颜色范围外的其余颜色
mask = cv2.inRange(hsv, color_lower, color_upper)
cv2.imshow("mask", mask)
# 二值化操作
ret, binary = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY)
# 膨胀操作,因为是对线条进行提取定位,所以腐蚀可能会造成更大间隔的断点,将线条切断,因此仅做膨胀操作
kernel = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(binary, kernel, iterations=1)
#
# img2 = cv2.bitwise_and(img, img, mask=mask)
# cv2.imshow('image', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 获取图像轮廓坐标其中contours为坐标值此处只检测外形轮廓
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if __name__ == '__main__':
read_image("../data/004.png")