Linuxkiss
    首页 Linux C/C++ C++面试 Qt答疑 Qml中文手册 Qt CMake Python 工具
Linuxkiss
www.linuxkiss.com 你可以精通一门IT技术
  1. 首页
  2. openCv
  3. 正文

opencv系列29--凸包 [ api:convexHull ]

2018年04月01日 13点热度 0人点赞

概念介绍

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

正式定义

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

检测算法

- Graham扫描法

Graham扫描算法

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

步骤

1:首先把图像从RGB转为灰度

2:然后再转为二值图像

3:在通过发现轮廓得到候选点

4:凸包API调用

5:绘制显示

API说明cv::convexHull

convexHull(

InputArray points,// 输入候选点,来自findContours

OutputArray hull,// 凸包

bool clockwise,// default true, 顺时针方向

bool returnPoints // true 表示返回点个数,如果第二个参数是vector<Point>则自动忽略

)

示例代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace std;
using namespace cv;
Mat src, src_gray, dst;
int threshold_value = 100;
int threshold_max = 255;
const char* output_win = "convex hull demo";
void Threshold_Callback(int, void*);
RNG rng(12345);
int main(int argc, char** argv) {
        src = imread("D:/linuxkiss/images/leo.png");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	const char* input_win = "input image";
	namedWindow(input_win, CV_WINDOW_AUTOSIZE);
	namedWindow(output_win, CV_WINDOW_NORMAL);
	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++) {
                //------api调用------
		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(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;
}

 

标签: openCv
最后更新:2020年08月26日

Leo

保持饥渴的专注,追求最佳的品质

点赞
< 上一篇
下一篇 >
关注公众号

日历
2021年4月
一 二 三 四 五 六 日
« 2月    
 1234
567891011
12131415161718
19202122232425
2627282930  
最新 热点 随机
最新 热点 随机
windows中出现"无法解析的外部符号"到底是什么原因 Qt5中lambda表达式用法,非常实用 warning: class 'InterFace' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator 无法解析的外部符号 "public: static struct QMetaObject const Windows下Qt代码出现的错误总结 QT Creator如何在创建项目的时候,头文件和cpp文件的首字母默认大写
指针常量与常量指针的区别 qmake中的替换函数clean_path(path) QML共享的JavaScript资源(库)Shared JavaScript Resources (Libraries) QML简单属性动画 【C/C++,QT】面试大全六 opencv系列4--图像操作
标签聚合
openCv qmake qml中文手册 qml中文文档 Linux Qt C++ C/C++面试

COPYRIGHT © 2015-2021 Linuxkiss. ALL RIGHTS RESERVED.

苏ICP备12059464号-2