opencv3返回外部矩形边界-boundingRect函数

#include<iostream>
#include<vector>
#include<opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
	Mat srcImage(Size(600, 600), CV_8UC3, Scalar(0));

	RNG &rng = theRNG();

	char key;
	while (1)
	{
		//随机生成一些点
		//首先就是随机生成点的总数量
		int g_nPointCount = rng.uniform(3, 30);
		//接下来就是随机生成一些点的坐标
		vector<Point> points;
		for (int i = 0; i < g_nPointCount; i++)
		{
			Point midPoint;

			midPoint.x = rng.uniform(srcImage.cols / 4, srcImage.cols * 3 / 4);
			midPoint.y = rng.uniform(srcImage.rows / 4, srcImage.rows * 3 / 4);

			points.push_back(midPoint);
		}

		//显示刚刚随机生成的那些点
		for (int i = 0; i < g_nPointCount; i++)
		{
			circle(srcImage, points[i], 0, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3);
		}

		//在生成的那些随机点中寻找最小包围矩形
		//rect变量中得到了矩形
		Rect rect = boundingRect(points);

		//根据得到的矩形  绘制矩形
		rectangle(srcImage, rect, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 3);

		imshow("【绘制结束后的图像】", srcImage);

		key = waitKey();
		if (key == 27)
			break;
		else
			srcImage = Scalar::all(0);
	}

	return 0;
}


用空格键来改变随机值


没有更多推荐了,返回首页