5--OpenCV:图形绘制与文字输出

opencv图形绘制与文字输出

注意:

        ①cv::Point类型

        ②C++ STL中 vector的data()函数 :

        std::vector::data() 是 C++ 中的 STL,它返回一个指向内存数组的直接指针,该内存数组由向量内部用于存储其拥有的元素

vector_name.data()

参数:该函数不接受任何参数。

返回值:该函数返回一个指向数组中第一个元素的指针,该指针在向量内部使用。

// C++ program to demonstrate the
// vector::date() function

#include <bits/stdc++.h>
using namespace std;
int main()
{
	// initialising vector
	vector<int> vec = { 10, 20, 30, 40, 50 };

	// memory pointer pointing to the
	// first element
	int* pos = vec.data();

	// prints the vector
	cout << "The vector elements are: ";
	for (int i = 0; i < vec.size(); ++i)
		cout << *pos++ << " ";
	return 0;
}

         ③注意画多边形的时候,传入的是点集简单版的可以直接传入vector<Point>类型。

复杂版本的,需要 const Point**类型--->vector的data方法,构造一个数组。

绘线

void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0);
//线的样式
enum LineTypes 
{
    FILLED  = -1,
    LINE_4  = 4, //!< 4-connected line
    LINE_8  = 8, //!< 8-connected line
    LINE_AA = 16 //!< antialiased line
};
/*******************************************************************
*           img:            绘制在那个图像上
*           pt1:            起点
*           pt2:            终点
*           color:          颜色
*           thickness:      厚度(宽度) 
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 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);
//线的样式
enum LineTypes {
    FILLED  = -1,
    LINE_4  = 4, //!< 4-connected line
    LINE_8  = 8, //!< 8-connected line
    LINE_AA = 16 //!< antialiased line
};
/*******************************************************************
*           img:            绘制在那个图像上
*           center:         圆心坐标
*           radius:         半径
*           color:          颜色
*           thickness:      厚度(宽度)
*                               -1:     填充圆
*                               其他值:  空心
*           lineType:        线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 8邻接连接线
*                   LINE_AA:反锯齿连接线(高斯滤波)
*           shift:          坐标点小数点位数(可忽略不写)
*********************************************************************/

绘矩形

void rectangle(InputOutputArray img, Rect rec,const Scalar& color, int thickness = 1,int lineType = LINE_8, int shift = 0);
/*******************************************************************
*           img:            绘制在那个图像上
*           rec:            矩形大小  Rect(x,y,w,h);  
*                   x,y:    起始坐标
*                   w,h:    宽度和高度
*           color:          颜色
*           thickness:      厚度(宽度)
*                               -1:     填充矩形
*                               其他值:  空心矩形
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 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);
/*******************************************************************
*           img:            绘制在那个图像上
*           center:         椭圆圆心
*           axes:           矩形内置椭圆
*           angle:          倾斜角
*           startAngle:     扩展的弧度 0
*           endAngle:        扩展的弧度  360
*           color:          颜色
*           thickness:      线宽度
*                               -1:     填充矩形
*                               其他值:  空心矩形
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 8邻接连接线
*                   LINE_AA:反锯齿连接线(高斯滤波)
*           shift:          坐标点小数点位数(可忽略不写)
*********************************************************************/

绘制多边形

polylines简单版

void polylines(InputOutputArray img, InputArrayOfArrays pts,bool isClosed, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0 );
/*******************************************************************
*           img:            绘制在那个图像上
*           pts:            点集
*           isClosed:       是否封闭
*           color:          颜色
*           thickness:      厚度(宽度)
*                               -1:     引发中断
*                               其他值:  空心矩形
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 8邻接连接线
*                   LINE_AA:反锯齿连接线(高斯滤波)
*           shift:          坐标点小数点位数(可忽略不写)
*********************************************************************/

polylines复杂版

void polylines(InputOutputArray img, const Point* const* pts, const int* npts,int ncontours, bool isClosed, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0 );
/*******************************************************************
*           img:            绘制在那个图像上
*           pts:            点集
*           npts:           点数目
*           ncontours:      待绘制折线数
*           isClosed:       是否封闭
*           color:          颜色
*           thickness:      厚度(宽度)
*                               -1:     应发中断
*                               其他值:  空心形状
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 8邻接连接线
*                   LINE_AA:反锯齿连接线(高斯滤波)
*           shift:          坐标点小数点位数(可忽略不写)
*********************************************************************/

fillPoly简单版

void fillPoly(InputOutputArray img, InputArrayOfArrays pts,const Scalar& color, int lineType = LINE_8, int shift = 0,Point offset = Point() );
/*******************************************************************
*           img:            绘制在那个图像上
*           pts:            点集
*           color:          颜色
*           thickness:      厚度(宽度)
*                               -1:     引发中断
*                               其他值:  空心矩形
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 8邻接连接线
*                   LINE_AA:反锯齿连接线(高斯滤波)
*           shift:          坐标点小数点位数(可忽略不写)
*           offset: 忽略
*********************************************************************/

