【opencv】示例-contours2.cpp 使用 findContours 和drawContours函数来查找和绘制轮廓

cpp 复制代码
// 包含OpenCV图像处理头文件
#include "opencv2/imgproc.hpp"
// 包含OpenCV高级用户界面头文件
#include "opencv2/highgui.hpp"
// 化学数学函数库头文件
#include <math.h>
// 包含输入输出流头文件
#include <iostream>


// 使用cv和std命名空间中定义的所有内容
using namespace cv;
using namespace std;


// 帮助函数的声明和实现
static void help(char** argv)
{
    // 输出帮助信息
    cout
        << "\nThis program illustrates the use of findContours and drawContours\n"
        << "The original image is put up along with the image of drawn contours\n"
        << "Usage:\n";
    // 输出程序使用方法
    cout
        << argv[0]
        << "\nA trackbar is put up which controls the contour level from -3 to 3\n"
        << endl;
}


// 定义图像宽度为500
const int w = 500;
// 定义轮廓等级变量,初始值为3
int levels = 3;


// 定义存放轮廓的向量
vector<vector<Point> > contours;
// 定义层次结构的向量
vector<Vec4i> hierarchy;


// 滑块回调函数,当滑块值改变时调用
static void on_trackbar(int, void*)
{
    // 创建一个黑色的图像
    Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
    // 计算轮廓等级
    int _levels = levels - 3;
    // 根据轮廓等级绘制轮廓
    drawContours( cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128,255,255),
                  3, LINE_AA, hierarchy, std::abs(_levels) );


    // 显示轮廓图像
    imshow("contours", cnt_img);
}


// 主函数
int main( int argc, char** argv)
{
    // 解析命令行参数
    cv::CommandLineParser parser(argc, argv, "{help h||}");
    // 如果有help参数,则显示帮助信息
    if (parser.has("help"))
    {
        help(argv);
        return 0;
    }
    // 创建一个黑色的背景图
    Mat img = Mat::zeros(w, w, CV_8UC1);
    // 绘制6个面孔
    for( int i = 0; i < 6; i++ )
    {
        // 根据面孔索引计算偏移量
        int dx = (i%2)*250 - 30;
        int dy = (i/2)*150;
        // 定义白色和黑色
        const Scalar white = Scalar(255);
        const Scalar black = Scalar(0);


        // 如果是第一个面孔,绘制眼睛里的光芒
        if( i == 0 )
        {
            for( int j = 0; j <= 10; j++ )
            {
                double angle = (j+5)*CV_PI/21;
                line(img, Point(cvRound(dx+100+j*10-80*cos(angle)),
                    cvRound(dy+100-90*sin(angle))),
                    Point(cvRound(dx+100+j*10-30*cos(angle)),
                    cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
            }
        }
        
        // 绘制面孔的其他部分
        ellipse( img, Point(dx+150, dy+100), Size(100,70), 0, 0, 360, white, -1, 8, 0 );
        ellipse( img, Point(dx+115, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
        ellipse( img, Point(dx+185, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
        ellipse( img, Point(dx+115, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
        ellipse( img, Point(dx+185, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
        ellipse( img, Point(dx+115, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
        ellipse( img, Point(dx+185, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
        // 绘制一个椭圆,表示鼻子
        ellipse( img, Point(dx+150, dy+100), Size(10,5), 0, 0, 360, black, -1, 8, 0 );
        // 绘制一个椭圆,表示嘴巴
        ellipse( img, Point(dx+150, dy+150), Size(40,10), 0, 0, 360, black, -1, 8, 0 );
        // 绘制两个椭圆表示耳朵
        ellipse( img, Point(dx+27, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
        ellipse( img, Point(dx+273, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
    }
    // 显示带有面孔的图像
    namedWindow( "image", 1 );
    imshow( "image", img );
    // 提取轮廓
    vector<vector<Point> > contours0;
    findContours( img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);


    // 将提取的轮廓近似为多边形
    contours.resize(contours0.size());
    for( size_t k = 0; k < contours0.size(); k++ )
        approxPolyDP(Mat(contours0[k]), contours[k], 3, true);


    // 创建一个新窗口来显示轮廓
    namedWindow( "contours", 1 );
    // 创建滑块,用于控制轮廓等级
    createTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );


    // 初始调用滑块回调函数
    on_trackbar(0,0);
    // 等待用户按键事件
    waitKey();


    // 程序正常退出
    return 0;
}

代码的功能是演示如何使用 findContoursdrawContours 函数来查找和绘制轮廓。首先,程序创建了一个黑色的背景图,在上面绘制了六个有趣的面孔。然后,它提取这些面孔的轮廓,并使用轨迹条控制器来调整显示的轮廓等级。这是一个使用OpenCV进行图像处理和用户界面交云的演示程序,演示了轮廓绘制的基本概念。

go 复制代码
findContours( img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
css 复制代码
approxPolyDP(Mat(contours0[k]), contours[k], 3, true);
cpp 复制代码
drawContours( cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128,255,255),
              3, LINE_AA, hierarchy, std::abs(_levels) );

The End

相关推荐
JackieZhengChina2 分钟前
吴泳铭:AI最大的想象力不在手机屏幕,而是改变物理世界
人工智能·智能手机
ShuQiHere3 分钟前
【ShuQiHere】 探索数据挖掘的世界:从概念到应用
人工智能·数据挖掘
嵌入式杂谈3 分钟前
OpenCV计算机视觉:探索图片处理的多种操作
人工智能·opencv·计算机视觉
时光追逐者5 分钟前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
东隆科技5 分钟前
PicoQuant公司:探索铜铟镓硒(CIGS)太阳能电池技术,引领绿色能源革新
人工智能·能源
红米煮粥6 分钟前
图像处理-掩码
图像处理·opencv·计算机视觉
DisonTangor17 分钟前
上海AI气象大模型提前6天预测“贝碧嘉”台风登陆浦东 今年已多次精准预测
人工智能
人工智能培训咨询叶梓34 分钟前
生成式人工智能在无人机群中的应用、挑战和机遇
人工智能·语言模型·自然语言处理·aigc·无人机·多模态·生成式人工智能
羊小猪~~1 小时前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
Zhangci]1 小时前
OpenCv(一)
人工智能·opencv·计算机视觉