效果:

或者灰度,cell大小可调

代码:

#include <opencv2\opencv.hpp>
#include <Windows.h> struct parameters
{
parameters()
{
size=cv::Size(640,480);
fps=25;
nF=50;
save="";
crossSize= cv::Size(15, 5);
crossColor = 0;
show=true;
seed=cv::getTickCount();
color=false;
cellSize = cv::Size(5, 5);
} cv::Size size;
int fps;
int nF;
std::string save;
cv::Size crossSize;
uchar crossColor;
bool show;
int64 seed;
bool color;
cv::Size cellSize; void setParas(
cv::Size _size,
int _fps,
int _nF,
std::string _save,
cv::Size _crossSize,
uchar _crossColor,
bool _show,
int64 _seed,
bool _color,
cv::Size _cellSize)
{
size = _size ;
fps = _fps ;
nF = _nF ;
save = _save ;
crossSize = _crossSize ;
crossColor = _crossColor ;
show = _show ;
seed = _seed ;
color = _color ;
cellSize = _cellSize ; } void showParas()
{
std::cout << "size: " << size << std::endl;
std::cout << "fps: " << fps << std::endl;
std::cout << "nF: " << nF << std::endl;
std::cout << "save: " << save << std::endl;
std::cout << "crossSize: " << crossSize << std::endl;
std::cout << "crossColor: " << crossColor << std::endl;
std::cout << "show: " << (show? "TRUE" : "FALSE") << std::endl;
std::cout << "seed: " << seed << std::endl;
std::cout << "color: " << color << std::endl;
std::cout << "cellSize: " << cellSize << std::endl; } }; void help()
{
std::cout << "* -help : .\n"
<< " randomVideo.exe -help | to see how to use. \n"
<< " randomVideo.exe -size=(640,580) | to set video size. \n"
<< " randomVideo.exe -fps=25 | to set video fps(default is 25). \n"
<< " randomVideo.exe -nF=50 | to set number of frames. "
<< "If fps is 25, 50 nF means the video will last 2 seconds (50/25). \n" << " randomVideo.exe -save=example1 | "
<< "to set video name, only *.avi support, "
<< "you do not need input \".avi\". "
<< "If empty, the default name is local-system-time.avi, your previous files are safe. \n" << " randomVideo.exe -crossSize=(15,5) | "
<< "to set the size of cross."
<< " (height, width) you can see the showing window. \n" << " randomVideo.exe -crossColor=0 | Recommd not to set it. "
<< "If = 0, black(default); if = 255, white.\n\n" << " randomVideo.exe -show=TRUE | Recommd to be TRUE. "
<<"If FALSE, the window to show the video is stopped. \n" << " randomVideo.exe -seed=15 | Recommd not to set it. "
<<"If empty, the local time will be used, then,"
<<" you will get different video each time. \n\n" << " { NEW } randomVideo.exe -color=TRUE | If FALSE, the video is gray. \n" << " { NEW } randomVideo.exe -cellSize=(2,3) | To control the size of pixel size. "
<< "If cellSize=(2,3), the image width will be seperated into 2 parts, "
<< "the image height will be seperated into 3 parts. In defalut mode pixel size as 1 will be used!"
<< "In order to get exact pixel size, you need to calculate it by yourself. \n\n" << "example:\n"
<< " randomVideo.exe -size=(640,580) -fps=25 -nF=50 -crossSize=(15,5) -color=1 -cellSize=(5,5)\n"
<< " The other parameters using default is fine. \n" << std::endl;
} void getParas(int argn, char** argv, parameters & _parameters)
{
if (argn <= 1)
{
std::cout << "Using default parameter to generate video.\n"
<< "How to use? Please check the usage by input \" randomVideo.exe -help\" \n"
<< std::endl;
help();
exit(0);
}
else
{
std::map<std::string, std::string> table;
for (int i = 1; i<argn; ++i)
{
std::string sub(argv[i]);
std::string name = sub.substr(1, sub.find("=")-1);
table.insert({name, sub.substr(sub.find("=")+1, sub.length()-sub.find("=")-1)});
} if (table.count("help") == 1)
{
help();
exit(0);
} if (table.count("size") == 1)
{
cv::Size _size(atoi(table["size"].substr(1, table["size"].find(",")-1).c_str()),
atoi(table["size"].substr(
table["size"].find(",")+1, table["size"].length()-table["size"].find(",")-2
).c_str()));
_parameters.size = _size;
}
else
_parameters.size = cv::Size(640, 480); if (table.count("crossSize") == 1)
{
cv::Size _crossSize(atoi(table["crossSize"].substr(1, table["crossSize"].find(",") - 1).c_str()),
atoi(table["crossSize"].substr(
table["crossSize"].find(",") + 1, table["crossSize"].length() - table["crossSize"].find(",") - 2
).c_str()));
_parameters.crossSize = _crossSize;
}
else
_parameters.crossSize = cv::Size(15, 5); if (table.count("crossColor") == 1)
_parameters.crossColor = atoi(table["crossColor"].c_str());
else
_parameters.crossColor = 0; if (table.count("fps") == 1)
_parameters.fps = atoi(table["fps"].c_str());
else
_parameters.fps = 25; if (table.count("nF") == 1)
_parameters.nF = atoi(table["nF"].c_str());
else
_parameters.nF = 50; if (table.count("save") == 1)
_parameters.save = table["save"] + ".avi";
else
{
SYSTEMTIME sys;
GetLocalTime(&sys); char fname[100] = {};
sprintf_s(fname, 100, "%4d-%02d-%02d_%02d_%02d_%02d.avi", \
sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond); _parameters.save = fname; } if (table.count("show") == 1)
_parameters.show = (table["show"] == "TRUE");
else
_parameters.show = true; if (table.count("seed") == 1)
_parameters.seed = atoi(table["seed"].c_str());
else
{
SYSTEMTIME sys;
GetLocalTime(&sys);
_parameters.seed = sys.wYear + sys.wMonth + sys.wDay + sys.wHour + sys.wMinute + sys.wSecond;
} /*if (table.count("color") == 1)
{
cv::Scalar _color(
atoi(table["color"].substr(1, table["color"].find(",") - 1).c_str()),
atoi(table["color"].substr(table["color"].find(",") + 1,
table["color"].find_last_of(",") - table["color"].find(",") - 2).c_str()),
atoi(table["color"].substr(table["color"].find_last_of(",") + 1,
table["color"].find_last_of(")") - table["color"].find_last_of(",") - 1).c_str()));
_parameters.color = _color;
}
else
_parameters.size = cv::Size(640, 480);*/ if (table.count("color") == 1)
_parameters.color = (table["color"] == "TRUE");
else
_parameters.color = true; if (table.count("cellSize") == 1)
{
cv::Size _cellSize(atoi(table["cellSize"].substr(1, table["cellSize"].find(",") - 1).c_str()),
atoi(table["cellSize"].substr(
table["cellSize"].find(",") + 1, table["cellSize"].length() - table["cellSize"].find(",") - 2
).c_str()));
_parameters.cellSize = _cellSize;
}
else
_parameters.cellSize = cv::Size(2, 2); } _parameters.showParas(); } void randMatrix(cv::Mat & img, int64 seed, cv::Size cell=cv::Size(0,0))
{
cv::RNG rng(seed);
if (cell == cv::Size(0, 0) || cell.width > img.cols || cell.height > img.rows)
{
for (size_t j = 0; j < img.rows; ++j)
{
uchar* pdata = img.ptr<uchar>(j);
for (size_t i = 0; i < img.cols; ++i)
{
pdata[i] = rng.uniform(0, 255);
}
}
}
else
{
size_t stepX = img.cols / cell.width;
size_t stepY = img.rows / cell.height; for (size_t j = 0; j < img.rows; j+=stepY)
{
size_t lastY(j + stepY);
if (lastY >= img.rows)
lastY = img.rows - 1; for (size_t i = 0; i < img.cols; i += stepX)
{
size_t lastX(i+stepX);
if (lastX >= img.cols)
lastX = img.cols - 1; img.rowRange(j, lastY).colRange(i, lastX).setTo(rng.uniform(0,255));
}
}
}
} void addCross(cv::Mat & img, cv::Size cSzie, cv::Scalar scalar)
{
int x_center = (img.cols - 1) / 2, y_center = (img.rows - 1) / 2;
cv::Point
P1_tl(x_center - (cSzie.width - 1) / 2, y_center - (cSzie.height - 1) / 2),
P1_bd(x_center + (cSzie.width - 1) / 2, y_center + (cSzie.height - 1) / 2),
P2_tl(x_center - (cSzie.height - 1) / 2, y_center - (cSzie.width - 1) / 2),
P2_bd(x_center + (cSzie.height - 1) / 2, y_center + (cSzie.width - 1) / 2); cv::rectangle(img, P1_tl, P1_bd, scalar, -1);
cv::rectangle(img, P2_tl, P2_bd, scalar, -1);
} int main(int argn, char** argv)
{
parameters _pa;
getParas(argn, argv, _pa); cv::VideoWriter writer(_pa.save, CV_FOURCC('M', 'J','P','G'),
double(_pa.fps), _pa.size); cv::Mat frame0 = cv::Mat::zeros(_pa.size, CV_8UC1);
cv::Mat frame1 = cv::Mat::zeros(_pa.size, CV_8UC1);
cv::Mat frame2 = cv::Mat::zeros(_pa.size, CV_8UC1); for (int i=0; i<_pa.nF; ++i)
{
randMatrix(frame0, _pa.seed + i, _pa.cellSize);
addCross(frame0, _pa.crossSize, _pa.crossColor);
randMatrix(frame1, 0.3*(_pa.seed + i), _pa.cellSize);
addCross(frame1, _pa.crossSize, _pa.crossColor);
randMatrix(frame2, 0.7*(_pa.seed + i), _pa.cellSize);
addCross(frame2, _pa.crossSize, _pa.crossColor); cv::Mat f3;
if(_pa.color)
cv::merge(std::vector<cv::Mat>{frame0,frame1,frame2}, f3);
else
cv::merge(std::vector<cv::Mat>{frame0, frame0, frame0}, f3); writer << f3;
if (_pa.show)
cv::imshow("video", f3); cv::waitKey(10);
} return 0;
}

  

