OpenCV图像处理教程C++(八) 绘制形状与文字

point表示2D平面上一个点x,y
Point p p.x, p.y
or p=Point(x,y)
Scalar表示四个元素的向量
Scalar(a,b,c);a=blue,b=green,c=red

代码:

#include <opencv2/opencv.hpp>
#include<iostream>
#include<math.h>
#include <string> 
#include<fstream> 
using namespace cv;
using namespace std;

Mat src, dst;

void getlinepoint() {//绘制线条
    Point p = Point(20, 30);
    Point p2 = Point(800, 900);
    Scalar sl = Scalar(0, 255, 255);
    line(src, p, p2, sl, 1);
}

void getrectangle() {//绘制矩形
    Rect rect = Rect(200, 100, 500, 300);
    Scalar sl = Scalar(244, 124, 121);
    rectangle(src, rect, sl, 2);
}

void getellipse() {//绘制椭圆
    Scalar sl = Scalar(11, 12, 132);
    ellipse(src, Point(src.cols / 2, src.rows / 2), Size(src.cols / 4, src.rows / 8), 45, 0, 360, sl, 3);
}

void getcircle() {//绘制圆形
    Scalar sl = Scalar(0, 1, 2);
    circle(src, Point(src.rows / 2, src.cols / 2), src.cols/8, sl, 4);
}

void getfillpoly() {//填充
        Point pts[1][5];
        pts [0][0] = Point(200, 300);
        pts[0][1] = Point(100, 300);
        pts[0][2] = Point(200, 400);
        pts[0][3] = Point(100, 200);
        pts[0][4] = Point(200, 400);
        const Point*ppts[] = { pts[0] };
        int npt[] = { 5 };
        Scalar sl = Scalar(200, 3, 88);
        fillPoly(src, ppts, npt, 1, sl);
}
int main() {
    src = imread("C:\\Users\\Administrator\\Desktop\\3.jpg");
    getlinepoint();
    getrectangle();
    getellipse();
    getcircle();
    getfillpoly();
    putText(src, "hello opencv", Point(800, 666), CV_FONT_BLACK, 8, Scalar(200, 30),3);
    imshow("绘图", src);
    waitKey(0);
}

结果:
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_26907755/article/details/81667748