27 OpenCV 凸包

文章目录

概念

什么是凸包(Convex Hull),在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部。

正式定义:

包含点集合S中所有点的最小凸多边形称为凸包

Graham扫描算法

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

convexHull 凸包函数

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

示例

c 复制代码
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using 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; // 二值化输出图像
    vector<vector<Point>> contours; // 存储轮廓点集
    vector<Vec4i> 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)); // 查找图像中的轮廓

    vector<vector<Point>> 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); // 创建与原始图像相同大小的空白图像
    vector<Vec4i> 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;
}
相关推荐
张人玉1 小时前
人工智能——猴子摘香蕉问题
人工智能
草莓屁屁我不吃1 小时前
Siri因ChatGPT-4o升级:我们的个人信息还安全吗?
人工智能·安全·chatgpt·chatgpt-4o
小言从不摸鱼1 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
AI科研视界1 小时前
ChatGPT+2:修订初始AI安全性和超级智能假设
人工智能·chatgpt
霍格沃兹测试开发学社测试人社区1 小时前
人工智能 | 基于ChatGPT开发人工智能服务平台
软件测试·人工智能·测试开发·chatgpt
小R资源2 小时前
3款免费的GPT类工具
人工智能·gpt·chatgpt·ai作画·ai模型·国内免费
artificiali5 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
酱香编程,风雨兼程5 小时前
深度学习——基础知识
人工智能·深度学习
Lossya5 小时前
【机器学习】参数学习的基本概念以及贝叶斯网络的参数学习和马尔可夫随机场的参数学习
人工智能·学习·机器学习·贝叶斯网络·马尔科夫随机场·参数学习
#include<菜鸡>6 小时前
动手学深度学习(pytorch土堆)-04torchvision中数据集的使用
人工智能·pytorch·深度学习