點擊上方藍字和「好玩的MATLAB」一起快樂玩耍吧!
好玩的matlab
帶你學會不一樣的matlab新玩法
請今天教大家怎麼用matlab隨機生成不同顏色的文字!clear,clc,close all;figure('color',[0,0,0],'position',get(0,'ScreenSize'))set(gcf, 'menubar', 'none', 'toolbar', 'none'); % 不顯示菜單欄和工具欄axis off;for k = 1 : 100%循環次數 h = text(rand, rand, ['\fontsize{',num2str(unifrnd(20, 50)),'}\fontname{Times New Roman}Who???'],... 'color',rand(1, 3), 'Rotation', 360 * rand); pause(0.1);%動態觀察變化過程延遲0.1秒執行下一條end之前也寫過關於C++版本的OpenCV的隨機生成文字的代碼,感興趣的小夥伴可以訪問下面連結:
https://idmatlab.blog.csdn.net/article/details/108691235難點問題:因為OpenCV的putText()不支持列印中文,我找到一個大佬的解決方案,他自定義一個putTextZH()函數,然後copy他的代碼配置環境就解決了。putText()連結:https://blog.csdn.net/wanggao_1990/article/details/52955056C++版本的OpenCV隨機生成文字源碼附上,感興趣的小夥伴可以去試試。
#include<opencv2/opencv.hpp>#include "putText.h"#include<iostream>using namespace cv;using namespace std;int main(int argc, char** argv) { Mat image = Mat::zeros(Size(1000, 800), CV_8UC3); int row = image.rows; int col = image.cols; int x1 = 0, y1 = 0; RNG rng; while (true) { x1 = (int)rng.uniform(0, col-1); y1 = (int)rng.uniform(0, row-1); putTextZH(image,"帥", Point(x1, y1),Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), rng.uniform(0,200),"宋體"); imshow("別人都誇我帥", image); char c = waitKey(100); if (c == 27) { break; } } waitKey(0); destroyAllWindows(); return 0;}哎呀!跑題了!俺是個matlab博主!今天介紹到此結束!溜了!溜了!參考文獻:
【1】https://idmatlab.blog.csdn.net/article/details/108691235
【2】https://gitee.com/iDmatlab/dynamic-graph---random-letters
【3】https://blog.csdn.net/weixinhum/article/details/84074594
【4】https://blog.csdn.net/wanggao_1990/article/details/52955056