Opencv学习笔记-使用opencv计算轮廓凸包

代码很简单,废话少说,let's go!

// !!注意:不要混用Opencv中分别为c和c++的数据类型

#include "stdafx.h"
#include <opencv.hpp>
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
        Mat *img_01 = new Mat(400, 400, CV_8UC3); 
	Mat *img_02 = new Mat(400, 400, CV_8UC3);
	*img_01 = Scalar::all(0);
	*img_02 = Scalar::all(0);
	// 轮廓点组成的数组
	vector<Point> points_01,points_02;


	// 给轮廓组赋值
	points_01.push_back(Point(10, 10));points_01.push_back(Point(10,390));
	points_01.push_back(Point(390, 390));points_01.push_back(Point(150, 250));
	points_02.push_back(Point(10, 10));points_02.push_back(Point(10,390));
	points_02.push_back(Point(390, 390));points_02.push_back(Point(250, 150));

	vector<int> hull_01,hull_02;
	// 计算凸包
	convexHull(points_01, hull_01, true);
	convexHull(points_02, hull_02, true);

	// 绘制轮廓
	for(int i=0;i < 4;++i)
	{
		circle(*img_01, points_01[i], 3, Scalar(0,255,255), CV_FILLED, CV_AA);
		circle(*img_02, points_02[i], 3, Scalar(0,255,255), CV_FILLED, CV_AA);
	}
	// 绘制凸包轮廓
	CvPoint poi_01 = points_01[hull_01[hull_01.size()-1]];
	for(int i=0;i < hull_01.size();++i)
	{
		line(*img_01, poi_01, points_01[i], Scalar(255,255,0), 1, CV_AA);
		poi_01 = points_01[i];
	}
	CvPoint poi_02 = points_02[hull_02[hull_02.size()-1]];
	for(int i=0;i < hull_02.size();++i)
	{
		line(*img_02, poi_02, points_02[i], Scalar(255,255,0), 1, CV_AA);
		poi_02 = points_02[i];
	}

	cout<<"img_01 是否是凸包:"<<isContourConvex(points_01)<<endl;
	cout<<"img_02 是否是凸包:"<<isContourConvex(points_02)<<endl;

	imshow("img_01 的轮廓和凸包:", *img_01);
	imshow("img_02 的轮廓和凸包:", *img_02);
	cvWaitKey();
	return 0;
}


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值