OpenCV3入门(三)基本绘图函数

1、函数原型

OpenCV包含大量的绘图函数,如直线、圆、椭圆、多边形等。下面是部分函数的原型。

/** @brief Draws a line segment connecting two points.*/
CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
                     int thickness = 1, int lineType = LINE_8, int shift = 0);

                  int thickness=1, int line_type=8, int shift=0, double tipLength=0.1);

/** @brief Draws a simple, thick, or filled up-right rectangle.*/
CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,
                          const Scalar& color, int thickness = 1,
                          int lineType = LINE_8, int shift = 0);


/** @brief Draws a simple or filled circle with a given center and radius.*/
CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius,
                       const Scalar& color, int thickness = 1,
                       int lineType = LINE_8, int shift = 0);

/** @brief Draws a simple or thick elliptic arc or fills an ellipse sector.*/
CV_EXPORTS_W 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);


/** @brief Fills the area bounded by one or more polygons. */
CV_EXPORTS_W void fillPoly(InputOutputArray img, InputArrayOfArrays pts,
                           const Scalar& color, int lineType = LINE_8, int shift = 0,
                           Point offset = Point() );

/** @brief Draws a text string.*/
CV_EXPORTS_W 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 );

2、示例

#include <opencv2/opencv.hpp>
#include <iostream>
#include <ctype.h>

using namespace std;
using namespace cv;
void DrawLine(Mat img, Point start, Point end)
{
    int thickness = 2;
    int lineType = 8;
    line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}

void DrawEllipse(Mat img, double angle)
{
    int thickness = 2;
    int lineType = 8;
    ellipse(img,
        Point(img.rows / 2.0, img.cols / 2.0),
        Size(img.rows / 4.0, img.cols / 16.0),
        angle,
        0,
        360,
        Scalar(8, 131, 229),
        thickness,
        lineType);
}

void DrawFilledCircle(Mat img, Point center)
{
    int thickness = -1;
    int lineType = 8;
    circle(img,
        center,
        img.rows / 32.0,
        Scalar(0, 0, 255),
        thickness,
        lineType);
}

void DrawRect(Mat img, Point start, Point end)
{
    rectangle(img, start, end, Scalar(120, 0, 0), 2);
}

int main() {
    CvFont font;
    Mat m = Mat::zeros(300, 300, CV_8UC3);
    m.setTo(cv::Scalar(100, 100, 0));

    DrawEllipse(m, 90);
    DrawEllipse(m, 0);
    DrawEllipse(m, 45);
    DrawEllipse(m, -45);

    DrawFilledCircle(m, Point(m.rows / 2, m.cols / 2));
    DrawRect(m,Point(m.rows/4,m.cols/4),Point(m.rows/2+m.rows/4, m.cols/2+ m.cols/4));
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 2, 8);
    IplImage* image = &IplImage(m); // Mat -> IplImage 类型转换
    cvPutText(image, "Hello World !", cvPoint(70, 40), &font, cvScalar(0, 255, 0, 1));
    imshow("椭圆图", m);
    waitKey(0);
}

输出如下。

3、遇到的问题

1)Error:OpenCV 不存在从 "cv::Mat" 到 "CvArr的转换

中间需要IplImage 衔接一下.

cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 2, 8);

IplImage* image = &IplImage(m); // Mat -> IplImage 类型转换

cvPutText(image, "Hello World !", cvPoint(70, 40), &font, cvScalar(0, 255, 0, 1));

imshow("椭圆图", m);

4、参考文献

1、《OpenCV3 编程入门》,电子工业出版社,毛星雨著

2、《学习OpenCV》,清华大学出版社,Gary Bradski, Adrian kaehler著

3、OpenCV 中的绘图函数

https://www.cnblogs.com/yujiachen/p/7672417.html?utm_source=debugrun&utm_medium=referral

  

尊重原创技术文章,转载请注明。

https://www.cnblogs.com/pingwen/p/12296477.html

posted on 2020-02-11 20:03  啊哈彭  阅读(250)  评论(0编辑  收藏

导航