drawContours()函数解析

时间: 2023-11-14 22:21:46 浏览: 29
`drawContours()`函数是OpenCV中的一个函数,用于在图像上绘制轮廓。该函数的定义如下: ```python cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None) ``` 其中,参数含义如下: - `image`:要绘制轮廓的图像,必须为8位无符号图像。 - `contours`:表示轮廓的列表。每个轮廓都是一个包含边界点的numpy数组。 - `contourIdx`:要绘制的轮廓的索引。当设置为-1时,绘制所有轮廓。 - `color`:绘制轮廓的颜色。 - `thickness`:轮廓线的粗细。默认值为1。 - `lineType`:轮廓线的类型。默认为`cv2.LINE_8`。 - `hierarchy`:轮廓的层次结构信息。默认为`None`。 - `maxLevel`:要绘制的轮廓的最大层次结构级别。默认为`None`。 - `offset`:轮廓偏移量。默认为`(0,0)`。 `drawContours()`函数可以用于在图像上绘制单个轮廓或多个轮廓。如果要绘制多个轮廓,需要将所有的轮廓放在一个列表中,然后将该列表作为`contours`参数传递给函数。如果要绘制所有轮廓,可以将`contourIdx`参数设置为-1。 例如,下面的代码演示了如何使用`drawContours()`函数在图像上绘制轮廓: ```python import cv2 # 读取图像 img = cv2.imread('test.jpg') # 将图像转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 二值化处理 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 获取轮廓 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # 在图像上绘制轮廓 cv2.drawContours(img, contours, -1, (0, 255, 0), 2) # 显示图像 cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在这个例子中,我们首先将图像转换为灰度图像,然后对其进行二值化处理,接着使用`cv2.findContours()`函数获取轮廓。最后,我们使用`cv2.drawContours()`函数在图像上绘制轮廓,并将结果显示出来。 注意,`drawContours()`函数并不会修改传递给它的图像,而是返回一个绘制了轮廓的新图像。如果要修改原始图像,请将结果赋值给原始图像。

相关推荐

import json import base64 from PIL import Image import io import cv2 import numpy as np from ultralytics import YOLO import supervision as sv def init_context(context): context.logger.info("Init context... 0%") model_path = "yolov8m-seg.pt" # YOLOV8模型放在nuclio目录下构建 model = YOLO(model_path) # Read the DL model context.user_data.model = model context.logger.info("Init context...100%") def handler(context, event): context.logger.info("Run yolo-v8-seg model") data = event.body buf = io.BytesIO(base64.b64decode(data["image"])) threshold = float(data.get("threshold", 0.35)) context.user_data.model.conf = threshold image = Image.open(buf) yolo_results = context.user_data.model(image, conf=threshold)[0] labels = yolo_results.names detections = sv.Detections.from_yolov8(yolo_results) detections = detections[detections.confidence > threshold] masks = detections.xy conf = detections.confidence class_ids = detections.class_id results = [] if masks.shape[0] > 0: for label, score, mask in zip(class_ids, conf, masks): # 将mask转换为轮廓 contours, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: points = [] for point in contour: x = point[0][0] y = point[0][1] points.append([x, y]) results.append({ "confidence": str(score), "label": labels.get(label, "unknown"), "points": points, "type": "polygon",}) return context.Response(body=json.dumps(results), headers={}, content_type='application/json', status_code=200)不用supervision 包 用别的方式解析

最新推荐

recommend-type

Java自定义函数调用方法解析

主要介绍了java自定义函数调用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Python中enumerate函数代码解析

主要介绍了Python中enumerate函数代码解析,涉及函数说明以及相关示例,具有一定参考价值,需要的朋友可以了解下。
recommend-type

C++中的friend友元函数详细解析

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。友元函数的特点是能够访问类中的私有成员的非成员函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数...
recommend-type

Python reduce函数作用及实例解析

主要介绍了Python reduce函数作用及实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

Softmax函数原理及Python实现过程解析

主要介绍了Softmax函数原理及Python实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。