Random number Generation classes: RNG and text rendering
1#include <opencv2\opencv.hpp>2#include <iostream>3#include <string>4 5 #pragmaComment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")6 7 #defineNumber 208 9 using namespacestd;Ten using namespaceCV; One A voidShow (std::stringname,mat img) - { - Namedwindow (name, cv_window_autosize); the imshow (Name, IMG); - } - - StaticScalar Randomcolor (RNG &rng) + { - returnScalar (Rng.uniform (0,255), Rng.uniform (0,255), Rng.uniform (0,255)); + } A at voidDrawing_random_lines (Mat img,Char*win_name, rng&rng) - { - intLinetype =8; - intW = -; - Point pt1, pt2; - intX_1 =0, x_2 =W; in - for(inti =0; I < number; ++i) to { +pt1.x =Rng.uniform (x_1, x_2); -PT1.Y =Rng.uniform (x_1, x_2); thept2.x =Rng.uniform (x_1, x_2); *PT2.Y =Rng.uniform (x_1, x_2); $ Panax NotoginsengLine (IMG, PT1, pt2, Randomcolor (RNG), Rng.uniform (1,Ten),8); - Show (Win_name, IMG); the } + } A the voidDisplay_random_text (Mat img,Char*win_name, RNG &rng) + { - intLinetype =8; $ intW = -; $ intX1 =0, x2 =W; - - for(inti =0; I < number; ++i) the { - Point org;Wuyiorg.x =rng.uniform (x1, x2); theORG.Y =rng.uniform (x1, x2); - WuPuttext (IMG,"Testing Text", org, rng.uniform (0,8), Rng.uniform (0, -)*0.05+0.1, Randomcolor (RNG), Rng.uniform (1,Ten), linetype); - About Show (Win_name, IMG); $ } - } - - voidDisplay_big_end (Mat img,Char*Win_name, rng rng) A { + intW = -; theSize textSize = GetTextSize ("OpenCV Forever", Cv_font_hershey_complex,3,5,0); -Point org ((w-textsize.width)/2, (w + textsize.height)/2); $ intLinetype =8; the the Mat Img2; the the for(inti =0; I < number; ++i) - { inImg2 = img-Scalar::all (i); thePuttext (Img2,"OpenCV Forever", org, Cv_font_hershey_complex,3, Scalar (i, I,255),5, Linetype); the About Show (Win_name, img2); the } the } the + intMain () - { the intW = -;BayiRNG RNG (0xFFFFFFFF); the Char*win_name ="Drawing"; theMat img =Mat::zeros (W, W, CV_8UC3); - - drawing_random_lines (IMG, win_name, rng); the //Drawing_random_rectangle (IMG, win_name, rng); the //drawing_random_ellipse (IMG, win_name, rng); the //drawing_random_polylines (IMG, win_name, rng); the //Drawing_random_filledpolygon (IMG, win_name, rng); - //drawing_random_circle (IMG, win_name, rng); the Display_random_text (IMG, win_name, rng); the display_big_end (IMG, win_name, rng); the 94 Waitkey (); the return 0; the}
The documentation is explained and will not be mentioned.
The result is:
This explains the Puttext function:
Excerpted from CORE.HPP
1 enum2 {3Font_hershey_simplex =0,4Font_hershey_plain =1,5Font_hershey_duplex =2,6Font_hershey_complex =3,7Font_hershey_triplex =4,8Font_hershey_complex_small =5,9Font_hershey_script_simplex =6,TenFont_hershey_script_complex =7, OneFont_italic = - A }; - - //! Renders text string in the image theCv_exports_wvoidPuttext (mat& img,Const string&text, point org, - intFontface,DoubleFontscale, Scalar color, - intthickness=1,intLinetype=8, - BOOLbottomleftorigin=false ); + - //! Returns bounding box of the text string +Cv_exports_w Size GetTextSize (Const string& Text,intFontface, A DoubleFontscale,intthickness, atCv_outint* BaseLine);
int Fontface accepts the parameters of the font, so there is a definition of the above enum body.
int Fontscale accepts the scaling factor of a font;
The last Boolean parameter may be to determine whether to flip up or down;
Above.
OPENCV Official Document Learning record (5)