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

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

算法描述

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

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

函数原型


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) 进行平移。

代码示例

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

运行结果

在这里插入图片描述

  • 18
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值