OpenCV 图形API(64)图像结构分析和形状描述符------在图像中查找轮廓函数findContours()

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

在二值图像中查找轮廓。

该函数使用[253]中的算法从二值图像检索轮廓。轮廓是形状分析以及对象检测和识别的有用工具。请参阅 OpenCV 示例目录中的 squares.cpp。

注意

函数文本ID为 "org.opencv.imgproc.shape.findContours"。

函数原型

cpp 复制代码
GArray<GArray<Point> > cv::gapi::findContours 
(
 	const GMat &  	src,
	const RetrievalModes  	mode,
	const ContourApproximationModes  	method,
	const GOpaque< Point > &  	offset 
) 	

参数

  • 参数 src:输入灰度图像 CV_8UC1。非零像素被视为 1。零像素保持为 0,因此图像被视为二值图像。您可以使用 compare、inRange、threshold、adaptiveThreshold、Canny 等方法从灰度或彩色图像创建二值图像。如果 mode 等于 RETR_CCOMP,则输入也可以是标签的 32 位整数图像 (CV_32SC1)。如果 mode 是 RETR_FLOODFILL,则仅支持 CV_32SC1。
  • 参数 mode:轮廓检索模式,参见 RetrievalModes。
  • 参数 method:轮廓近似方法,参见 ContourApproximationModes。
  • 参数 offset:可选偏移量,每个轮廓点都会根据此偏移量进行移动。当轮廓是从图像 ROI 中提取并需要在整个图像上下文中进行分析时,这非常有用。

返回值

检测到的轮廓的 GArray。每个轮廓都存储为点的 GArray。

代码示例

cpp 复制代码
#include <opencv2/gapi.hpp>
#include <opencv2/gapi/core.hpp>
#include <opencv2/gapi/imgproc.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace cv::gapi;

int main() {
    // 读取输入图像,这里假设为灰度图像
    Mat src = imread("/media/dingxin/data/study/OpenCV/sources/images/Lenna.png", IMREAD_GRAYSCALE);
    if (src.empty()) {
        std::cerr << "无法读取图像" << std::endl;
        return -1;
    }

    // 转换为二值图像
    Mat binary;
    threshold(src, binary, 127, 255, THRESH_BINARY);

    // 创建一个彩色版的输入图像,以便绘制彩色边框
    Mat src_color;
    cvtColor(src, src_color, COLOR_GRAY2BGR);

    // 定义G-API网络
    GMat in;
    auto contours_op = gapi::findContours(in, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // 移除偏移点参数
    GComputation comp(GIn(in), GOut(contours_op));

    // 使用G-API获取轮廓
    std::vector<std::vector<Point>> contours_host; // Host storage for contours

    // Apply the computation to get contours
    comp.apply(cv::gin(binary), cv::gout(contours_host)); // 使用gin和gout宏来包装输入和输出

    // 对于每个轮廓,计算并绘制边界矩形
    for (const auto& contour : contours_host) {
        // 使用OpenCV获取边界矩形
        Rect bounding_rect = boundingRect(contour);

        // 确保边界矩形不会超出图像边界
        bounding_rect &= Rect(0, 0, src.cols, src.rows);

        // 额外检查以防止空矩形或其他问题
        if (bounding_rect.width == 0 || bounding_rect.height == 0) continue;

        // 在原图上绘制矩形
        rectangle(src_color, bounding_rect.tl(), bounding_rect.br(), Scalar(0, 255, 0), 2);
    }

    // 显示结果
    imshow("Bounding Rect", src_color);
    waitKey();

    return 0;
}

运行结果

相关推荐
CHANG_THE_WORLD8 分钟前
封装一个png的编码解码操作
图像处理·人工智能·计算机视觉
赛丽曼22 分钟前
Assistant API的原理及应用
人工智能·chatgpt
Yo_Becky1 小时前
【PyTorch】PyTorch预训练模型缓存位置迁移,也可拓展应用于其他文件的迁移
人工智能·pytorch·经验分享·笔记·python·程序人生·其他
DeepSeek-大模型系统教程1 小时前
深入金融与多模态场景实战:金融文档分块技术与案例汇总
人工智能·ai·语言模型·程序员·大模型·大模型学习·大模型教程
xinxiangwangzhi_1 小时前
pytorch底层原理学习--PyTorch 架构梳理
人工智能·pytorch·架构
yzx9910131 小时前
关于网络协议
网络·人工智能·python·网络协议
AiTEN_Robot1 小时前
AGV 无人叉车关键技术问题解析:精准定位算法 / 安全避障逻辑 / 系统对接协议全方案
人工智能·机器人·自动化·制造
云天徽上1 小时前
【PaddleOCR】OCR常见关键信息抽取数据集,包含FUNSD、XFUND、WildReceipt等整理,持续更新中......
人工智能·计算机视觉·信息可视化·paddlepaddle·paddleocr·文本识别
zskj_zhyl1 小时前
智绅科技:以科技为翼,构建养老安全守护网
人工智能·科技·安全
刘海东刘海东1 小时前
结构型智能科技的关键可行性——信息型智能向结构型智能的转换(提纲)
人工智能