30.凸包(Convex Hull)

1.主要内容

  • 概念介绍
  • API说明
  • 代码演示

2.概念介绍

  • 什么是凸包(Convex Hull),在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部。
  • 正式定义:包含点集合S中所有点的最小凸多边形称为凸包
    在这里插入图片描述
    检测算法- Graham扫描法(较常用的一个算法)
    在这里插入图片描述

3.Graham扫描法介绍

  • 首先选择Y方向最低的点作为起始点p0
  • 从p0开始极坐标扫描,依次添加p1….pn(排序顺序是根据极坐标的角度大小,逆时针方向)
  • 对每个点pi来说,如果添加pi点到凸包中导致一个左转向(逆时针方法)则添加该点到凸包, 反之如果导致一个右转向(顺时针方向)删除该点从凸包中

我们所需要掌握的
只是需要了解,OpenCV已经实现了凸包发现算法和API提供我们使用
该算法根据下图学习理解
在这里插入图片描述

4.API说明cv::convexHull

convexHull(
InputArray points,// 输入候选点,来自findContours
OutputArray hull,// 凸包
bool clockwise,// default true, 顺时针方向
bool returnPoints)// true 表示返回点个数,如果第二个参数是vector<Point>则自动忽略

5.代码演示:

主要步骤:
1.首先把图像从RGB转为灰度
2.然后再转为二值图像
3.在通过发现轮廓得到候选点
4.凸包API调用
5.绘制显示。

cvtColor(src,src_gray,CV_BGR2GRAY);
blur(src_gray,src_gray,Size(3,3),Point(-1,-1));

void Threshold_Callback(int ,void*){
    Mat threshold_output;
    vector<vector<Point>>contours;
    vector<Vec4i>hierarchy;
    
     threshold(src_gray,threshold_output,threshold_value,255,THRESH_BINARY)
    findContours(threshold_output,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMOPLE,POint(0,0));
    
    vector<vector<Point>>hull(contours.size());//存放凸包的点集(此处进行初始化)
    for(size_t i=0;i<contours.size();i++){
        convexHull(Mat(contours[i]),hull[i],false)
    }
    
    Mat dst= Mat::zeros(threshold_output,size(),CV_8UC3);
    for(size_t i = 0;i < contours.size();i++){
        Scalar color = Scalar (rng.uniform(0,255),rng.uniform(0,255),rng.uniform(0,255))       
        drawContours(dst,hull,i,color,1,LINE_8,hierarchy,0,Point(0,0));
        drawContours(dst,contours,i,color,1,LINE_8,hierarchy,0,Point(0,0));
       }
   imshow(output_win,dst);    
}

6.课外扩展

凸包
lconvexHull()函数
电脑的屏幕坐标系
threshold()函数

发布了66 篇原创文章 · 获赞 53 · 访问量 6812

猜你喜欢

转载自blog.csdn.net/qq_43367829/article/details/105424816