输出图片效果
代码 main.cpp
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
// 创建工程添加包含路径
// 并添加opencv相关链接库!!
using namespace cv;
using namespace std;
#define COUT_H cout << "\n[" __FILE__ ":" << __LINE__ << "]" __FUNCTION__ "()"
#define out_num 10
#define window_height 60
#define window_width 180
#define height_width (window_height + window_width)
#define colour 1 // 1彩色 0黑白
int random(int a, int b)
{
return a == b ? a : (int)(rand() % (b - a) + a);
}
Scalar randomColor(int l = 0, int r = 255)
{
#if colour
return Scalar(random(l, r), random(l, r), random(l, r));
#else // 黑白
int icolor = random(l, r);
return Scalar(icolor, icolor, icolor);
#endif
}
// 背景
// l r 颜色随机范围0-255
int Drawing_Random_Bg(Mat image, int l = 0, int r = 255)
{
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
image.at<Vec3b>(i, j)[0] = random(l, r);
#if colour
image.at<Vec3b>(i, j)[1] = random(l, r);
image.at<Vec3b>(i, j)[2] = random(l, r);
#else
image.at<Vec3b>(i, j)[1] = image.at<Vec3b>(i, j)[0];
image.at<Vec3b>(i, j)[2] = image.at<Vec3b>(i, j)[0];
#endif
}
}
return 0;
}
// 画线
int Drawing_Random_Lines(Mat image, int l = 0, int r = 255)
{
int lineType = 8;
int lineSizeMin = 1;
int lineSizeMax = 3;
// 纵向
int lineNum = random(5, 8); // 数量
for (int i = 0; i < lineNum; i++)
{
int lineSize = random(lineSizeMin, lineSizeMax); // 粗细
Point pt1(random(window_width / 10, window_width), 0);
Point pt2(random(0, window_width * 11 / 10), window_height);
Scalar scolor = randomColor(l, r);
line(image, pt1, pt2, scolor, lineSize, lineType);
}
// 横向
lineNum = random(5, 8); // 数量
for (int i = 0; i < lineNum; i++)
{
int lineSize = random(lineSizeMin, lineSizeMax); // 粗细
Point pt1(0, random(-window_height / 10, window_height * 11 / 10));
Point pt2(window_width, random(-window_height / 10, window_height * 11 / 10));
Scalar scolor = randomColor(l, r);
line(image, pt1, pt2, scolor, lineSize, lineType);
}
return 0;
}
string getLetterCode(int len = 4)
{
//string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string chars = "0123456789"
//"abcdefghjklmnpqrstuvwxyz"
"ABCDEFGHJKLMNPQRSTUVWXYZ";
string str = "";
for (int i = 0; i < len; i++)
{
int j = random(0, chars.length());
str.append(1, chars[j]);
}
return str;
}
int Drawing_Random_Text(Mat image, string txt, int type = 0, int l = 0, int r = 255)
{
int len = txt.length() + 1;
for (int i = 0; i < txt.length(); i++)
{
double x = window_width / len;
double y = window_height / 10;
double pos_x = x / 4 + random(x * i, x * i + x / 3);
double pos_y = random(y * 6, y * 9);
string tempChar = txt.substr(i, 1);
int font_face = FONT_HERSHEY_TRIPLEX;
double font_scale = random(15, 17);
int thickness = 2;
int lineType = LINE_AA;
if (type == 0)
{
font_scale /= 10;
}
else
{
font_scale = 0.6f;
pos_x = random(window_width / 100, window_width * 80 / 100);
pos_y = random(y * 3, y * 10);
int thickness = 1;
lineType = LINE_8;
}
Point origin(pos_x, pos_y);
Scalar scolor = randomColor(l, r);
putText(image, tempChar, origin, font_face, font_scale, scolor, thickness, lineType, 0);
}
return 0;
}
// 画干扰
int Drawing_Random_disturb(Mat image, int l = 0, int r = 255)
{
int per = 100;
// 线
int lineNum = random(4, 6); // 数量
for (int i = 0; i < lineNum; i++)
{
Point pt1(random(0, window_width * 70 / 100), random(0, window_height * 90 / 100));
int rdx = random(window_width * 20 / per, window_width * 50 / per);
int rdy = random(-window_width * 20 / per, window_width * 20 / per);
int lineSize = random(1, 3);
Point pt2(pt1.x + rdx, pt1.y + rdy);
Scalar scolor = randomColor(l, r);
line(image, pt1, pt2, scolor, lineSize, LINE_8);
}
return 0;
}
// 画边框
int Drawing_Random_frame(Mat image, int l = 0, int r = 255)
{
int siz = height_width * 1 / 100;
if (siz < 2)
{
siz = 2;
}
Scalar scolor = randomColor(l, r);
Point pt1(0, 0);
Point pt2(window_width - siz / 2, window_height - siz / 2);
rectangle(image, pt1, pt2, scolor, siz, 8);
return 0;
}
int main(void)
{
std::srand(time(0));
Mat img[out_num];
for (int i = 0; i < out_num; i++)
{
img[i] = Mat::zeros(window_height, window_width, CV_8UC3);
Drawing_Random_Bg(img[i], 220, 250); // 背景用淡一点的颜色
Drawing_Random_Lines(img[i], 180, 250);
string str = getLetterCode(5);
Drawing_Random_Text(img[i], str, 0, 10, 150);
Drawing_Random_disturb(img[i], 10, 150);
Drawing_Random_frame(img[i], 100, 150);
Drawing_Random_Text(img[i], getLetterCode(3), 1, 50, 200);
COUT_H << "str=" << str;
#if 1
imshow(str, img[i]);
waitKey(0);
#else
std::vector<int> vct = { IMWRITE_JPEG_QUALITY , 60 }; // 输出图片质量
imwrite("..\\..\\output\\" + str + ".jpg", img[i], vct); // 注意创建output文件夹!
#endif
}
system("pause");
return (0);
}