所用函数:
cv2.threshold() —— 阈值处理
cv2.findContours() —— 轮廓检测
cv2.boundingRect() —— 最大外接矩阵
cv2.rectangle() —— 画出矩形
cv2.minAreaRect —— 找到最小外接矩形(矩形具有一定的角度)
cv2.boxPoints —— 外接矩形的坐标位置
cv2.drawContours(image, [box], 0, (0, 0, 255), 3) —— 根据点画出矩形
# -*- coding: utf-8 -*-
import cv2
import numpy as np
image = cv2.imread('/home/rose/PycharmProjects/PSENet.pytorch/test/img/20190507193429910.png')
# 图像转灰度图
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 图像转二值图
ret, thresh = cv2.threshold(img, 230, 255, cv2.THRESH_BINARY_INV)
# 功能:cv2.findContours()函数来查找检测物体的轮廓。
#参数:
# 参数1:寻找轮廓的图像,接收的参数为二值图,即黑白的(不是灰度图),所以读取的图像要先转成灰度的,再转成二值图