020 OpenCV 轮廓、外接圆、外接矩形

一、环境

本文使用环境为:

  • Windows10
  • Python 3.9.17
  • opencv-python 4.8.0.74

二、原理

2.1 函数接口

OpenCV中的findContours函数用于检测图像中的轮廓。轮廓是图像中连续的点集,它们通常表示物体的边缘或形状。在计算机视觉和图像处理中,轮廓分析是一种常见的任务,例如目标检测、形状识别等。

findContours函数的基本语法如下:

python 复制代码
contours, hierarchy = cv.findContours(image, mode, method[, contours[, hierarchy[, offset ]]])

参数说明:

  • image:输入图像,通常是一个二值图像。
  • mode:轮廓检索模式。这个参数决定了函数如何返回轮廓。常见的模式有:
    • cv2.RETR_EXTERNAL:只检索最外层的轮廓。
    • cv2.RETR_LIST:检索所有轮廓并将其保存到列表中。
    • cv2.RETR_CCOMP:检索所有轮廓,并将它们组织到两个不同的层次结构中(例如,外部和内部)。
    • cv2.RETR_TREE:检索所有轮廓,并将它们组织到一个层次结构中。
  • method:轮廓的近似方法。常见的有:
    • cv2.CHAIN_APPROX_SIMPLE:压缩水平、垂直和对角分段。
    • cv2.CHAIN_APPROX_NONE:存储所有的段(4点)。
    • cv2.CHAIN_APPROX_SIMPLEcv2.CHAIN_APPROX_DP:使用动态规划压缩轮廓。
  • contours(可选):输出参数,返回找到的轮廓。
  • hierarchy(可选):输出参数,返回有关轮廓之间关系的信息。
  • offset(可选):偏移量,用于调整轮廓的位置。

返回值:

  • 如果指定了 contours 参数,则返回找到的轮廓。
  • 如果指定了 hierarchy 参数,则返回有关轮廓之间关系的信息。

2.2 原理理解

在OpenCV库中,函数cv2.findContours()是一个用于查找图像中物体轮廓的重要工具。该函数的主要参数包括:输入图像、轮廓检索模式和近似方法等。

首先,输入的图像通常是二值化的单通道图像,其中黑色代表背景,白色代表目标物体。这样的图像通常通过Canny或拉普拉斯等边缘检测算子进行处理得到。

其次,轮廓检索模式决定了如何处理图像中的轮廓。例如,cv2.RETR_EXTERNAL只检测外轮廓;cv2.RETR_LIST检测的轮廓不建立等级关系;cv2.RETR_CCOMP建立两个等级的轮廓,上一层为外边界,内层为内孔的边界;而cv2.RETR_TREE则建立一个等级树结构的轮廓。

最后,近似方法决定了如何简化轮廓。例如,cv2.CHAIN_APPROX_SIMPLE就表示用尽可能少的像素点表示轮廓。

函数的返回值包括两个部分:contours和hierarchy。其中,contours是一个包含所有检测到的轮廓信息的数组,每个轮廓又是由若干个点所构成的;而hierarchy则是一个包含了各轮廓之间的层次关系的数组。

三、完整代码

python 复制代码
from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
import random as rng

rng.seed(12345)

def thresh_callback(val):
    threshold = val
    # 使用canny检测边缘
    canny_output = cv.Canny(src_gray, threshold, threshold * 2)
    # 查找轮廓
    contours, _ = cv.findContours(canny_output, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    

    # 这里在分配空间
    contours_poly = [None]*len(contours)
    boundRect = [None]*len(contours)
    centers = [None]*len(contours)
    radius = [None]*len(contours) 

    # 依据canny检测出来的边缘,下面查找边缘的轮廓、边缘的外接圆、外接矩形
    for i, c in enumerate(contours):
        contours_poly[i] = cv.approxPolyDP(c, 3, True) # 轮廓
        boundRect[i] = cv.boundingRect(contours_poly[i]) # 外接矩形
        centers[i], radius[i] = cv.minEnclosingCircle(contours_poly[i]) # 外接圆
   
    # 搞一张黑色的图,用于绘制
    drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
    
    # 绘制轮廓、矩形、圆
    for i in range(len(contours)):
        color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
        cv.drawContours(drawing, contours_poly, i, color)
        cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \
          (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
        cv.circle(drawing, (int(centers[i][0]), int(centers[i][1])), int(radius[i]), color, 2) 
    cv.imshow('Contours', drawing)

parser = argparse.ArgumentParser(description='Code for Creating Bounding boxes and circles for contours tutorial.')
parser.add_argument('--input', help='Path to input image.', default='data/stuff.jpg')
args = parser.parse_args()
# 读取图片
src = cv.imread(cv.samples.findFile(args.input))
if src is None:
    print('Could not open or find the image:', args.input)
    exit(0)

# 彩色图转灰度图
src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
# 图片平滑
src_gray = cv.blur(src_gray, (3,3))

# 显示原彩色图
source_window = 'Source'
cv.namedWindow(source_window)
cv.imshow(source_window, src)

# 创建滑条,控制canny查找边缘的阈值
max_thresh = 255
thresh = 100 # canny初始化阈值
cv.createTrackbar('Canny thresh:', source_window, thresh, max_thresh, thresh_callback)
thresh_callback(thresh)

cv.waitKey()
相关推荐
咕噜咕噜啦啦1 分钟前
RTX5090配置DGL
pytorch·python·conda·pip
零壹AI实验室4 分钟前
用AI 10分钟搭建一个监控系统:Prometheus + Grafana 实战
人工智能·grafana·prometheus
志栋智能6 分钟前
超自动化巡检:量化运维成效的标尺
运维·网络·人工智能·自动化
AI科技星7 分钟前
紫金山天文台与6G 超导太赫兹实验对比【乖乖数学】
人工智能·线性代数·机器学习·量子计算·agi
摩尔线程7 分钟前
摩尔线程携手紫光计算机发布《语音识别全栈国产化技术实践白皮书》
人工智能·语音识别·摩尔线程
字节跳动开源8 分钟前
局中局!给 Agent 装上 OpenViking,它们竟然学会了“记仇”和“伪装”?
人工智能·开源·llm
Exploring11 分钟前
通过 Vibe Coding,我开发的第一款鸿蒙 App 上架了,欢迎大家下载体验
人工智能
杀生丸学AI14 分钟前
【VALSE 2026】AI领域年度重要进展
人工智能
2401_8246976617 分钟前
如何实现SQL存储过程状态监控_编写实时运行监控仪表盘
jvm·数据库·python
iAm_Ike20 分钟前
c++怎么在写入文件流时通过peek预读功能实现复杂的逻辑判断【实战】
jvm·数据库·python