绘图及注释

Posted on 2020-05-27 16:26  金色的省略号  阅读(205)  评论(0编辑  收藏  举报

  我们常常想画一些图,或在某些地方获得的图像上画些东西,OpenCV提供了一些函数让我们画线,画矩形,画圆等等;

  OpenCV的绘图函数,可以在任意深度的图像上工作,但在大多数情况下,它们只对图像的前三个通道有影响,如果是单通道图像,则默认只影响第一个通道。
大多数绘图函数都支持操作对象的颜色、宽度、线型和亚像素对齐等参数。

  想指定颜色的时候,通常的做法是使用cv::Scalar对象,即使大多数时候只用到最前面的三个值,OpenCV在对多通道图像进行色彩渲染时使用了BGR的顺序( imshow()所使用的顺序 ) 

  一、艺术线条和填充多边形

  cv::circle()  画一个圆  

void circle(
    cv::Mat&           img,                //Image to be drawn on
    cv::Point          center,             //Location of circle center
    int                radius,          //Radius of circle
    const cv::Scalar&  color,            //Color, RGB form
    int                   thickness = 1,   // Thickness of line
    int                lineType = 8,    //Connectedness, 4 or 8
    int                shift = 0        //Bits of radius to treat as fraction
);

   cv::clipLine()  判断一条直线是否在给定的矩形内

bool clipLine( // True if any part of line in 'imgRect'
    cv::Rect imgRect, // Rectangle to clip to
    cv::Point& pt1, // First endpoint of line, overwritten
    cv::Point& pt2 // Second endpoint of line, overwritten
);

bool clipLine( // True if any part of line in image size
    cv::Size imgSize, // Size of image, implies rectangle at 0,0
    cv::Point& pt1, // First endpoint of line, overwritten
    cv::Point& pt2 // Second endpoint of line, overwritten
);

  cv::ellipse()  画一个椭圆

bool ellipse(
    cv::Mat& img, // Image to be drawn on
    cv::Point center, // Location of ellipse center
    cv::Size axes, // Length of major and minor axes
    double angle, // Tilt angle of major axis
    double startAngle, // Start angle for arc drawing
    double endAngle, // End angle for arc drawing
    const cv::Scalar& color, // Color, BGR form
    int thickness = 1, // Thickness of line
    int lineType = 8, // Connectedness, 4 or 8
    int shift = 0 // Bits of radius to treat as fraction
);

bool ellipse(
    cv::Mat& img, // Image to be drawn on
    const cv::RotatedRect& rect, // Rotated rectangle bounds ellipse
    const cv::Scalar& color, // Color, BGR form
    int thickness = 1, // Thickness of line
    int lineType = 8, // Connectedness, 4 or 8
    int shift = 0 // Bits of radius to treat as fraction
);

  shift参数,OpenCV源码中,是用移位操作实现的,就是 radius 值 会被除以2的shift次方

  cv::ellipse2Poly()  计算一个近似椭圆的多边形  

void ellipse2Poly(
    cv::Point center, // Location of ellipse center
    cv::Size axes, // Length of major and minor axes
    double angle, // Tilt angle of major axis
    double startAngle, // Start angle for arc drawing
    double endAngle, // End angle for arc drawing
    int delta, // Angle between sequential vertices
    vector<cv::Point>& pts // Result, STL-vector of points
);

  cv::fillPoly()  画一个填充的任意多边形

void fillPoly(
    cv::Mat& img, // Image to be drawn on
    const cv::Point* pts, // C-style array of arrays of points
    int npts, // Number of points in 'pts[i]'
    int ncontours, // Number of arrays in 'pts'
    const cv::Scalar& color, // Color, BGR form
    int lineType = 8, // Connectedness, 4 or 8
    int shift = 0, // Bits of radius to treat as fraction
    cv::Point offset = Point() // Uniform offset applied to all points
);

   cv::rectangle()  画一个矩形

void rectangle(
    cv::Mat& img, // Image to be drawn on
    cv::Point pt1, // First corner of rectangle
    cv::Point pt2 // Opposite corner of rectangle
    const cv::Scalar& color, // Color, BGR form
    int lineType = 8, // Connectedness, 4 or 8
    int shift = 0 // Bits of radius to treat as fraction
);

void rectangle(
    cv::Mat& img, // Image to be drawn on
    cv::Rect r, // Rectangle to draw
    const cv::Scalar& color, // Color, BGR form
    int lineType = 8, // Connectedness, 4 or 8
    int shift = 0 // Bits of radius to treat as fraction
);

  cv::polyLines()    画多重折线

void polyLines(
    cv::Mat& img, // Image to be drawn on
    const cv::Point* pts, // C-style array of arrays of points
    int npts, // Number of points in 'pts[i]'
    int ncontours, // Number of arrays in 'pts'
    bool isClosed, // If true, connect last and first pts
    const cv::Scalar& color, // Color, BGR form
    int lineType = 8, // Connectedness, 4 or 8
    int shift = 0 // Bits of radius to treat as fraction
);

  cv::LineIterator  迭代器

LineIterator::LineIterator(
    cv::Mat& img, // Image to be drawn on
    cv::Point pt1, // First endpoint of line
    cv::Point pt2 // Second endpoint of line
    int lineType = 8, // Connectedness, 4 or 8
    bool leftToRight = false // If true, always start steps on the left
);

  二、字体和文字

  cv::putText()

void cv::putText(
    cv::Mat& img, // Image to be drawn on
    const string& text, // write this (often from cv::format)
    cv::Point origin, // Upper-left corner of text box
    int fontFace, // Font (e.g., cv::FONT_HERSHEY_PLAIN)
    double fontScale, // size (a multiplier, not "points"!)
    cv::Scalar color, // Color, RGB form
    int thickness = 1, // Thickness of line
    int lineType = 8, // Connectedness, 4 or 8
    bool bottomLeftOrigin = false // true='origin at lower left'
);

  可以使用的字体 ( 第4个参数 )

cv::FONT_HERSHEY_SIMPLEX //Normal size sans-serif
cv::FONT_HERSHEY_PLAIN //Small size sans-serif
cv::FONT_HERSHEY_DUPLEX //Normal size sans-serif; more complex than cv::FONT_HERSHEY_SIMPLEX
cv::FONT_HERSHEY_COMPLEX //Normal size serif; more complex than cv::FONT_HERSHEY_DUPLEX
cv::FONT_HERSHEY_TRIPLEX //Normal size serif; more complex than cv::FONT_HERSHEY_COMPLEX
cv::FONT_HERSHEY_COMPLEX_SMALL //Smaller version of cv::FONT_HERSHEY_COMPLEX
cv::FONT_HERSHEY_SCRIPT_SIMPLEX //Handwriting style
cv::FONT_HERSHEY_SCRIPT_COMPLEX //More complex variant of cv::FONT_HERSHEY_SCRIPT_SIMPLEX