opencv7-绘制形状和文字
生活随笔
收集整理的這篇文章主要介紹了
opencv7-绘制形状和文字
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、理論
?
二、代碼演示?
#include<opencv2\opencv.hpp> #include<iostream> #include<math.h> using namespace cv; using namespace std; Mat bgImage; const char* drawdemo_win = "draw shapes and text demo"; void MyLines(); void MyRectangle(); void MyEllipse(); void MyCircle(); void MyPolygon(); void RandomLineDemo(); int main(int argc,char** argv) {bgImage= imread("E:\\vs2015\\opencvstudy\\3.jpg", 1);if (!bgImage.data){cout << "could not load image1!" << endl;return -1;}MyLines();MyRectangle();MyEllipse();MyCircle();MyPolygon();RandomLineDemo();putText(bgImage, "Hello World", Point(350, 200), CV_FONT_HERSHEY_SCRIPT_COMPLEX,4, Scalar(0, 200, 200), 4, LINE_8);imshow(drawdemo_win, bgImage);waitKey(0);return 0; } void MyLines() {Point p1 = Point(20, 20);Point p2;p2.x = 800;p2.y = 800;Scalar color = (0, 0, 255);line(bgImage, p1, p2, color, 1, LINE_AA); //在圖上畫線..LINE_AA 代表反鋸齒 }void MyRectangle() {Rect rect = Rect(500, 500, 300, 300);Scalar color = (255, 0, 0);rectangle(bgImage, rect, color, 2, LINE_4); }void MyEllipse() {ellipse(bgImage, Point(bgImage.cols/2, bgImage.rows/2), Size(bgImage.cols / 4, bgImage.rows /8),90,0,360, Scalar(0, 255, 0), 2, LINE_8);// Point(x,y)代表橢圓中心點坐標 //Size(x,y)代表 長軸,短軸 // 90 代表橢圓的傾斜程度//0-360 代表橢圓的繪制弧度// Scalar(0, 255, 0) 橢圓顏色 }void MyCircle() {Point p1 = Point(bgImage.cols / 2, bgImage.rows / 2);circle(bgImage, p1, 100, Scalar(255, 255, 0), 2, LINE_8); }void MyPolygon() {Point pts[1][5];pts[0][0] = Point(100, 100);pts[0][1] = Point(100, 200);pts[0][2] = Point(200, 200);pts[0][3] = Point(200, 100);pts[0][4] = Point(100, 100);const Point* ppts[] = { pts[0] };int nps[] = {5};Scalar color = Scalar(255, 12, 255);fillPoly(bgImage, ppts, nps, 1, color, LINE_8); //1代表只有一個輪廓}void RandomLineDemo() {RNG rng(12345);Point pt1;Point pt2;Mat bg = Mat::zeros(bgImage.size(), bgImage.type());for (int i = 0; i < 100000; i++){pt1.x = rng.uniform(0, bgImage.cols);pt2.x = rng.uniform(0, bgImage.cols);pt1.y = rng.uniform(0, bgImage.rows);pt2.y = rng.uniform(0, bgImage.rows);line(bg, pt1, pt2, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 2, LINE_8);if (waitKey(50) > 0){break;}imshow("RandomLineDemo", bg);}}?
總結
以上是生活随笔為你收集整理的opencv7-绘制形状和文字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习-01regression
- 下一篇: 使用Xftp5连接云服务器