• 【OpenCV-Python】教程:3-9 轮廓(1)开始


    OpenCV Python 轮廓-开始

    【目标】

    • 理解什么是轮廓?
    • 如何找轮廓?
    • 如何画轮廓?
    • 使用 cv2.findContours(), cv2.drawContours()

    【概述】

    轮廓简单的说就是一个连接了很多连续点(沿着边界)的曲线,有相同的颜色和亮度。轮廓是一个非常有用的工具,可以用于形状分析,目标检测和识别。

    为了更高的准确度,常使用二值图像,所以在寻找轮廓前,需要对图像进行二值化或Canny边缘检测。在 OpenCV 里,寻找轮廓就像在黑色背景上寻找白色物体,所以记住,目标必须为白色,背景必须为黑色

    【代码】

    在这里插入图片描述

    在这里插入图片描述

    CHAIN_APPROX_SIMPLE 比 最原始的所有点都存储要好得多。

    import numpy as np
    import cv2
    
    im = cv2.imread('testcontours1.png')
    imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(imgray, 127, 255, 0)
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    cnt = contours[0]
    print(cnt.shape)
    
    cv2.drawContours(im, [cnt], 0, (0,255,0), 3, maxLevel=0)
    cv2.imshow('dst', im)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    显示轮廓方式一
    cv2.drawContours(img, contours, -1, (0,255,0), 3)

    显示轮廓方式二
    cv2.drawContours(img, contours, 3, (0,255,0), 3)

    显示轮廓方式三
    cnt = contours[4]
    cv2.drawContours(img, [cnt], 0, (0,255,0), 3)

    【接口】

    • findContours
    void cv::findContours	(	InputArray 	image,
    OutputArrayOfArrays 	contours,
    OutputArray 	hierarchy,
    int 	mode,
    int 	method,
    Point 	offset = Point() 
    );
    
    void cv::findContours	(	InputArray 	image,
    OutputArrayOfArrays 	contours,
    int 	mode,
    int 	method,
    Point 	offset = Point() 
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    cv2.findContours(	image, mode, method[, contours[, hierarchy[, offset]]]	) ->	contours, hierarchy
    
    • 1

    寻找二值图像的轮廓

    • image: 8位单通道图像,非零像素被认为是1,零值保持为0值,所以图像被看做是二值图像。可以利用 compare, inRange, threshold, adaptiveThreshold, Canny 或其他创建二值图像的函数。如果 mode 是 RETR_CCOMP 或 RETR_FLOODFILL ,则输入可以是32位图像标签(CV_32SC1)
    • contours: 检测到的轮廓,每个轮廓被存储为 vector points(e.g. std::vector >).
    • hierarchy: 可选的输出 vector (e.g. std::vector), 包含关于图像拓扑的信息,它的元素与图像的轮廓数量一样多。对于 第 i 个轮廓 contours[i], 元素 hierarchy[i][0] 表示同层次的下一个轮廓索引, 元素 hierarchy[i][1] 表示同层次的上一个轮廓索引,元素 hierarchy[i][2] 表示第一个子轮廓索引,元素 hierarchy[i][3] 表示父轮廓索引;如果没有,则对应值为负数。在 Python 中,hierarchy[0][i] 可以访问第i个轮廓的层级元素。
    • mode: 轮廓提取模式,见 RetrievalModes
    • method: 轮廓近似方法,见 ContourApproximationModes
    • offset: 每个轮廓点的偏移点,对于ROI来说非常有用。
    • RetrievalModes

    在这里插入图片描述

    • ContourApproximationModes

    在这里插入图片描述

    • drawContours
    void cv::drawContours	(	InputOutputArray 	image,
    InputArrayOfArrays 	contours,
    int 	contourIdx,
    const Scalar & 	color,
    int 	thickness = 1,
    int 	lineType = LINE_8,
    InputArray 	hierarchy = noArray(),
    int 	maxLevel = INT_MAX,
    Point 	offset = Point() 
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    cv2.drawContours(	image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]]	) ->	image
    
    • 1

    绘制轮廓或填充轮廓
    如果 t h i c k n e s s < 0 thickness<0 thickness0,填充轮廓,否则绘制轮廓线

    • image: 目标图像
    • contours: 所有的输入轮廓,每个轮廓存储为 point vector
    • contourIdx: 轮廓的索引,如果为负数,则绘制所有
    • color: 绘制颜色
    • thickness: 线条粗细, 如果为负数,则表示填充
    • lineType: 线的类型,见 LineTypes
    • hierarchy: 如果只想绘制某些级别的轮廓时,可选。
    • maxLevel: 需要绘制的最大层级,如果是0,则只有特殊的轮廓被绘制;如果是1,则绘制轮廓和子轮廓;如果是2,则绘制轮廓、子轮廓、子轮廓的子轮廓等等。
    • offset: 偏移量,当用ROI时;

    【参考】

    1. OpenCV官方文档
  • 相关阅读:
    软件测试之Web测试流程和方法
    深度剖析 Apache EventMesh 云原生分布式事件驱动架构
    【基于FreeRTOS的STM32F103系统】编写FreeRTOS程序
    右值引用&移动构造函数
    华为Atlas 300I 推理卡显卡安装
    JavaScript知识点复习--(二)函数高级
    MATLAB | 一种简易的随机曼陀罗图形生成函数
    web前端面试题附答案003-说一下你都用过那些格式的图片
    6-1应用层-网络应用模型
    微服务Spring Boot 整合Redis 基于Redis的Stream 消息队列 实现异步秒杀下单
  • 原文地址:https://blog.csdn.net/zhoujinwang/article/details/127986008