c++ opencv随机画不同颜色的线条

c++ opencv随机画不同颜色的线条

#include 
#include 
#include 
#include 
#include 
#include 
using namespace cv;
using namespace std;

#define w 800

#define  NUMBER   1000

#define  DELAY 5
/**
 * https://docs.opencv.org/4.6.0/df/d61/tutorial_random_generator_and_text.html
 *
 * cmake  .
 * make
 * ./a
 */

Scalar random_color(RNG rng) {
	int r = rng.uniform(0, 266);
	int g = rng.uniform(0, 266);
	int b = rng.uniform(0, 266);
	return Scalar(g, b, r);
}

int random_lines(Mat image, char *window_name, RNG rng) {
	const int lineType = 8;
	Point pt1, pt2;
	for (int i = 0; i < NUMBER; i++) {
		pt1.x = rng.uniform(0, w);
		pt1.y = rng.uniform(0, w);
		pt2.x = rng.uniform(0, w);
		pt2.y = rng.uniform(0, w);
		line(image, pt1, pt2, random_color(rng), rng.uniform(1, 10), lineType);
		imshow(window_name, image);
		if (waitKey( DELAY) >= 0) {
			return -1;
		}
	}
	return 0;
}

int main(int argc, char **argv) {
	RNG rng(0xFFFFFFFF);
	Mat img = Mat::zeros( w, w, CV_8UC3);
	namedWindow("opencv", WINDOW_FREERATIO);
	random_lines(img, "11111", rng);
	int k = waitKey(0);
	return 0;
}

你可能感兴趣的:(opencv,计算机视觉,opencv)