###### tags: `東京威力 TEL` # I. 影像辨識 OpenCV ## <font color="orange"> 03. 畫圖</font> ### <font color="pink">3-1. 建立黑屏與畫直線</font> 開黑屏的寫法: ```c++= Mat black(500, 750, CV_8UC3, Scalar(0,0,0)); Mat black_2(500, 750, CV_8UC3, Scalar::all(0)); ``` 畫圖函式直接看怎麼用就可以了。 ><font color = "magenza">line (圖檔, 線起點, 線終點, 線顏色, 線粗度)</font> ```cpp= #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(){ Mat black(500, 750, CV_8UC3, Scalar(0,0,0)); // 建立黑屏 Point p1(10, 10); Point p2(200, 300); Scalar color = Scalar(255, 0, 0); line(black, p1, p2, color,3); // 畫線 while(1){ imshow("black", black); if(waitKey(1) == 27) break; } return 0 ; } ``` :::success CV_8UC3 的意思: CV = OpenCV;8U = 8 bits;C3 = 3 channels。 通常未處理過的照片都是 RGB ,它的型別 (type) 就會是 CV_8UC3。 ::: ### <font color="pink">3-2. 畫矩形</font> 在使用 `rectangle()` 繪製矩形前,要先建立一個 `Rect` 實例描述矩形特徵。 ><font color = "magenza"> rect(座標X, 座標Y, 向右長度, 向下高度)</font> ><font color = "magenza"> rectangle(圖檔, rect特徵, 顏色, 粗細)</font> ```cpp= #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(){ Mat black(500, 500, CV_8UC3, Scalar(0,0,0)); Rect rect = Rect(100, 200, 50, 80); //Rect實例:在座標(100,200),畫矩形,向右長50,向下長80。 Scalar color = Scalar(0, 255, 0); rectangle(black, rect, color, 1); while(1){ imshow("pic", black); if(waitKey(1)==27) break; } return 0 ; } ``` ### <font color="pink">3-3. 畫圓形</font> ><font color = "magenza"> circle(圖檔, 中心點, 半徑, 顏色, 粗細)</font> ```cpp= #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(){ Mat black(500, 500, CV_8UC3, Scalar(0,0,0)); Point center(250, 250); circle(black, center, 30, Scalar(0,0,255), 2); while(waitKey(1)!=27){ imshow("pic", black); } return 0; } ``` ### <font color="pink">3-4. 寫文字</font> ><font color = "magenza">putText(輸出圖片, 文字, 起始點, 字型, 字體大小, 顏色, 粗細, 線型, 顛倒)</font> ```cpp= #include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(){ Mat black(500, 500, CV_8UC3, Scalar::all(0)); putText(black, "Hello world!", Point(black.cols/3, black.rows/2), 3, 1, Scalar(0,255,0)); imshow("text with black background", black); waitKey(0); return 0; } ``` :yellow_heart: <font color = "yellow"> putText() 參數4 -- int fontFace </font> ```cpp= enum HersheyFonts { FONT_HERSHEY_SIMPLEX = 0, //!< normal size sans-serif font FONT_HERSHEY_PLAIN = 1, //!< small size sans-serif font FONT_HERSHEY_DUPLEX = 2, //!< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX) FONT_HERSHEY_COMPLEX = 3, //!< normal size serif font FONT_HERSHEY_TRIPLEX = 4, //!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX) FONT_HERSHEY_COMPLEX_SMALL = 5, //!< smaller version of FONT_HERSHEY_COMPLEX FONT_HERSHEY_SCRIPT_SIMPLEX = 6, //!< hand-writing style font FONT_HERSHEY_SCRIPT_COMPLEX = 7, //!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX FONT_ITALIC = 16 //!< flag for italic // fontFONT_ITALIC is not a font, but is a flag that can be combined with the other fonts to get italic text. }; ``` ![](https://hackmd.io/_uploads/rJWfWF6s9.png)