使用说明:

见help()

C++随机马赛克图程序的更多相关文章

  1. 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序

    三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...

  2. 300道随机四则运算小程序(java编写)

    这是由Java编写的300道随机四则运算小程序, 运算数范围为0~100(不包括100),运算结果保留两位小数. 程序代码: import java.util.*; class Yunsuan{ pu ...

  3. R in action读书笔记(15)第十一章 中级绘图 之二 折线图 相关图 马赛克图

    第十一章 中级绘图 本节用到的函数有: plot legend corrgram mosaic 11.2折线图 如果将散点图上的点从左往右连接起来,那么就会得到一个折线图. 创建散点图和折线图: &g ...

  4. python数据可视化(一)——绘制随机漫步图

    数据可视化指的是通过可视化表示来探索数据,它与数据挖掘紧密相关. python有一系列的可视化和分析工具,最流行的工具之一是matplotlib,它是一个数学绘图库. 实现绘制随机漫步图   利用ra ...

  5. js实现刷新页面出现随机背景图

    直接上代码: <script>         var bodyBgs = [];         bodyBgs[0] = "IMG/01.jpg";         ...

  6. java实现课堂随机点名小程序

    通过jdbc连接数据库实现读取学生花名册进行随机点名! ~jdbc连接mysql数据库  ||  注释部分代码可通过读取.txt文档实现显示学生信息 ~通过点击开始按钮实现界面中间标签不断更新学生信息 ...

  7. 随机点名小程序--- -JAVA版本

    话不多少,直接上代码 一个能够直接运行的随机点名的小程序,一个界面化的小程序.望广大网友多多支持! 1.创建一个随机点名的类 public class ProcessRandomName { JFra ...

  8. Matplotlib 随机漫步图

    import matplotlib.pyplot as plt from random import choice class Randomwalk(): def __init__(self,num_ ...

  9. Winform 随机抽奖小程序

    效果图: 主要代码: Form1.cs using System; using System.Drawing; using System.IO; using System.Runtime.Intero ...

随机推荐

  1. 【Ribbon篇四】Ribbon核心组件IRule(3)

    Ribbon在工作时分为两步: 先选择 EurekaServer,它优先选择在同一个区域内负载较少的Server: 再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址: 其中Rib ...

  2. 20K掌握的技术要点?

    银四指的是每年的三四月份都是人才招聘的高峰期,由于跟新年和春运紧接,到人才市场,人都是满的,所以称为 :伴随的四月则称为银四.每一年职场迎来“ 银四”.总结做完了,得失看清了,奖金拿到了,“算账”往后 ...

  3. [2019BUAA软工助教]助教学期总结

    [2019BUAA软工助教]助教学期总结 一.量化自评 线上 博客点评:https://www.cnblogs.com/ChildishChange/MyComments.html 共 106 条 博 ...

  4. 快速了解Electron:新一代基于Web的跨平台桌面技术

    本文引用了作者“ ConardLi”的<用JS开发跨平台桌面应用,从原理到实践>一文部分内容,原文链接:segmentfault.com/a/1190000019426512,感谢原作者的 ...

  5. redis之线程IO模型

    非阻塞 IO 当我们调用套接字的读写方法,默认它们是阻塞的,比如 read 方法要传递进去一个参数n,表示读取这么多字节后再返回,如果没有读够线程就会卡在那里,直到新的数据到来或者连接关闭了,read ...

  6. SQL Server in Docker - 还原数据库

    SQL Server in Docker 还原数据库 上一会演示了如果在Docker环境下安装SQL Server,这次我们来演示下如何还原一个数据库备份文件到数据库实例上. 使用winscp上传ba ...

  7. 查询SAP系统支持的ABAP版本

    7.52可以使用select 内表,但是怎么看版本呢? 如果有在开发中用到ABAP 7.4&7.5个版本的新语法时,需要考虑到系统支持的ABAP版本,那么要怎么查看呢? 其实这个和SAP的内核 ...

  8. Java面试复习(纯手打)

    1.面向对象和面向过程的区别: 面向过程比面向对象高.因为类调用时需要实例化,开销比较大,比较消耗资源,所以当性能是最重要的考量因素得时候,比如单片机.嵌入式开发.Linux/Unix等一般采用面向过 ...

  9. 第九届极客大挑战——怎么又是江师傅的秘密(java反序列化)

    这道题其实是考jsp和java的,我没学过jsp,java倒是有一点了解,但是刚拿到题的时候还是看不懂java代码里的内容,所以去简单学习了下jsp(jsp教程),按照教程里的步骤搭建了eclipse ...

  10. 【转】Git使用教程之远程仓库

    1.远程仓库 在了解之前,先注册github账号,由于你的本地Git仓库和github仓库之间的传输是通过SSH加密的,所以需要一点设置: 第一步:创建SSH Key.在用户主目录下,看看有没有.ss ...