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 <iostream>
#include <opencv2/opencv.hpp>

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 绘制图形和文字

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
OpenCV绘制中文时,可能会遇到乱码问题。这是因为 OpenCV 默认使用的是 ASCII 编码,而中文需要使用 Unicode 编码。为了解决这个问题,可以按照以下步骤操作: 1. 将要绘制的中文字符串转换为 Unicode 编码,可以使用 Qt 的 QString 类进行转换。 2. 将 Unicode 编码转换为 UTF-8 编码,可以使用 ICU 库进行转换。 3. 使用 OpenCV 的 putText 函数绘制 UTF-8 编码的字符串。 下面是一个示例代码: ``` #include <opencv2/opencv.hpp> #include <QString> #include <unicode/ucnv.h> int main() { // 要绘制的中文字符串 QString text = QString::fromUtf8("你好,世界!"); // 将字符串转换为 Unicode 编码 std::u16string utf16 = text.toStdU16String(); // 将 Unicode 编码转换为 UTF-8 编码 UErrorCode status = U_ZERO_ERROR; UConverter* utf8_converter = ucnv_open("UTF-8", &status); std::string utf8; utf8.resize(utf16.size() * 3); int32_t utf8_length = ucnv_fromUChars(utf8_converter, &utf8[0], utf8.size(), reinterpret_cast<const UChar*>(utf16.data()), utf16.size(), &status); utf8.resize(utf8_length); // 创建一个黑色图像 cv::Mat img = cv::Mat::zeros(100, 200, CV_8UC3); // 在图像上绘制字符串 cv::putText(img, utf8, cv::Point(10, 50), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 0, 255), 2); // 显示图像 cv::imshow("image", img); cv::waitKey(); return 0; } ``` 在上面的代码中,我们使用了 Qt 的 QString 类将中文字符串转换为 Unicode 编码,然后使用 ICU 库将 Unicode 编码转换为 UTF-8 编码。最后,我们使用 OpenCV 的 putText 函数在图像上绘制字符串。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Li_Zhi_Yao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值