本文共 2117 字,大约阅读时间需要 7 分钟。
知识掌握
cv2.threshold()函数该函数用于设置固定级别的阈值应用于多通道矩阵,将灰度图像转换为二值图像,或去除指定级别的噪声,或过滤掉过小或者过大的像素点。函数定义如下:
Python: cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst
其中:
示例代码:
import cv2 img = cv2.imread('1.jpg') cv2.imshow("src", img) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, dst = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) cv2.imshow("dst", dst) cv2.waitKey(0) cv2.findContours()函数
该函数用于检测图像中的物体轮廓。函数定义如下:
cv2.findContours(image, mode, method)
函数返回两个值:contours和hierarchy。在OpenCV3中,返回三个值:img、contours、hierarchy。其中:
示例代码:
import numpy as np import cv2 rectangle = np.zeros((300,300), dtype="uint8") cv2.rectangle(rectangle, (25,25), (275,275), 255, -1) cv2.imshow("Rectangle", rectangle) img, contours, hierarchy = cv2.findContours(rectangle, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) print(contours) print(hierarchy) cv2.waitKey(0) cv2.polylines()函数
该函数用于绘制多边形。函数定义如下:
cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[,shift]]])
参数说明:
示例代码:
import numpy as np import cv2 # 创建黑色图像 img = np.zeros((200, 200, 3), np.uint8) pts = np.array([[10, 5], [20, 30], [70, 20], [80, 15]]) cv2.polylines(img, pts, True, (0, 255, 0), 2, cv2.LINE_AA, shift=1) cv2.imshow("Polylines", img) cv2.waitKey(0) 转载地址:http://gvafk.baihongyu.com/