设为首页收藏本站 |天气与日历| 2025-04-19 星期六 13:34:00 乙巳(蛇)年 三月廿二 未时
     
切换到窄版

私人站点

 找回密码
 立即注册
搜索
查看: 567|回复: 0

凸包-Convex Hull

[复制链接]

954

主题

954

帖子

3875

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3875
发表于 2022-3-14 09:12:57 | 显示全部楼层 |阅读模式
概念介绍

  • 什么是凸包(Convex Hull),在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部
  • 正式定义:
  • 包含点集合S中所有点的最小凸多边形称为凸包





检测算法
- Graham扫描法

11111.png




检测算法
-convexHull
22.png 33.png





Graham扫描算法

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






API说明cv::convexHull

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




处理流程
  • 首先把图像从RGB转为灰度
  • 然后再转为二值图像
  • 在通过发现轮廓得到候选点
  • 凸包API调用
  • 绘制显示。



[C++] 纯文本查看 复制代码
/*
注意:
处理流程
首先把图像从RGB转为灰度
然后再转为二值图像
在通过发现轮廓得到候选点
凸包API调用
绘制显示。

*/
#include <opencv2/opencv.hpp>
#include <iostream>
#include<math.h>
using namespace std;
using namespace cv;

Mat src, dst, gray;
int threshold_val = 100;
int threshold_max = 255;
void threshold_call(int, void*);


int main(int argc, char** argv) {
	src = imread("1.jpg");
	if (!src.data) {
		printf("加载图片异常\n");
		return -1;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	imshow("input", src);
	//转灰度图
	cvtColor(src, gray, CV_BGR2GRAY);
	//模糊减低噪声 
	blur(gray, gray, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
	//创建拖动条
	createTrackbar("拖动条:", "input", &threshold_val, threshold_max, threshold_call);
	threshold_call(0, 0);



	waitKey(0);
	return 0;
}
void threshold_call(int, void*) {
	Mat bin_output;
	vector<vector<Point>> contours;
	//层次
	vector<Vec4i>hierachy;

	//二值化
	threshold(gray, bin_output, threshold_val, 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);
	for (size_t k = 0; k < contours.size(); k++)
	{
		//定义颜色
		Scalar color = Scalar(255, 255, 0);
		//绘制
		drawContours(dst, contours, k, color,2, LINE_AA, hierachy, 0, Point(0, 0));
		drawContours(dst, convexs, k, color, 2, LINE_AA, empty, 0, Point(0, 0));
	}
	imshow("output", dst);

}

x2.jpg x3.jpg
xx1.jpg








回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|编程站点 ( 冀ICP备2023028127号-2 )|友链申请|

GMT+8, 2025-4-19 13:34 , Processed in 0.100187 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表