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;
}

运行结果

相关推荐
mwq301232 分钟前
GPT-2 中的残差权重初始化
人工智能
mwq3012330 分钟前
Transformer : 深度神经网络中的残差连接 (Residual Connection)
人工智能
信田君952731 分钟前
瑞莎星瑞(Radxa Orion O6) 基于 Android OS 使用 NPU的图片模糊查找APP 开发
android·人工智能·深度学习·神经网络
StarPrayers.1 小时前
卷积神经网络(CNN)入门实践及Sequential 容器封装
人工智能·pytorch·神经网络·cnn
周末程序猿1 小时前
谈谈上下文工程(Context Engineering)
人工智能
一水鉴天1 小时前
整体设计 逻辑系统程序 之29 拼语言+ CNN 框架核心定位、三阶段程序与三种交换模式配套的方案讨论 之2
人工智能·神经网络·cnn
海森大数据1 小时前
AI破解数学界遗忘谜题:GPT-5重新发现尘封二十年的埃尔德什问题解法
人工智能·gpt
望获linux2 小时前
【实时Linux实战系列】Linux 内核的实时组调度(Real-Time Group Scheduling)
java·linux·服务器·前端·数据库·人工智能·深度学习
Dev7z2 小时前
河南特色农产品识别系统:让AI守护“中原味道”
人工智能