C + + random mosaic diagram Program

Source: Internet
Author: User
Tags local time scalar

Effect:

Or grayscale, the cell size is adjustable

Code:

#include <opencv2\opencv.hpp> #include <windows.h>struct parameters{parameters () {size=cv::size (640,480 ); fps=25;nf=50;save= ""; crosssize= cv::size (5); crosscolor = 0;show=true;seed=cv::gettickcount (); color=false; Cellsize = Cv::size (5, 5);} CV::SIZESIZE;INTFPS;INTNF;STD::STRINGSAVE;CV::SIZECROSSSIZE;UCHARCROSSCOLOR;BOOLSHOW;INT64SEED;BOOLCOLOR;CV:: Sizecellsize;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= _cross Size; 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 << "Sho W: "<< (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 the 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 means, the video would last 2 seconds (50/25).  \ n "<<" Randomvideo.exe-save=example1 | "<<" to set video name, only *.avi support, "<<" 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 is 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 would be used and then, "<<" You'll get different video each time.  \ n \ nthe "<<" {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 would be seperated into 2 parts, "<<" the image height would be seperate D into 3 parts. In Defalut mode pixel size as 1 would be used! " << "in order to getExact pixel size, you need to calculate it by yourself. \ n "<<" example:\n "<<" randomvideo.exe-size= (640,580)-fps=25-nf=50-crosssize= (15,5)-color=1-cellsiz e= (5,5) \ n "<<" the other parameters using the default is fine. \ n "<< Std::endl;} void Getparas (int argn, char** argv, Parameters & _parameters) {if (argn <= 1) {std::cout << "Using default par Ameter to generate video.\n "<<" How do I 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 (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, +, "%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+=s Tepy) {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.row S-1)/2;CV::P oint p1_tl (X_center-(cszie.width-1)/2, Y_center-(cszie.height-1)/2), P1_BD (X_center + (cszie.wi DTH-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&LT;CV::MAT&GT;{FRAME0, FRAME0, FRAME0}, F3), writer << f3;if (_pa.show) cv::imshow ("video", F3); CV:: Waitkey (10);} return 0;}

  

Instructions for use:

See Help ()

C + + random mosaic diagram Program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.