长短随机,颜色随机,方向随机
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
void RandLine();
Mat bgimg;
int main(int argc, char** argv) {
bgimg = imread("d:/gg.jpg");
if (!bgimg.data) {
cout << "not load..." << endl;
}
RandLine();
waitKey(0);
return 0;
}
void RandLine() {
RNG rng(12345);
Point pt1;
Point pt2;
Mat bg = Mat::zeros(bgimg.size(), bgimg.type());
namedWindow("随机线条", WINDOW_AUTOSIZE);
for (int i = 0; i < 100; i++) {
pt1.x = rng.uniform(0, bgimg.cols);
pt1.y = rng.uniform(0, bgimg.cols);
pt2.x = rng.uniform(0, bgimg.rows);
pt2.y = rng.uniform(0, bgimg.rows);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
if (waitKey(50) > 0) {
break;
}
line(bg, pt1, pt2, color, 1, LINE_8);
imshow("随机线条", bg);
}
}