opencv学习(二十三)寻找并绘制轮廓findCounters()/找出凸包convexHull()

1,查找轮廓findCounters()函数

这里写图片描述
这里写图片描述
这里写图片描述

2,绘制轮廓drawCounters()

这里写图片描述
这里写图片描述

实例1查找轮廓

#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    //【1】读图
    Mat srcimg = imread("D://1.jpg", 0);
    imshow("原始图", srcimg);

    //【2】初始化结果图
    Mat dstimg = Mat::zeros(srcimg.rows, srcimg.cols, CV_8UC3);

    //【3】取阈值大于119的原图部分
    srcimg = srcimg > 119;
    imshow("取阈值后", srcimg);

    //【4】定义轮廓和层次结构
    vector<vector<Point>>contours;
    vector<Vec4i> hierarchy;

    //【5】查找轮廓
    findContours(srcimg, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);

    //【6】历遍所有顶层可以囊括,以随机颜色绘制出每个连接组件的颜色
    int index = 0;
    for (;index>=0;index=hierarchy[index][0])
    {
        Scalar color(rand() & 255, rand() & 255, rand() & 255);//rand()&255用法
        drawContours(dstimg, contours, index, color, FILLED, 8, hierarchy);
    }
    //【7】显示最后轮廓图
    imshow("轮廓图", dstimg);

    waitKey(0);
}

这里写图片描述

实例2查找并绘制轮廓

#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

//全局变量申明
Mat g_srcimg, g_grayimg;
int g_thresh = 80;
int g_thresh_max = 255;
RNG g_rng(12345);
Mat g_cannyMat_output;
vector<vector<Point>> g_vContours;
vector<Vec4i>g_vHierarchy;

//全局函数申明
void on_ThreshChange(int, void*);

//主函数
int main()
{
    //【1】读图处理
    g_srcimg = imread("D://1.jpg", 0);
    blur(g_srcimg, g_grayimg, Size(3, 3));
    imshow("灰度模糊降噪图", g_grayimg);

    //【2】创建滑动条
    createTrackbar("canny阈值", "灰度模糊降噪图", &g_thresh, g_thresh_max, on_ThreshChange);
    on_ThreshChange(0, 0);
    waitKey(0);
    return(0);


}//【3】回调函数
void on_ThreshChange(int, void*)
{
    //【4】边缘检测
    Canny(g_grayimg, g_cannyMat_output, g_thresh, g_thresh * 2, 3);

    //【5】查找轮廓
    findContours(g_cannyMat_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));


    //【6】绘制轮廓
    Mat drawing = Mat::zeros(g_cannyMat_output.size(), CV_8UC3);
    int i = 0;
    for (; i <g_vContours.size(); i++)
    {
        Scalar color(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));// g_rng.uniform(0, 255))用法?
        drawContours(drawing, g_vContours, i, color, 2, 8, g_vHierarchy, 0, Point());
    }
    //【7】显示最后轮廓图
    imshow("轮廓图", drawing);
}

这里写图片描述

2,找凸包

这里写图片描述
这里写图片描述

实例3,随机点找凸包实例(vector出错–①复制时注意变动的东西②for循环语句后面不要跟封号;)


#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
    //初始化变量和随机值
    Mat image(600, 600, CV_8UC3);
    RNG&rng = theRNG();

    //循环
    while (1)
    {
        char key;//键值
        int count = (unsigned)rng % 100 + 3;//随机生成点数量
        vector<Point> points;//点值

                            //随机生成坐标
        for (int i = 0; i < count; i++)
        {
            Point point;
            point.x = rng.uniform(image.cols / 4, image.cols * 3 / 4);
            point.y = rng.uniform(image.rows / 4, image.rows * 3 / 4);//注意X,y分别赋值

            points.push_back(point);

        }

        //检测凸包
        vector<int>hull;
        convexHull(Mat(points), hull, true);


        //绘制出随机颜色点
        image = Scalar::all(0);

        for (int i = 0; i < count; i++)//for后面不需要分号
        {
            circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), FILLED, LINE_AA);
        }


        //准备参数
        int hullcount = (int)hull.size();
        Point point0 = points[hull[hullcount - 1]];//连接凸包边的坐标点


                                                   //绘凸包的边
        for (int i = 0; i < hullcount; i++)
        {
            Point point = points[hull[i]];
            line(image, point0, point, Scalar(255, 255, 255), 2, LINE_AA);
            point0 = point;

        }
        imshow("凸包检测实例", image);
        //按下ESC或q退出
        key = (char)waitKey();
        if (key == 27 || key == 'q' || key == 'Q')
            break;
    }
    return 0;
}

这里写图片描述
这里写图片描述

实例4,对图片轮廓找凸包(类似实例2,画出轮廓后在对其求凸包)

猜你喜欢

转载自blog.csdn.net/qq_41553038/article/details/80161871
今日推荐