网站开发用什么笔记本,好的wordpress主题,长沙营销型网站建设费用,图书网站建设的规模策划书文章目录 概念Graham扫描算法convexHull 凸包函数示例 概念 什么是凸包(Convex Hull)#xff0c;在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部。 正式定义#xff1a; 包含点集合S中所有点的最小凸多边形称为凸包 Graham扫描算法 首先选择Y方向最低… 文章目录 概念Graham扫描算法convexHull 凸包函数示例 概念 什么是凸包(Convex Hull)在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部。 正式定义 包含点集合S中所有点的最小凸多边形称为凸包 Graham扫描算法 首先选择Y方向最低的点作为起始点p0从p0开始极坐标扫描依次添加p1….pn排序顺序是根据极坐标的角度大小逆时针方向对每个点pi来说如果添加pi点到凸包中导致一个左转向逆时针方法则添加该点到凸包 反之如果导致一个右转向顺时针方向删除该点从凸包中 convexHull 凸包函数
convexHull(
InputArray points,// 输入候选点来自findContours
OutputArray hull,// 凸包
bool clockwise,// default true, 顺时针方向
bool returnPoints// true 表示返回点个数如果第二个参数是vectorPoint则自动忽略示例
#include opencv2/opencv.hpp
#include iostream
#include math.husing namespace std;
using namespace cv;Mat src, src_gray, dst; // 定义原始图像、灰度图像和结果图像
int threshold_value 100; // 初始阈值设为100
int threshold_max 255; // 最大阈值为255
const char* output_win convex hull demo; // 定义输出窗口名称
RNG rng(12345); // 随机数生成器// 回调函数声明
void Threshold_Callback(int, void*);int main(int argc, char** argv) {src imread(D:/vcprojects/images/hand.png); // 读取图像if (!src.data) {printf(could not load image...\n);return -1;}const char* input_win input image;namedWindow(input_win); // 创建输入图像窗口namedWindow(output_win); // 创建输出图像窗口const char* trackbar_label Threshold : ; // 创建滑动条标题cvtColor(src, src_gray, CV_BGR2GRAY); // 将彩色图像转换为灰度图像blur(src_gray, src_gray, Size(3, 3), Point(-1, -1), BORDER_DEFAULT); // 对灰度图像进行模糊处理imshow(input_win, src_gray); // 在输入窗口中显示灰度图像createTrackbar(trackbar_label, output_win, threshold_value, threshold_max, Threshold_Callback); // 创建阈值滑动条Threshold_Callback(0, 0); // 初始化回调函数waitKey(0); // 等待按键return 0;
}void Threshold_Callback(int, void*) {Mat bin_output; // 二值化输出图像vectorvectorPoint contours; // 存储轮廓点集vectorVec4i hierachy; // 轮廓层级关系threshold(src_gray, bin_output, threshold_value, threshold_max, THRESH_BINARY); // 对灰度图像进行阈值处理findContours(bin_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); // 查找图像中的轮廓vectorvectorPoint convexs(contours.size()); // 存储凸包结果for (size_t i 0; i contours.size(); i) {convexHull(contours[i], convexs[i], false, true); // 计算每个轮廓的凸包}dst Mat::zeros(src.size(), CV_8UC3); // 创建与原始图像相同大小的空白图像vectorVec4i empty(0); // 空Vec4i用于绘制凸包for (size_t k 0; k contours.size(); k) {Scalar color Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); // 随机颜色drawContours(dst, contours, k, color, 2, LINE_8, hierachy, 0, Point(0, 0)); // 绘制轮廓drawContours(dst, convexs, k, color, 2, LINE_8, empty, 0, Point(0, 0)); // 绘制凸包}imshow(output_win, dst); // 在输出窗口中显示结果图像return;
}