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()
相关推荐
AngelPP14 小时前
OpenClaw 架构深度解析:如何把 AI 助手搬到你的个人设备上
人工智能
宅小年14 小时前
Claude Code 换成了Kimi K2.5后,我再也回不去了
人工智能·ai编程·claude
AI探索者14 小时前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者14 小时前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
九狼14 小时前
Flutter URL Scheme 跨平台跳转
人工智能·flutter·github
ZFSS14 小时前
Kimi Chat Completion API 申请及使用
前端·人工智能
天翼云开发者社区15 小时前
春节复工福利就位!天翼云息壤2500万Tokens免费送,全品类大模型一键畅玩!
人工智能·算力服务·息壤
知识浅谈16 小时前
教你如何用 Gemini 将课本图片一键转为精美 PPT
人工智能
FishCoderh16 小时前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅16 小时前
Python函数入门详解(定义+调用+参数)
python