概述
凸包(Convex Hull)是一个计算几何(图形学)中的概念,在一个实数向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包。
X的凸包可以用X内所有点(x1, x2….xn)的线性组合来构造。在二维欧几里得空间中,凸包可以想象为一条刚好包着所有点的橡皮圈,用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边形,它能包含点集中所有的点接!
API函数:
void cv::convexHull ( InputArray points,
OutputArray hull,
bool clockwise = false,
bool returnPoints = true
)
参数解释
- points:输入的二维点集,Mat类型数据即可
- hull:输出参数,用于输出函数调用后找到的凸包
- clockwise:操作方向,当标识符为真时,输出凸包为顺时针方向,否则为逆时针方向。
- returnPoints:操作标识符,默认值为true,此时返回各凸包的各个点,否则返回凸包各点的指数,当输出数组时std::vector时,此标识被忽略。
//寻找物体的凸包convexHull
//定义和输出vector容器点坐标
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <vector>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat testImage(600, 600, CV_8UC3);//画布大小600x600
RNG& rng = theRNG();
//按回车键一直更新
while (1)
{
//int count = (unsigned)rng % 100 + 8;//随机生成点的数量
int count = rng.uniform(5, 20);//随机产生点的个数
cout << "凸包包含 " << count << " 个点" << endl;
cout << "各点坐标如下:" << endl;//输出产生随机点个数
vector<Point>points;//vector容器存放点坐标
for (int i = 0; i < count; i++)
{
Point point;
//点坐标随机产生
point.x = rng.uniform(testImage.cols / 5, testImage.cols * 4 / 5); //横坐标x在范围(600/1,600*4/5)随机产生
point.y = rng.uniform(testImage.rows / 4, testImage.rows * 3 / 4);//纵坐标y在范围(600/4,600*3/4)随机产生
points.push_back(point);
cout << "Point" << i + 1 << ": " << point << endl;//依次输出随机产生每个点的坐标
}
cout << "基于Mat的vector:\n" << Mat(points) << endl;//基于Mat类的vector将以矩阵形式输出坐标
//检测凸包
vector<int >hull;
convexHull(Mat(points), hull, true);
testImage = Scalar::all(0);//将画布设置为黑色
for (int i = 0; i < count; i++)
circle(testImage, points[i], 5, Scalar(255, 0, 255), -1, 4);//以随机点为圆心,画出半径为5的实心圆(标记作用)
//参数准备
int hullcout = (int)hull.size();//凸包的边数
Point point0 = points[hull[hullcout - 1]];//连接凸包的坐标点
//依次连接随机点,绘制凸包
for (int i = 0; i < hullcout; i++)
{
Point point = points[hull[i]];
line(testImage, point0, point, Scalar(0, 255, 255), 1, 4);//连接线条颜色为黄色
point0 = point;
}
//输出
imshow("凸包绘制检测", testImage);
char key;
key = (char)waitKey();
if (key == 27 || key == 'q' || key == 'Q')//按下ESC 或q 或 Q 退出
break;
}
return 0;
}
综合示例
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//宏定义的部分
#define WINDOW_NAME1 "【原始图窗口】"
#define WINDOW_NAME2 "【效果图窗口】"
//全局变量
Mat g_srcImage, g_grayImage;
int g_nThresh = 50l;
int g_maxThresh = 255;
RNG g_rng(12345);
Mat srcImage_copy = g_srcImage.clone();
Mat g_thresholdImage_output;
vector<vector<Point>> g_vContours;
vector<Vec4i> g_vHierarchy;
//全局函数定义
void on_ThreshChange(int, void *);
//main()函数
int main()
{
//加载源图像
g_srcImage = imread("1.jpg");
//将原图转换成灰度图并且进行模糊降噪处理
cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
blur(g_grayImage, g_grayImage, Size(3, 3));
//创建原图窗口
namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE);
imshow(WINDOW_NAME1, g_srcImage);
//创建滚动条
createTrackbar("阈值:", WINDOW_NAME1,&g_nThresh,g_maxThresh,on_ThreshChange);
on_ThreshChange(0, 0);//调用一次进行初始化
waitKey(0);
return 0;
}
//回调函数
void on_ThreshChange(int, void *)
{
//对图像进行二值化,控制阈值
threshold(g_grayImage, g_thresholdImage_output, g_nThresh, 255, THRESH_BINARY);
//寻找轮廓
findContours(g_thresholdImage_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
//遍历每个轮廓点,寻找凸包
vector<vector<Point> > hull(g_vContours.size());
for (unsigned int i = 0; i < g_vContours.size(); i++)
{
convexHull(Mat(g_vContours[i]), hull[i], false);
}
//绘出轮廓及其凸包
Mat drawing = Mat::zeros(g_thresholdImage_output.size(), CV_8UC3);
for (unsigned int i = 0; i < g_vContours.size(); i++)
{
Scalar color = Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255));
drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
}
//显示效果图
imshow(WINDOW_NAME2, drawing);
}