效果:

或者灰度,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. Django cache (缓存)

    五种配置 1.开发调试 # 此为开始调试用,实际内部不做任何操作 # 配置: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backend ...

  2. 【转】Struts2 表单验证与验证框架

    版权声明:好笔头不如烂记性 https://blog.csdn.net/zsbgood/article/details/81114038 表单数据验证是很常见的功能,通常前端页面会有一次 js验证,但 ...

  3. [探究] 用舞蹈链(DLX)解决一类数独问题

    考虑精准覆盖问题的本质--我们把行看做决策,把列看做任务,那么其实质就是通过决策来完成任务. 那么我们来考虑数独问题的本质,对于一个\(n^2\cdot n^2\)的数独而言,他的目标函数有四个: 1 ...

  4. 解决python 缺少os.fspath

    在python3.6下运行pandas会报错缺少os.fspath 升级到python3.7 3.7 安装参考:https://www.cnblogs.com/jifeng/p/11221469.ht ...

  5. 【shell脚本】创建账户及删除账户,批量创建账户及批量删除账户===autoCreateUser.sh

    一.字符串运算符 二.创建账户 1.提示用户输入用户名和密码,脚本自动创建相应的账户及配置密码.如果用户不输入账户名,则提示必须输入账户名并退出脚本;如果用户不输入密码,则统一使用默认的 123456 ...

  6. 七、Spring之深入理解AOP源码

    Spring之深入理解AOP源码 ​ 在上一篇博文中,我们对AOP有了初步的了解,那么接下来我们就对AOP的实现原理进行深入的分析. ​ 在之前写的那个AOP示例代码当中有这样一个注解:@Enable ...

  7. ICP&TPS:最近邻

    经过了一段时间的研bai究gei...终于可以偷得几天闲了. 这里来补个档. 无论是ICP还是TPS,缺乏锚点的前提下.你总是要通过找另一个曲面的最近的点来实现你的work beimat:点数*3,f ...

  8. 数据竞争检查工具(TSan)

    https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/ThreadSanitizerC ...

  9. yii2.0的学习之旅(一)

    一. 通过composer安装yii2.0项目 *本文是根据您已经安装了composer (1)跳转到项目根目录 cd /xxxx/www (2)下载插件 composer global requir ...

  10. FastDFS图片服务器(分布式文件系统)学习。

    参考:https://blog.csdn.net/hiqingtian/article/details/79413471 https://blog.csdn.net/sinat_40399893/ar ...