• c++视觉----使用多边形包围轮廓


    外部矩形边界:boundingRect()函数

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace cv;
    #include 
    #include 
    using namespace cv; //包含cv命名空间
    #include 
    int main()
    {
    	//初始化变量和随机值
    	Mat image(600, 600, CV_8UC3);
    	RNG& rng = theRNG();
    	//循环, 按下ESC,Q,q键程序退出, 否则有键按下便一直更新
    	while(1)
    	{
    		//参数初始化
    		int count = rng.uniform(3, 103);//随机生成点的数量
    		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);
    		}
    		//对给定的 2D 点集,寻找最小面积的包围矩形
    		RotatedRect box = minAreaRect(Mat(points));
    		Point2f vertex[4];
    		box.points(vertex);
    		//绘制出随机颜色的点
    		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);
    		//绘制出最小面积的包围矩形
    		for (int i = 0; i < 4; i++)
    			line(image, vertex[i], vertex[(i + 1) % 4], Scalar(100, 200, 211), 2);
    		//显示窗口
    		imshow("矩形包围示例", image);
    		//按下ESC,Q,或者q, 程序退出
    		char key = (char)waitKey();
    		if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
    			break;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    在这里插入图片描述

    轮廓的圆形边界

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace cv;
    #include 
    #include 
    using namespace cv; //包含cv命名空间
    #include 
    int main()
    {
    	//初始化变量和随机值
    	Mat image(600, 600, CV_8UC3);
    	RNG& rng = theRNG();
    	//循环, 按下 ESC,Q,q键程序退出, 否则有键按下便一直更新
    	while (1)
    	{
    		//参数初始化
    		int count = rng.uniform(3, 103);//随机生成点的数量
    		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);
    		}
    		//对给定的 2D 点集,寻找最小面积的包围圆
    		Point2f center;
    		float radius = 0;
    		minEnclosingCircle(Mat(points), center, radius);
    		//绘制出随机颜色的点
    		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);
    		//绘制出最小面积的包围圆
    		circle(image, center, cvRound(radius), Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 2, LINE_AA);
    		//显示窗口
    		imshow("圆形包围示例", image);
    		//按下ESC,Q,或者q, 程序退出
    		char key = (char)waitKey();
    		if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
    			break;
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    在这里插入图片描述

    使用多边形包围轮廓

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace cv;
    #include 
    #include 
    using namespace cv; //包含cv命名空间
    #include 
    #define WINDOW_NAME1 "【原始图窗口】" //为窗口标题定义的宏
    #define WINDOW_NAME2 "【效果图窗口】" //为窗口标题定义的宏
    //【全局变量声明部分】------------------------1 / 描述: 全局变量的声明
    //
    Mat g_srcImage;
    Mat g_grayImage;
    int g_nThresh = 50;//阈值
    int g_nMaxThresh = 255;//阈值最大值
    RNG g_rng(12345);//随机数生成器
    //【全局函数声明部分】-------------------- -
    // 描述:全局函数的声明
    void on_ContoursChange(int, void*);
    static void ShowHelpText();
    //【main()函数】--------------
    // 描述: 控制台应用程序的入口函数,我们的程序从这里开始执行
    int main()
    {
    	//【0】改变 console字体颜色
    	system("color 1A");
    	//【1】载入3通道的原图像
    	g_srcImage = imread("1.jpg", 1);
    	if (!g_srcImage.data) { printf("读取图片错误,请确定目录下是否有 imread 函数指定的图片存在~! \n"); return false; }
    	//【2】得到原图的灰度图像并进行平滑
    	cvtColor(g_srcImage, g_grayImage, COLOR_BGR2GRAY);
    	blur(g_grayImage, g_grayImage, Size(3, 3));
    	//【3】创建原始图窗口并显示
    	namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE);
    	imshow(WINDOW_NAME1, g_srcImage);
    	//【4】设置滚动条并调用一次回调函数
    	createTrackbar(" 阈值:", WINDOW_NAME1, &g_nThresh, g_nMaxThresh, on_ContoursChange);
    	on_ContoursChange(0, 0);
    	waitKey(0);
    	return(0);
    }
    //---------------------------con_ContoursChange()函数】------------------
    // 描述: 回调函数
    //-
    void on_ContoursChange(int, void*)
    {
    	//定义一些参数
    	Mat threshold_output;
    	vector<vector<Point>> contours;
    	vector<Vec4i> hierarchy;
    	// 使用Threshold检测边缘
    	threshold(g_grayImage, threshold_output, g_nThresh, 255, THRESH_BINARY);
    	// 找出轮廓
    	findContours(threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
    	//多边形逼近轮廓 + 获取矩形和圆形边界框
    	vector<vector<Point> > contours_poly(contours.size());
    	vector<Rect> boundRect(contours.size());
    	vector<Point2f>center(contours.size());
    	vector<float>radius(contours.size());
    	//一个循环,遍历所有部分, 进行本程序最核心的操作
    	for (unsigned int i = 0; i < contours.size(); i++)
    	{
    		approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);//用指定精度逼近多边形曲线
    		boundRect[i] = boundingRect(Mat(contours_poly[i]));//计算点集的最外面(up-right)矩形边界
    		minEnclosingCircle(contours_poly[i], center[i], radius[i]);//对给定的 2D点集,寻找最小面积的包围圆形
    	}
    	//绘制多边形轮廓 + 包围的矩形框 + 圆形框
    	Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3); for (int unsigned i = 0; i < contours.size(); i++)
    	{
    		Scalar color = Scalar(g_rng.uniform(0, 255),
    			g_rng.uniform(0, 255), g_rng.uniform(0, 255));//随机设置颜色
    		drawContours(drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point());//绘制轮廓
    		rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);//绘制矩形
    		circle(drawing, center[i], (int)radius[i], color, 2, 8, 0);//绘制圆
    	}
    	// 显示效果图窗口
    	namedWindow(WINDOW_NAME2, WINDOW_AUTOSIZE);
    	imshow(WINDOW_NAME2, drawing);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    在这里插入图片描述

  • 相关阅读:
    SpringBoot整合微信扫码登录
    深度学习卫星遥感图像检测与识别 -opencv python 目标检测 计算机竞赛
    【学习笔记之数据结构】时间复杂度与空间复杂度
    codeforces:Codeforces Round #821 (Div. 2) 【思维 + 贪心分贡献的dp】
    Java版企业电子招标采购系统源码Spring Cloud + Spring Boot +二次开发+ MybatisPlus + Redis
    两级页表(单级页表存在的问题,两级页表的地址转换)
    1.4 系统环境变量
    设计模式-Bridge模式(桥模式)
    【校招VIP】java开源框架之netty
    Harmony 复杂图形自绘制
  • 原文地址:https://blog.csdn.net/qq_46107892/article/details/133771691