效果:

或者灰度,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);elsecv::merge(std::vector<cv::Mat>{frame0, frame0, frame0}, f3);writer << f3;if (_pa.show)cv::imshow("video", f3);cv::waitKey(10);}return 0;
}

  

使用说明:

见help()

转载于:https://www.cnblogs.com/alexYuin/p/9614107.html

C++随机马赛克图程序相关推荐

  1. 利用matplotlib绘制马赛克图的案例

    一.概念介绍 马赛克图(Mosaic plot),基于列联表的数据排布,展现二维定性变量下的取值大小,我们可以将它想象成三维柱状图的俯视,颜色差异或面积大小常被用于区分每个块的赋值大小. · 什么是列 ...

  2. R语言可视化:散点图、散点图和折线图(line charts)、3D散点图、旋转3D散点图、气泡图、corrgram包可视化相关性矩阵、马赛克图( Mosaic plots)、hexbin、密度图

    R语言可视化:散点图.散点图和折线图(line charts).3D散点图.旋转3D散点图.气泡图.corrgram包可视化相关性矩阵.马赛克图( Mosaic plots).hexbin.密度图 目 ...

  3. R语言使用两个分类变量创建双向表(Two Way Table、两个分类变量的频率表)实战: 矩阵的双向表、dataframe的双向表、条形图和马赛克图来可视化频率表

    R语言使用两个分类变量创建双向表(Two Way Table.两个分类变量的频率表)实战: 矩阵的双向表.dataframe的双向表.条形图和马赛克图来可视化频率表 目录

  4. 风机桨叶故障诊断(四) 正负样本准备——从图像中随机扣图

    风机桨叶故障诊断(四)                                             正负样本准备--从图像中随机扣图 在之前的工作中,我们已经训练了一个400×25×2的三 ...

  5. 简洁的个人导航主页API网站源码 随机背景图

    简介: 简洁个人导航网站主页,附带几个可用的API接口,随机背景图,带简单后台 网盘下载地址: http://kekewangLuo.cc/sJo8z7Bam4p0 图片:

  6. python绘制两个离散变量关系图——马赛克图

    一个比较好看的图如下: 我们可以较为直观的看到两个离散变量之间的关系,python绘制方法也比较简单 可以使用statsmodels.graphics.mosaicplot.mosaic 文档位置:h ...

  7. graphics | 基础绘图系统(十)——星形图、四瓣图、马赛克图

    插播消息:最初建的交流群已经无法使用了,新建群的加入方式见次条推送. 本篇是"基础绘图系统"系列推文的最后一篇,我们来介绍三种不常见的图形,对应的绘图函数分别如下: stars() ...

  8. 【Python-3.5】绘制随机漫步图

    代码中我们定义两个模型,一个是RandomWalk.py模型,用于随机的选择前进方向.此模型中的RandomWalk类包含两个方法,一个是__init__(),一个是fill_walk(),后者是计算 ...

  9. Python中使用random随机函数与Matplotlib库绘制随机漫步图

    Python中使用random随机函数与Matplotlib库绘制随机漫步图 一.概述 随机现象在我们的生活中并不少见,我们常见的一次抛硬币的正反面,股票的走势等等,都是随机事件.这些不确定性事件给我 ...

最新文章

  1. jdbcpingquery mysql_JDBC - liuping - 博客园
  2. rmdir删除文件报Permission denied in 错误
  3. s5pv210 uboot-2012-10移植(三) 之支持SPL
  4. Springboot整合RabbitMq-用心看完这一篇就够了(最新)
  5. NBU网络备份大全之oracle系统冷备份
  6. 记linux_centOS安装as86过程
  7. 窗口缩小 怎么让定位的盒子不动_一次解决你的图像尺寸和定位问题
  8. 数据分析与挖掘建模实战003:单因子探索分析与可视化001数据案例介绍
  9. 第十五节:SpringBoot使用JPA访问数据库
  10. 极简代码(七)—— SNR
  11. 对列 、生产者与消费者
  12. 网络流(最大流) CQOI 2015 BZOJ 3931 网络吞吐量
  13. Linux链表list_head/hlist_head/hlist_nulls_head的并发性
  14. 微信小程序-“授权失败”场景的优雅处理
  15. 分数加减法混合计算机,1649.新人教版五年级数学下册第三课 分数加减混合运算(教案)(教学设计)(国家级一等奖、适合公开课).doc...
  16. 常用汉字库(约7千个)
  17. FFmpeg 异常:height not divisible by 2
  18. 基于朴素贝叶斯实现文本分类
  19. 作为篮球迷,我有一些话想说
  20. 查看loam的三维点云地图

热门文章

  1. SoftReference的使用
  2. 一文读懂 Web3:互联网发展的新时代还是骗局?
  3. javaweb——e家园项目
  4. XFF和referer
  5. directx11开发中的错误解决
  6. OJ笔记 18718 航行
  7. Intel Quartus Prime 安装激活 IP License大全 3339377509@qq.com
  8. oracle dcd设置,Oracle DCD 测试
  9. WinRAR 3.71 官方简体中文正式版
  10. 气凝胶隔热材料超低导热系数测试中存在的问题及解决方案