opencv4学习总结-图像绘制与填充

画线

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

参数解释:

  • img:要绘图的画布
  • pt1:初始点
  • pt2:目标点
  • color:线条颜色
  • thickness:线条宽度
  • lineType:线形
  • shift:图像偏移(可以相对的移动位置和放缩自己大小)

示例:

using namespace cv;
using namespace std;


int main()
{
    Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);

    line(canvas, Point(10, 10), Point(300, 300), Scalar(0, 0, 255), 1, 8);
    imshow("canvas", canvas);


    waitKey(0);
    destroyAllWindows();

    return 0;
}

画圆

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

参数解释:

  • img:要绘图的画布
  • center:圆心位置
  • radius:半径
  • color:颜色
  • thickness:线条厚度,如果小于0也就是-1的话则填充圆形内部
  • lineType:线形
  • shift:偏移

示例:


using namespace cv;
using namespace std;


int main()
{
    Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);

    circle(canvas, Point(256, 256), 128, Scalar(0, 0, 255), -1, 8);
    imshow("canvas", canvas);


    waitKey(0);
    destroyAllWindows();

    return 0;
}

画矩形

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

原型1参数解释:

  • img:要绘图的画布
  • pt1:矩形左上角的点
  • pt2:矩形右下角的点
  • color:颜色
  • thickness:线宽
  • lineType:线形
  • shift:偏移

原型2参数解释:

  • img:要绘图的画布
  • rec:一个矩形对象,用左上的点坐标和长宽来定义
  • color:颜色
  • thickness:线宽
  • lineType:线形
  • shift:偏移

示例:


using namespace cv;
using namespace std;


int main()
{
    Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);

    rectangle(canvas, Point(156, 156), Point(356, 356), Scalar(0, 0, 255), 1, 8);
    rectangle(canvas, Rect(0, 0, 100, 100), Scalar(0, 255, 0), 1, 8);
    imshow("canvas", canvas);


    waitKey(0);
    destroyAllWindows();

    return 0;
}

画椭圆

API原型:
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 ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color,
                        int thickness = 1, int lineType = LINE_8);

原型1参数解释:

  • img:要绘图的画布
  • center:椭圆中心点
  • axes:椭圆的长轴短轴构成的矩形
  • angle:椭圆旋转的角度
  • startAngle:开始角度
  • endAngle:结束角度(开始角度和结束角度决定了是画一个完整的椭圆还是一段椭圆线
  • color:颜色
  • thickness:线宽
  • lineType:线形
  • shift:偏移

原型2参数解释

  • img:要绘图的画布
  • box:一个椭圆对象
  • 其余参数与原型1相同

示例:

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

using namespace cv;
using namespace std;


int main()
{
    Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);

    ellipse(canvas, Point(256, 256), Size(100, 50), 30, 0, 90, Scalar(0, 0, 255), 1, 8);
    RotatedRect rrt(Point(256, 256), Size(100, 50), 90);
    ellipse(canvas, rrt, Scalar(0, 255, 0), 1, 8);
    imshow("canvas", canvas);


    waitKey(0);
    destroyAllWindows();

    return 0;
}