opencv学习日记——轮廓检测与绘制(效果很棒)

opencv学习日记——轮廓检测与绘制(效果很棒)

1、图像结果

在这里插入图片描述

2、代码部分

#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>

using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("圆形.png");
    if (img.empty())
    {
        cout << "输入为空" << endl;
        return -1;
    }
    imshow("原图", img);

    Mat gray, binary;
    //转化灰度值
    cvtColor(img, gray, COLOR_BGR2GRAY);
    //高斯滤波
    GaussianBlur(gray, gray, Size(13, 13), 4, 4);
    //自适应二值化
    //threshold(原图, 矩阵, 阈值, 阈值, 二值化类型)
    threshold(gray, binary, 170, 255, THRESH_BINARY | THRESH_OTSU);

    //发现轮廓
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    //findContours(原图(灰度图), 检测轮廓信息, 轮廓结构关系, 轮廓检验模式标志, 轮廓逼近方法标志, 偏移量)
    findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());

    //绘制轮廓
    for (int t = 0; t < contours.size(); t++)
    {
        //drawContours(原图, 轮廓, 轮廓数目, 颜色, 粗细, 边界线连接类型)
        drawContours(img, contours, t, Scalar(0, 0, 255), 2, 8);
    }

    for (int i = 0; i < hierarchy.size(); i++)
    {
        cout << hierarchy[i] << endl;

    }

    imshow("轮廓结果", img);
    waitKey(0);
    return 0;

}


‘’‘’‘’‘’‘’‘’‘’‘’
[-1, -1, 1, -1]
[4, -1, 2, 0]
[3, -1, -1, 1]
[-1, 2, -1, 1]
[5, 1, -1, 0]
[6, 4, -1, 0]
[12, 5, 7, 0]
[8, -1, -1, 6]
[9, 7, -1, 6]
[10, 8, -1, 6]
[11, 9, -1, 6]
[-1, 10, -1, 6]
[14, 6, 13, 0]
[-1, -1, -1, 12]
[15, 12, -1, 0]
[-1, 14, 16, 0]
[-1, -1, -1, 15]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值