fillPoly复杂版

void fillPoly(InputOutputArray img, const Point** pts,const int* npts, int ncontours,const Scalar& color, int lineType = LINE_8, int shift = 0,Point offset = Point() );
/*******************************************************************
*           img:            绘制在那个图像上
*           pts:            点集
*           npts:            点数
*           color:          颜色
*           ncontours:      待绘制折线数
*           thickness:      厚度(宽度)
*                               -1:     引发中断
*                               其他值:  空心矩形
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 8邻接连接线
*                   LINE_AA:反锯齿连接线(高斯滤波)
*           shift:          坐标点小数点位数(可忽略不写)
*           offset:         
*********************************************************************/

文字输出

        目前字体中只支持英文

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 );
/*******************************************************************
*           img:            绘制在那个图像上
*           text:           绘制文字
*           org:            文本框左下角
*           fontFace:       字体
*           fontScale:      缩放
*           color:          颜色
*           thickness       线宽度
*           lineType:       线的样式
*                   FILLED: 线填充的
*                   LINE_4: 4邻接连接线
*                   LINE_8: 8邻接连接线
*                   LINE_AA:反锯齿连接线(高斯滤波)
*           bottomLeftOrigin:       起点位置
*                               true:  左上角  反转倒立显示
*                               false: 左下角  正常显示
*********************************************************************/
//opencv 不识别汉字
//fontFace: 字体
enum HersheyFonts {
    FONT_HERSHEY_SIMPLEX        = 0, //!< normal size sans-serif font   //灯芯体
    FONT_HERSHEY_PLAIN          = 1, //!< small size sans-serif font
    FONT_HERSHEY_DUPLEX         = 2, //!< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)
    FONT_HERSHEY_COMPLEX        = 3, //!< normal size serif font
    FONT_HERSHEY_TRIPLEX        = 4, //!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX)
    FONT_HERSHEY_COMPLEX_SMALL  = 5, //!< smaller version of FONT_HERSHEY_COMPLEX
    FONT_HERSHEY_SCRIPT_SIMPLEX = 6, //!< hand-writing style font
    FONT_HERSHEY_SCRIPT_COMPLEX = 7, //!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX
    FONT_ITALIC                 = 16 //!< flag for italic font
};

综合代码

#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
using namespace cv;
class Shape 
{
public:
    Shape() :mat(imread("mm.jpg")) {}
    void DrawLine(int x = 0, int y = 0, int xx = 600, int yy = 460) 
    {
        line(mat, Point(x, y), Point(xx, yy), Scalar(0, 0, 255), 2, LINE_AA);
    }
    void DrawCircle(int x = 300, int y = 230, int r = 10) 
    {
        circle(mat, Point(x, y), r, Scalar(0, 255, 0), -1, FILLED);  //填充圆
        circle(mat, Point(x, y), r+2, Scalar(0, 0, 255), 2, FILLED); //空心圆
    }
    void DrawRectangle(int x = 100, int y = 100, int w = 40, int h = 40) 
    {
        rectangle(mat, Rect(x, y, w, h), Scalar(255, 0, 0), -1, LINE_4);
        rectangle(mat, Rect(x - 1, y - 1, w + 2, h + 2), Scalar(0, 255, 0));
    }
    void DrawEllipse(int x = 400, int y = 200, Size size = { 100,200 }) 
    {
        ellipse(mat, Point(x, y), size, 180, 0, 360, Scalar(255, 0, 0),1);
        ellipse(mat, Point(x, y), size, 90, 0, 360, Scalar(255, 0, 0),-1);
    }
    void DrawText() 
    {
        putText(mat, "opencv test draw shape", Point(50, 50), FONT_ITALIC, 1.0, Scalar(0, 0, 255),
        2,LINE_AA,false);
    }
    void Show(string wName = "shape") 
    {
        imshow(wName, mat);
        waitKey(0);
    }
    void DrawPolygon()
    {
        vector<Point> pixel;
        for (int i = 0; i < 5; i++) 
        {
            pixel.push_back(Point(rand() % 600, rand() % 460));
        }
        //简单版本
        polylines(mat, pixel, true, Scalar(0, 0, 255), 2);
        fillPoly(mat, pixel, Scalar(0, 255, 0), 1);
        
        //复杂版本
        //int size = 5;
        //auto p = pixel.data();
        //polylines(mat, &p, &size, 1, true, Scalar(0, 0, 255),1);
        //const Point** pts = const_cast<const Point**>(&p);
        //fillPoly(mat, pts, &size, 1, Scalar(255, 0, 255),1);
    }
protected:
    Mat mat;
};
int main() 
{
    srand((unsigned int)time(nullptr));
    Shape* pshape = new Shape;
    pshape->DrawLine();
    pshape->DrawCircle();
    pshape->DrawRectangle();
    pshape->DrawEllipse();
    pshape->DrawText();
    pshape->DrawPolygon();
​
    pshape->Show();
    delete pshape;
    return 0;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_Ocean__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值