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

运行结果

相关推荐
Neolnfra3 小时前
拒绝数据“裸奔”!把顶级AI装进自己的硬盘,这款神仙开源工具我粉了
人工智能·开源·蓝耘maas
code_li3 小时前
只花了几分钟,用AI开发了一个微信小程序!(附教程)
人工智能·微信小程序·小程序
飞Link3 小时前
瑞萨联姻 Irida Labs:嵌入式开发者如何玩转“端侧视觉 AI”新范式?
人工智能
RSTJ_16253 小时前
PYTHON+AI LLM DAY THREETY-SEVEN
开发语言·人工智能·python
郝学胜-神的一滴3 小时前
深度学习优化核心:梯度下降与网络训练全解析
数据结构·人工智能·python·深度学习·算法·机器学习
Aision_3 小时前
Agent 为什么需要 Checkpoint?
人工智能·python·gpt·langchain·prompt·aigc·agi
小贺儿开发3 小时前
《唐朝诡事录之长安》——盛世马球
人工智能·unity·ai·shader·绘画·影视·互动
秋94 小时前
ESP32 与 Air780E 4G 模块配合做 MQTT 数据传输
人工智能
DeepFlow 零侵扰全栈可观测4 小时前
运动战:AI 时代 IT 运维的决胜之道——DeepFlow 业务全链路可观测性的落地实践
运维·网络·人工智能·arcgis·云计算
链上日记4 小时前
AgentWin:AI Agent驱动的Web4智能金融新纪元
人工智能·金融