• 轮廓发现


    轮廓发现是基于图像边缘提取寻找对象轮廓的方法,所以边缘提取的阈值选定会影响最终的轮廓发现结果

    cv::findContours()

    InputOutputArray binImg//输入图像,非0像素被看成1,0的像素值保持不变8-bit

    OutputArrayOfArrays contours//全部发现的非轮廓对象

    OutputArray hierachy//图的拓卜结构

    int mode//轮廓的返回模式

    int method//发现方法

    Point offset=Point()//轮廓位移,默认是0

    cv::drawContours()

    InputOutputArray image表示目标图像,

    InputArrayOfArrays contour表示输入的轮廓组,每一组轮廓由点vector构成,

    int contourIdx指明画第几个轮廓,如果该参数为负值,则画全部轮廓,

    const Scalar& color为轮廓的颜色,

    int thickness为轮廓的线宽,如果为负值或CV_FILLED表示填充轮廓内部,

    int lineType为线型,

    InputArray hierarchy为轮廓结构信息,

    int maxLevel为maxLevel

    Point offset=Point()//轮廓位移,默认是0

    流程:

    彩图转灰度图

    灰度图转二值图像

    发现轮廓

    绘制轮廓

    #include"pch.h"
    #include<iostream>
    #include<math.h>
    #include<opencv2/opencv.hpp>
    
    using namespace std;
    using namespace cv;
    
    Mat gray_pic;
    int threshold_value = 100;
    int threshold_max = 255;
    void DemoContours(int, void*);
    int main(int argc, char**argv)
    {
        Mat src, dst;
        src = imread("2.jpg");
        namedWindow("output window", CV_WINDOW_AUTOSIZE);
        imshow("output window", src);
        cvtColor(src, gray_pic, COLOR_BGR2GRAY);
        createTrackbar("Threshold value", "output window", &threshold_value, threshold_max, DemoContours);
        DemoContours(0, 0);
        waitKey(0);
        return 0;
    }
    
    void DemoContours(int, void*)
    {
        Mat canny_output;
        vector<vector<Point>> points;
        vector<Vec4i>hierachy;
        Canny(gray_pic, canny_output, threshold_value, threshold_value * 2, 3, false);
        findContours(canny_output, points, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
        Mat dst = Mat::zeros(gray_pic.size(), CV_8UC3);
        RNG rng(12345);
        for (size_t i = 0; i < points.size(); ++i)
        {
            Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
            drawContours(dst, points, i, color, 2, 8, hierachy, 0, Point(0, 0));
        }
        imshow("rslt", dst);
    }

  • 相关阅读:
    js 检测浏览器
    js获取url参数
    js 使用Math函数取得数组最大最少值
    js 取一定范围内的整数
    遍历文件夹内所有文件
    'weinre' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 解决方案
    解决图片缓存导致页面刷新无效果问题
    JAVA中的几种基本数据类型是什么,各自占用多少字节
    Mac终端git,svn提交代码步骤
    小程序分享链接功能
  • 原文地址:https://www.cnblogs.com/wangtianning1223/p/13352584.html
Copyright © 2020-2023  润新知