1.8【OpenCV圖像處理】繪製形狀與文字

點Point()、顏色Scalar()、線line()、矩形rectangle()、橢圓ellipse()、圓circle()、多邊形fillPoly()、隨機數RNG、加文字putText()

1.8.1 使用點cv::Point與顏色cv::Scalar

(1)Point(點)表示二維2D平面上一個點(x, y),左上角為原點,x軸向左,y軸向下:

Point p;

p.x = 10;

p.y = 8;

Point p = Point(10,8);

Point p1 = Point(0, 30); //左上角為原點,x軸向左,y軸向下nPoint p2;np2.x = 720;np2.y = 380;n

(2)Scalar(顏色)表示四個元素的向量,通道顏色:

Scalar(a, b, c); // a = blue, b = green, c = red表示RGB三個通道(不聲明也可以)

Scalar color = Scalar(0, 0, 255); //紅色n

1.8.2 繪製線、矩形、園、橢圓等基本幾何形狀

(1)畫線 cv::line() 線類型 LINE_4LINE_8LINE_AA反鋸齒(要渲染)

/*繪製直線實現函數*/nvoid MyLines() { //函數實現nPoint p1 = Point(0, 30); //左上角為原點,x軸向左,y軸向下nPoint p2;n p2.x = 720;n p2.y = 380;nScalar color = Scalar(0, 0, 255); //紅nline(bgImage, p1, p2, color, 1, LINE_AA); //原圖,點1,點2,線顏色,線寬(值越大越寬),線類型n}n

(2)畫矩形 cv::rectangle()

/*繪製矩形實現函數 */nvoid MyRectangle() {nRect rect = Rect(200, 100, 300, 300); //矩形範圍,起始位置xy,寬,高nScalar color = Scalar(255, 0, 255); //粉紅nrectangle(bgImage, rect, color, 2, LINE_8); //原圖,矩陣範圍,線顏色,線寬,線類型n}n

(3)畫橢圓(弧) cv::ellipse()

/*繪製橢圓(弧)實現函數 */nvoid MyEllipse() {nScalar color = Scalar(255, 255, 0); //青nellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows/ 8),80 ,0 ,270 ,color, 2, LINE_8);n//原圖,橢圓中心,橢圓半徑(長軸和短軸),傾斜角度,開始角度,結束角度,線顏色,線寬,線類型n}n

(4)畫圓 cv::circle()

/*繪製圓實現函數 */nvoid MyCircle() {nScalar color = Scalar(0,255, 255); //黃nPoint center = Point(bgImage.cols / 2, bgImage.rows / 2); //圓心ncircle(bgImage, center, 50, color, 3, 8); //原圖,圓心,半徑長度,線顏色,線寬,線類型n}n

(5)畫多邊形填充 cv::fillPoly()

/*多邊形填充實現函數 */nvoid MyPolygon() {nPoint pts[1][6]; //多個點,二維數組n pts[0][0] = Point(100, 100); //5邊形,6個點,能形成封閉區域n pts[0][1] = Point(100, 200);n pts[0][2] = Point(150, 250);n pts[0][3] = Point(200, 200);n pts[0][4] = Point(200, 100);n pts[0][5] = Point(100, 100);nconst Point* ppts[] = { pts[0] }; //指針nint npt[] = { 6 }; //多少個nScalar color = Scalar(255, 0, 0); //白nfillPoly(bgImage, ppts, npt, 1, color, 8); //原圖,多個點指針,幾個點連線,線寬,線顏色,線類型n}n

1.8.3 隨機數生成cv::RNG

生成高斯隨機數gaussian (double sigma)

生成正態分布隨機數uniform (int a, int b)

畫隨機線:

/*繪製隨機線*/nvoid RandomLineDemo() {nRNG rng(12345); //隨機數生成函數nPoint pt1; // 線的位置兩點nPoint pt2;nMat bg = Mat::zeros(bgImage.size(), bgImage.type()); // 空白圖像全黑背景nfor (int i = 0; i < 100000; i++) {npt1.x = rng.uniform(0, bgImage.cols); //點隨機規格,生成正態分布隨機數,不超過範圍n pt2.x = rng.uniform(0, bgImage.cols);n pt1.y = rng.uniform(0, bgImage.rows);n pt2.y = rng.uniform(0, bgImage.rows);nScalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); //隨機顏色nif (waitKey(50) > 0) { //等待x ms,如果在此期間有按鍵按下,則立即結束並返回按下按鍵的ASCII碼,否則沒有按鍵返回 -1nbreak; // 跳出for循環n }n line(bg, pt1, pt2, color, 1, 8); // 劃線n imshow("random line demo",bg);n } n}n

1.8.4 繪製添加文字

putText函數 中設置fontFace(cv::HersheyFonts),

- fontFace, CV_FONT_HERSHEY_PLAIN 字體

- fontScale , 1.0, 2.0~ 8.0 字型大小

putText(bgImage, "Hello OpenCV", Point(250, 300), CV_FONT_HERSHEY_COMPLEX, 1.5, Scalar(255, 255, 255), 2, LINE_AA);n//原圖,字元串,中心,字體,字型大小,線顏色,線寬,線類型n


完整程序:

/*1.8 繪製形狀與文字*/n#include <opencv2/opencv.hpp>n#include <iostream> nusing namespace cv; //使用cv命名空間,下面的cv::line等的前綴可以不寫nMat bgImage;nconst char* drawdemo_win = "draw shapes and text demo";nvoid MyLines(); //函數聲明nvoid MyRectangle();nvoid MyEllipse();nvoid MyCircle();nvoid MyPolygon();nvoid RandomLineDemo();nnint main(int argc, char** argv) { //argc 表示命令行輸入參數的個數(以空白符分隔),argv中存儲了所有的命令行參數n bgImage = imread("E:/OpenCV/testimage/test5.jpg");nif (bgImage.empty()) {n printf("could not load image...n");nreturn -1;n }nn MyLines(); //函數調用n MyRectangle();n MyEllipse();n MyCircle();n MyPolygon();n RandomLineDemo();n putText(bgImage, "Hello OpenCV", Point(250, 300), CV_FONT_HERSHEY_COMPLEX, 1.5, Scalar(255, 255, 255), 2, LINE_AA);n//原圖,字元串,中心,字體,字型大小,線顏色,線寬,線類型nnn namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE);n imshow("test opencv setup", bgImage);n waitKey(0); //x=0無限等待下去,直到有按鍵按下nreturn 0;n}nn/*繪製直線*/nvoid MyLines() { //函數實現nPoint p1 = Point(0, 30); //左上角為原點,x軸向左,y軸向下nPoint p2;n p2.x = 720;n p2.y = 380;nScalar color = Scalar(0, 0, 255); //紅n line(bgImage, p1, p2, color, 1, LINE_AA); //原圖,點1,點2,線顏色,線寬(值越大越寬),線類型n}n/*繪製矩形*/nvoid MyRectangle() {nRect rect = Rect(200, 100, 300, 300); //矩形範圍,起始位置xy,寬,高nScalar color = Scalar(255, 0, 255); //粉紅n rectangle(bgImage, rect, color, 2, LINE_8); //原圖,矩陣範圍,線顏色,線寬,線類型n}n/*繪製橢圓(弧)*/nvoid MyEllipse() {nScalar color = Scalar(255, 255, 0); //青n ellipse(bgImage, Point(bgImage.cols / 2, bgImage.rows / 2), Size(bgImage.cols / 4, bgImage.rows/ 8),80 ,0 ,270 ,color, 2, LINE_8); //原圖,橢圓中心,橢圓半徑(長軸和短軸),傾斜角度,開始角度,結束角度,線顏色,線寬,線類型n}n/*繪製圓*/nvoid MyCircle() {nScalar color = Scalar(0,255, 255); //黃nPoint center = Point(bgImage.cols / 2, bgImage.rows / 2); //圓心n circle(bgImage, center, 50, color, 3, 8); //原圖,圓心,半徑長度,線顏色,線寬,線類型n}n/*多邊形填充*/nvoid MyPolygon() {nPoint pts[1][6]; //多個點,二維數組n pts[0][0] = Point(100, 100); //5邊形,6個點,能形成封閉區域n pts[0][1] = Point(100, 200);n pts[0][2] = Point(150, 250);n pts[0][3] = Point(200, 200);n pts[0][4] = Point(200, 100);n pts[0][5] = Point(100, 100);nconst Point* ppts[] = { pts[0] }; //指針nint npt[] = { 6 }; //多少個nScalar color = Scalar(255, 0, 0); //白n fillPoly(bgImage, ppts, npt, 1, color, 8); //原圖,多個點指針,幾個點連線,線寬,線顏色,線類型n}n/*繪製隨機線*/nvoid RandomLineDemo() {nRNG rng(12345); //隨機數生成函數nPoint pt1; // 線的位置兩點nPoint pt2;nMat bg = Mat::zeros(bgImage.size(), bgImage.type()); // 空白圖像全黑背景nfor (int i = 0; i < 100000; i++) {n pt1.x = rng.uniform(0, bgImage.cols); //點隨機規格,生成正態分布隨機數,不超過範圍n pt2.x = rng.uniform(0, bgImage.cols);n pt1.y = rng.uniform(0, bgImage.rows);n pt2.y = rng.uniform(0, bgImage.rows);nScalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)); //隨機顏色nif (waitKey(50)>0){ //等待xms,如果在此期間有按鍵按下,則立即結束並返回按下按鍵的ASCII碼,否則沒有按鍵返回-1nbreak; // 跳出for循環n }n line(bg, pt1, pt2, color, 1, 8); // 劃線n imshow("random line demo",bg);n } n}n

運行結果:


推薦閱讀:

50行代碼實現人臉檢測
1.9【OpenCV圖像處理】平滑模糊濾波
最近在搞Kinect的指尖識別,手指都可以畫出來了,有什麼方法可以將每隻手指對應的名稱識別出來啊?
[171103] 基於縮略圖哈希值比較的圖像相似性檢索
元旦贈書 | 18本紙質書:OpenCV、Python和機器學習,總有一本適合你

TAG:OpenCV | 图像处理 | 计算机视觉 |