opencv笔记14-随机数与随机绘制

随机数与随机绘制

#include
#include

using namespace std;
using namespace cv;

//随机数与随机颜色
void random_demo()
{
	Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);//画布
	RNG rng(12345);
	int width = canvas.cols;//宽度
	int height = canvas.rows;//高度
	while (true)
	{
		int c = waitKey(50);
		if (c == 27)//esc键
		{
			break;
		}
		//随机数
		int x1 = rng.uniform(0, width);
		int y1 = rng.uniform(0, height);
		int x2 = rng.uniform(0, width);
		int y2 = rng.uniform(0, height);

		//随机颜色
		int b = rng.uniform(0, 255);
		int g = rng.uniform(0, 255);
		int r = rng.uniform(0, 255);
		//canvas = Scalar(0, 0, 0);//只绘制一条
		//绘制
		line(canvas, Point(x1, y1), Point(x2, y2), Scalar(b,g,r), 1, LINE_AA, 0);

		imshow("随机绘制", canvas);
	}

}
int main()
{
	string path= "C:\\Users\\四明\\Pictures\\QQ图片20200608155343.jpg";
	Mat img = imread(path);
	if (img.empty())
	{
		cout << "图片加载失败!" << endl;
		return -1;
	}
	namedWindow("image", WINDOW_FREERATIO);
	imshow("image", img);
	random_demo();
	waitKey(0);
	destroyAllWindows();
	return 0;
}

效果图
opencv笔记14-随机数与随机绘制_第1张图片

你可能感兴趣的:(opencv笔记,笔记,c++,opencv)