寻找物体的凸包

1 寻找凸包:convexHull()函数
void convexHull(InputArray points,OutputArray hull,bool clockwise=false,bool returnPoints=true)
//例子

include "opencv2/imgproc/imgproc.hpp"

include "opencv2/highgui/highgui.hpp"

include

using namespace cv;
using namespace std;

//-----------------------------------【ShowHelpText( )函数】----------------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
static void ShowHelpText()
{
//输出欢迎信息和OpenCV版本
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV3版的第71个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION );
printf("\n\n ----------------------------------------------------------------------------\n");
//输出一些帮助信息
printf("\n\t欢迎来到【凸包检测】示例程序~\n\n");
printf("\n\t按键操作说明: \n\n"
"\t\t键盘按键【ESC】、【Q】、【q】- 退出程序\n\n"
"\t\t键盘按键任意键 - 重新生成随机点,并进行凸包检测\n" );

}

//--------------------------------------【main( )函数】-----------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main( )
{
//改变console字体颜色
system("color 1F");

//显示帮助文字
ShowHelpText();

//初始化变量和随机值
Mat image(600, 600, CV_8UC3);
RNG& rng = theRNG();

//循环,按下ESC,Q,q键程序退出,否则有键按下便一直更新
while(1)
{
	//参数初始化
	char key;//键值
	int count = (unsigned)rng%100 + 3;//随机生成点的数量
	vector<Point> points; //点值

	//随机生成点坐标
	for(int i = 0; i < count; i++ )
	{
		Point point;
		point.x = rng.uniform(image.cols/4, image.cols*3/4);
		point.y = rng.uniform(image.rows/4, image.rows*3/4);

		points.push_back(point);
	}

	//检测凸包
	vector<int> hull;
	convexHull(Mat(points), hull, true);

	//绘制出随机颜色的点
	image = Scalar::all(0);
	for(int i = 0; i < count; i++ )
		circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), FILLED, LINE_AA);

	//准备参数
	int hullcount = (int)hull.size();//凸包的边数
	Point point0 = points[hull[hullcount-1]];//连接凸包边的坐标点

	//绘制凸包的边
	for(int  i = 0; i < hullcount; i++ )
	{
		Point point = points[hull[i]];
		line(image, point0, point, Scalar(255, 255, 255), 2, LINE_AA);
		point0 = point;
	}

	//显示效果图
	imshow("凸包检测示例", image);

	//按下ESC,Q,或者q,程序退出
	key = (char)waitKey();
	if( key == 27 || key == 'q' || key == 'Q' ) 
		break;
}

return 0;

}
//寻找和绘制物体的凸包

include "opencv2/highgui/highgui.hpp"

include "opencv2/imgproc/imgproc.hpp"

include

using namespace cv;
using namespace std;

//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------

define WINDOW_NAME1 "【原始图窗口】" //为窗口标题定义的宏

define WINDOW_NAME2 "【效果图窗口】" //为窗口标题定义的宏

//-----------------------------------【全局变量声明部分】--------------------------------------
// 描述:全局变量的声明
//-----------------------------------------------------------------------------------------------
Mat g_srcImage; Mat g_grayImage;
int g_nThresh = 50;
int g_maxThresh = 255;
RNG g_rng(12345);
Mat srcImage_copy = g_srcImage.clone();
Mat g_thresholdImage_output;
vector<vector > g_vContours;
vector g_vHierarchy;

//-----------------------------------【全局函数声明部分】--------------------------------------
// 描述:全局函数的声明
//-----------------------------------------------------------------------------------------------
//static void ShowHelpText( );
void on_ThreshChange(int, void* );
//void ShowHelpText();

//-----------------------------------【main( )函数】------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main()
{
/system("color 3F");
ShowHelpText();
/

// 加载源图像
g_srcImage = imread( "1.jpg", 1 );

// 将原图转换成灰度图并进行模糊降
cvtColor( g_srcImage, g_grayImage, COLOR_BGR2GRAY );
blur( g_grayImage, g_grayImage, Size(3,3) );

// 创建原图窗口并显示
namedWindow( WINDOW_NAME1, WINDOW_AUTOSIZE );
imshow( WINDOW_NAME1, g_srcImage );

//创建滚动条
createTrackbar( " 阈值:", WINDOW_NAME1, &g_nThresh, g_maxThresh, on_ThreshChange );
on_ThreshChange( 0, 0 );//调用一次进行初始化

waitKey(0);
return(0);

}

//-----------------------------------【thresh_callback( )函数】----------------------------------
// 描述:回调函数
//----------------------------------------------------------------------------------------------
void on_ThreshChange(int, void* )
{
// 对图像进行二值化,控制阈值
threshold( g_grayImage, g_thresholdImage_output, g_nThresh, 255, THRESH_BINARY );

// 寻找轮廓
findContours( g_thresholdImage_output, g_vContours, g_vHierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );

// 遍历每个轮廓,寻找其凸包
vector<vector<Point> >hull( g_vContours.size() );
for( unsigned int i = 0; i < g_vContours.size(); i++ )
{  
	convexHull( Mat(g_vContours[i]), hull[i], false );
}

// 绘出轮廓及其凸包
Mat drawing = Mat::zeros( g_thresholdImage_output.size(), CV_8UC3 );
for(unsigned  int i = 0; i< g_vContours.size(); i++ )
{
	Scalar color = Scalar( g_rng.uniform(0, 255), g_rng.uniform(0,255), g_rng.uniform(0,255) );
	drawContours( drawing, g_vContours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
	drawContours( drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
}

// 显示效果图
imshow( WINDOW_NAME2, drawing );

}

//-----------------------------------【ShowHelpText( )函数】-----------------------------
// 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
/void ShowHelpText()
{
//输出欢迎信息和OpenCV版本
printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");
printf("\n\n\t\t\t此为本书OpenCV3版的第72个配套示例程序\n");
printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION );
printf("\n\n ----------------------------------------------------------------------------\n");
}
/

posted @ 2019-12-07 20:28  我输过但没服过  阅读(155)  评论(0编辑  收藏  举报