OpenCV绘图函数(4)绘制轮廓线的函数drawContours()的使用

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

算法描述

函数会在图像中绘制轮廓线,如果 thickness ≥ 0,则绘制轮廓线;如果 thickness < 0,则填充由轮廓包围的区域。下面的例子展示了如何从二值图像中检索连接组件并对其进行标记:

cpp 复制代码
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
    Mat src;
    // the first command-line parameter must be a filename of the binary
    // (black-n-white) image
    if( argc != 2 || !(src=imread(argv[1], IMREAD_GRAYSCALE)).data)
        return -1;
    Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);
    src = src > 1;
    namedWindow( "Source", 1 );
    imshow( "Source", src );
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours( src, contours, hierarchy,
        RETR_CCOMP, CHAIN_APPROX_SIMPLE );
    // iterate through all the top-level contours,
    // draw each connected component with its own random color
    int idx = 0;
    for( ; idx >= 0; idx = hierarchy[idx][0] )
    {
        Scalar color( rand()&255, rand()&255, rand()&255 );
        drawContours( dst, contours, idx, color, FILLED, 8, hierarchy );
    }
    namedWindow( "Components", 1 );
    imshow( "Components", dst );
    waitKey(0);
}

函数原型

cpp 复制代码
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() 
)		

参数

  • 参数image 目标图像。
  • 参数icontours 所有输入的轮廓。每个轮廓存储为点向量。
  • 参数icontourIdx 指示要绘制的轮廓的参数。如果它是负数,则绘制所有轮廓。
  • 参数icolor 轮廓的颜色。
  • 参数ithickness 绘制轮廓所用线条的粗细。如果它是负数(例如,thickness=FILLED),则绘制轮廓内部。
    l- 参数iineType 线条连接方式。参见 LineTypes
  • 参数ihierarchy 关于层次结构的可选信息。仅当你想要绘制部分轮廓时需要它(参见 maxLevel)。
  • 参数imaxLevel 要绘制的轮廓的最大层级。如果它是 0,则仅绘制指定的轮廓。如果它是 1,则函数绘制轮廓及其所有的嵌套轮廓。如果它是 2,则函数绘制轮廓、所有嵌套轮廓以及所有嵌套至嵌套的轮廓等。此参数仅在存在层次结构的情况下有效。
  • 参数ioffset 可选的轮廓偏移参数。将所有绘制的轮廓按照指定的偏移 (dx, dy) 进行平移。

代码示例

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main( int argc, const char** argv )
{
    std::vector< std::vector< cv::Point > > contours;
    std::vector< cv::Vec4i > hierarchy;

    cv::Mat image3 = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/qiu.jpg", cv::IMREAD_GRAYSCALE );

    // 二值化图像
    cv::Mat binary;
    cv::threshold( image3, binary, 200, 255, cv::THRESH_BINARY );

    cv::findContours( binary, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE );

    Mat frame = Mat::zeros( image3.size(), CV_8UC3 );
    for ( size_t i = 0; i < contours.size(); i++ )
    {
        // 蓝色颜色
        cv::Scalar color( 255, 0, 0 );
        // 只绘制第一层的轮廓
        cv::drawContours( frame, contours, static_cast< int >( i ), color, 2, cv::LINE_8, hierarchy, 0 );
    }

    imshow( "原图", image3 );
    imshow( "画轮廓", frame );

    waitKey( 0 );

    return 0;
}

运行结果

相关推荐
ICscholar2 分钟前
深度Q网络(DQN)及其变体双深度Q网络(DDQN)对比学习
人工智能·神经网络·学习
serve the people6 分钟前
TensorFlow 2.0 手写数字分类教程之SparseCategoricalCrossentropy 核心原理(一)
人工智能·分类·tensorflow
lovingsoft6 分钟前
复用的Vibe Coding 提示词模板(含原型 / MVP、CRUD、UI 组件、调试反馈 4 类场景)
人工智能·ui·敏捷开发
乾元13 分钟前
LLM 自动生成安全基线与等保合规初稿——把“网络工程事实”转译为“可审计的制度语言”
运维·网络·人工智能·python·安全·架构
Das115 分钟前
【计算机视觉】05_不变性
人工智能·计算机视觉
全栈陈序员15 分钟前
【Python】基础语法入门(二十四)——文件与目录操作进阶:安全、高效地处理本地数据
开发语言·人工智能·python·学习
跨境卫士情报站23 分钟前
摆脱砍单魔咒!Temu 自养号系统化采购,低成本高安全
大数据·人工智能·安全·跨境电商·亚马逊·防关联
OAK中国_官方23 分钟前
将Roboflow工作流引入OAK4:简化实时计算机视觉
人工智能·计算机视觉
吴爃26 分钟前
N8N调用系统接口进行AI分析
运维·人工智能·ai
老鱼说AI26 分钟前
经典论文精读第一期:DeepSeek-R1-Zero ——RL奇迹
人工智能·分布式·深度学习·神经网络·自然语言处理·nlp·transformer