C++ OpenCV4.5 绘制形状与文字(四)

系列文章目录

C++ OpenCV4.5环境搭建(一)

C++ OpenCV4.5常用API查询手册(二)

C++ OpenCV4.5 图像处理(三)


文章目录

  • 系列文章目录
  • 前言
  • 一、绘制形状
    • 直线
    • 矩形
    • 椭圆
    • 填充
  • 二、图片绘制文字
  • 总结


前言

该篇主要讲解使用 OpenCV 绘制形状和向图片写文字


一、绘制形状

绘制主要用到 cv::Point 和 cv::Scalar,Point 表示 2D 平面上的一个点,Scalar 表示四个元素的向量

直线

画线 cv::line 函数声明如下:

// 功能:绘制一条连接两个点的线段
// 参数:img 图像源,Mat对象类型
//		pt1 线段的点1
//		pt2 线段的点2
//		color 线条颜色
//		thickness 线条厚度
//		lineType 线型类型(LINE_4\LINE_8\LINE_AA)
//		shift 移动点坐标中小数位数
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
                     int thickness = 1, int lineType = LINE_8, int shift = 0);

示例代码如下:

#include 
#include 

using namespace std;
using namespace cv;

void DrawLine(Mat& img)
{
	// 绘制
    Point p1(30, 30);
    Point p2(300, 300);
    Scalar color(0, 0, 255);
    line(img, p1, p2, color, 3);
}

int main()
{
    Mat img = imread("/home/kylin/1.jpeg");
    if(!img.data)
    {
        cout << "load image failed.." << endl;
        return -1;
    }

    DrawLine(img);
    imshow("测试", img);
    waitKey(0);

    return 0;
}

矩形

画矩形 cv::rectangle 函数声明如下:

// 功能:绘制一个矩形
// 参数:img 图像源,Mat对象类型
//		rec 矩形
//		color 矩形颜色或亮度(灰度图像)
//		thickness 线条厚度
//		lineType 线型类型(LINE_4\LINE_8\LINE_AA)
//		shift 移动点坐标中小数位数
void rectangle(Mat& img, Rect rec, const Scalar& color,
               int thickness = 1, int lineType = LINE_8, int shift = 0);
                          

示例代码如下:

void DrawRectangle(Mat &img)
{
    // 绘制一个起点X:200、Y:150,宽300,高400的矩形
    Rect rect(200, 150, 300, 400);
    Scalar color(255, 0, 0);
    rectangle(img, rect, color, 5);
}

画圆 cv::circle 函数声明如下:

// 功能:绘制一个圆
// 参数:img 图像源,Mat对象类型
//		center 圆的中心
//		radius 圆的半径
//		color 圆的颜色
//		thickness 线条厚度
//		lineType 线型类型(LINE_4\LINE_8\LINE_AA)
//		shift 移动点坐标中小数位数
void circle(InputOutputArray img, Point center, int radius,
                       const Scalar& color, int thickness = 1,
                       int lineType = LINE_8, int shift = 0);                        

示例代码如下:

void DrawCircle(Mat &img)
{
    // 绘制一个起点X 200、Y 150,宽300,高400的矩形
    Point center(img.cols / 2, img.rows / 2);
    Scalar color(0, 255, 0);
    circle(img, center, 120, color, 4);
}

椭圆

画圆 cv::ellipse 函数声明如下:

// 功能:绘制一个椭圆
// 参数:img 图像源,Mat对象类型
//		center 椭圆的中心点
//		axes 椭圆主轴大小的一半
//		angle 椭圆旋转角度(度)
//		startAngle 椭圆弧的起始角(度)
//		startAngle 椭圆弧的结束角(度)
//		color 矩形颜色或亮度(灰度图像)
//		thickness 线条厚度
//		lineType 线型类型(LINE_4\LINE_8\LINE_AA)
//		shift 移动点坐标中小数位数
void ellipse(InputOutputArray img, Point center, Size axes,
                        double angle, double startAngle, double endAngle,
                        const Scalar& color, int thickness = 1,
                        int lineType = LINE_8, int shift = 0);                     

示例代码如下:

void DrawEllipse(Mat &img)
{
    // 绘制一个起点X 200、Y 150,宽300,高400的矩形
    Point center(img.cols / 2, img.rows / 2);
    Size axes(img.cols / 4, img.rows / 8);
    Scalar color(255, 0, 255);
    // 绘制一个30度倾斜,0~180度的前半椭圆
    ellipse(img, center, axes, 30, 0, 180, color, 4);
}

填充

画圆 cv::fillPoly 函数声明如下:

// 功能:填充多边形
// 参数:img 图像源,Mat对象类型
//		pts 多边形数组,其中每个多边形都表示为一个点数组
//		npts 
//		ncontours 
//		color 填充的颜色
//		lineType 线型类型(LINE_4\LINE_8\LINE_AA)
//		shift 移动点坐标中小数位数
//		offset 等高线所有点的可选偏移
void fillPoly(Mat& img, const Point** pts,
                         const int* npts, int ncontours,
                         const Scalar& color, int lineType = LINE_8, int shift = 0,
                         Point offset = Point() );
                          

示例代码如下:

void DrawFillPoly(Mat &img)
{
    Point pts[1][7];
    pts[0][0] = Point(150, 100);
    pts[0][1] = Point(250, 100);
    pts[0][2] = Point(300, 150);
    pts[0][3] = Point(250, 200);
    pts[0][4] = Point(150, 200);
    pts[0][5] = Point(100, 150);
    pts[0][6] = Point(150, 100);
    const Point* ppts[] = { pts[0] };
    int npt[] = {7};
    Scalar color(0, 0, 0);
    // 填充一个六边形
    fillPoly(img, ppts, npt, 1, color);
}

二、图片绘制文字

函数声明如下:

// 功能:绘制字符串
// 参数:img 图像源,Mat对象类型
//		text 要绘制的字符串
//		org 图像中字符串的起始位置(左下角)
//		fontFace 字体类型
//		fontScale 字体大小
//		color 字体颜色
//		thickness 绘制字符串的线条的粗细
//		lineType 线型类型(LINE_4\LINE_8\LINE_AA)
//		bottomLeftOrigin 如果为true,则图像数据原点位于左下角。否则,它在左上角。
void putText( InputOutputArray img, const String& text, Point org,
                         int fontFace, double fontScale, Scalar color,
                         int thickness = 1, int lineType = LINE_8,
                         bool bottomLeftOrigin = false );

代码如下(示例):

void DrawText(Mat &img, const char* pText)
{
    if(pText == NULL)
    {
        return;
    }

    Point org(30, 30);
    Scalar color(12, 255, 200);
    putText(img, pText, org, CV_FONT_HERSHEY_COMPLEX, 1.0, color, 2);
}

总结

以上就是今天要讲的内容,本文仅仅简单介绍了使用 OpenCV 绘制图形和文字

你可能感兴趣的:(OpenCV4.5学习记录,opencv,c++)