【4】OpenCV入门-图形与文字绘制

1.重要绘图元素介绍

(1)cv::Point

Point表示平面上一个点(x,y),其中x,y都是整数。OpenCV中关于Point的定义如下:

typedef Point_<int> cv::Point2i;
typedef Point2i cv::Point;

其中Point_是一个模板类。

常用的类型还有,Point2f、Point3f,2f-二维坐标,坐标的类型为float型。3f同理。

(2)cv::Scalar

Scalar表示一个四元向量,在OpenCV中通常使用其前三个向量表示颜色。Scalar在OpenCV中Scalar定义如下:

typedef Scalar_<double> cv::Scalar;

对于Scalar_,在OpenCV中有着下面的继承关系

其中cv::Matx表示矩阵,cv::Vec表示向量。

2.图形绘制

在OpenCV中坐标原点在屏幕左上角,x轴正方向水平向右,y轴正方向竖直向下。

(1)直线-cv::line

函数原型:    void line(
            InputOutputArray img, 
            Point pt1, 
            Point pt2, 
            const Scalar& color,
             int thickness = 1, 
            int lineType = LINE_8, 
            int shift = 0
        );

函数说明:①在图像img上绘制一条从pt1到pt2的直线,img一般是Mat类型。

                  ②thickness-表示绘制的线的宽度。

                  ③lineType-表示线形。OpenCV中有三种可以选择LINE_4、LINE_8、LINE_AA。

                        LINE_4-表示采用4邻接的方式绘制,LINE_8-表示采用8邻接的方式绘制,LINE_AA

                        表示高斯滤波抗锯齿。

                   ④shift-表示坐标中的小数点位数。

代码演示:

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

int main()
{
	Mat src(300, 300, CV_8UC3, Scalar::all(255));
	namedWindow("srcImage");
	line(src, Point(10, 10), Point(200, 200), Scalar(0, 0, 255),2);
	imshow("srcImage", src);
	waitKey(0);
	return 0;
}

(2)圆弧-cv:ellipse

函数原型:    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
);

函数说明:①axes-表示椭圆长轴和短轴长度的一半。

                  ②angle、startAngle、enAngle-分别表示椭圆旋转的角度,圆弧开始的角度和圆弧结束的角度

                  ③thickness-表示线宽,如果取-1表示绘制填充的扇形。

代码演示:

ellipse(src, Point(150, 150), Size(150, 150), 0, 0, 90, Scalar(0, 0, 255),-1);
ellipse(src, Point(150, 150), Size(150, 150), 0, 0, 90, Scalar(0, 0, 255),2);

框架同直线部分类似,就不作赘述了。

(3)矩形-cv::rectangle

函数原型:    void rectangle(
            InputOutputArray img, 
            Point pt1, 
            Point pt2,
            const Scalar& color, 
            int thickness = 1,
            int lineType = LINE_8, 
            int shift = 0
        );

参数说明:thickness=-1,同样表示填充

代码演示:

rectangle(src, Point(100, 100), Point(200, 200), Scalar(0, 0, 255));

(4)圆绘制-cv::circle

函数原型:    void circle(
            InputOutputArray img, 
            Point center, 
            int radius,
            const Scalar& color, 
            int thickness = 1,
            int lineType = LINE_8, 
            int shift = 0
        );

函数说明:center-表示圆心。radius-表示半径。

代码演示:

circle(src, Point(150, 150), 50, Scalar(0, 0, 255));

(4)多边形:cv::fillPoly

函数原型:

    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() 
            );

函数说明:①pts-绘制多边形的各个点。

                  ②npts-绘制多边形点的数目。

                  ③ncontour-绘制的多边形的数量。

                  ④offset-轮廓所有点的可选偏移量。

代码演示:

const Point points[1][4] = { Point(100,100),Point(200,100),Point(250,200),Point(50,200) };
int npts = 4;
const Point *pts[] = { points[0] };
fillPoly(src, pts, &npts, 1,Scalar(0, 0, 255));

3.文字绘制

cv::putText

函数原型: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 
);

函数说明:①org-字体所在矩形的起始点,左下角。

                  ②fontFace-字体类型。

                  ③fontScale-字体比例因子。比例因子*字体类型基本大小=字体大小

                  ④bottomLeftOrigin-如果为true,则图像数据原点位于左下角。 否则,它位于左上角。

字体类型表格如下:

代码演示:

putText(src, "I love OpenCV", Point(10, 50), FONT_HERSHEY_PLAIN, 2, Scalar(100),2,8,true);
putText(src, "I love OpenCV", Point(10, 50), FONT_HERSHEY_PLAIN, 2, Scalar(100));

发布了7 篇原创文章 · 获赞 18 · 访问量 1913

猜你喜欢

转载自blog.csdn.net/qq_37981695/article/details/104227206
今日推荐