OpenCV6-图形绘制

OpenCV6-图形绘制

    • 1.绘制圆形
    • 2.绘制直线
    • 3.绘制椭圆
    • 4.绘制多边形
    • 5.文字生成
    • 6.demo


1.绘制圆形

void cv::circle(
    InputOutputArray img,  // 需要绘制圆形的图像
    Point center,          // 圆心坐标
    int radius,            // 半径,单位为像素
    const Scalar& color,   // 圆形颜色
    int thickness = 1,     // 轮廓宽度,如果数值为负,则绘制一个实心圆
    int lineType = LINE_8, // 边界的类型,可取值FILLED、LINE_4、LINE_8、LINE_AA
    int shift = 0          // 中心坐标和半径数值中的小数位数
);

// example
circle(img, Point(50, 50), 25, Scalar(255, 255, 255), -1);  //绘制一个实心圆
circle(img, Point(100, 50), 20, Scalar(255, 255, 255), 4);  //绘制一个空心圆

2.绘制直线

void line(
    InputOutputArray img,   
    Point pt1, Point pt2,  // 直线起点和终点
    const Scalar& color,   // 直线颜色,三通道表示
    int thickness = 1, 
    int lineType = LINE_8, 
    int shift = 0
);

line(img, Point(100, 100), Point(200, 100), Scalar(255, 255, 255), 2, LINE_4, 0);  //绘制一条直线

3.绘制椭圆

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

在OpenCV中还提供了ellipse2Poly函数用于输出椭圆边界的像素坐标,但是不会在图像中绘制椭圆:

void ellipse2Poly( 
    Point center, 
    Size axes, 
    int angle,
    int arcStart, 
    int arcEnd, 
    int delta,  // 后续折线顶点之间的角度,他定义了近似精度
    CV_OUT std::vector<Point>& pts // 椭圆边缘像素坐标向量集合
);
/*
该函数与绘制椭圆所需的参数类似,只不过不再将椭圆输出到图像中,而是通过vector(向量)将椭圆边缘的坐标点存储起来,便于后续的再处理。
*/

4.绘制多边形

矩形的绘制:

void rectangle(
    nputOutputArray 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);

typedef Rect2i Rect;
typedef Rect_<int> Rect2i;

template<typename _Tp> class Rect_
{
public:
    // ...
    _Tp x; //!< x coordinate of the top-left corner
    _Tp y; //!< y coordinate of the top-left corner
    _Tp width; //!< width of the rectangle
    _Tp height; //!< height of the rectangle
};

下面是绘制多边形的方法:可以一次绘制多个多边形。

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() // 所有顶点的可选偏移
);

5.文字生成

目前只支持英文文本的输出。

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 // 图像数据原点的位置,默认为左上角;如果参数为true,则原点为左下角。
);

/*
FONT_HERSHEY_SIMPLEX:正常大小的无衬线文字
FONT_HERSHEY_PLAIN:小尺寸无衬线文字
FONT_ITALIC:斜体字体
*/
// 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
};

6.demo

#include 
#include  // debug no log
#include 
using namespace cv;
using namespace std;

int main()
{
	cout << "OpenCV Version: " << CV_VERSION << endl;
	utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);

	Mat img = Mat::zeros(Size(512, 512), CV_8UC3);  //生成一个黑色图像用于绘制几何图形
	//绘制圆形
	circle(img, Point(50, 50), 25, Scalar(255, 255, 255), -1);  //绘制一个实心圆
	circle(img, Point(100, 50), 20, Scalar(255, 255, 255), 4);  //绘制一个空心圆
	//绘制直线
	line(img, Point(100, 100), Point(200, 100), Scalar(255, 255, 255), 2, LINE_4, 0);  //绘制一条直线
	//绘制椭圆
	ellipse(img, Point(300, 255), Size(100, 70), 0, 0, 100, Scalar(255, 255, 255), -1);  //绘制实心椭圆的一部分
	ellipse(img, RotatedRect(Point2f(150, 100), Size2f(30, 20), 0), Scalar(0, 0, 255), 2);  //绘制一个空心椭圆
	vector<Point> points;
	ellipse2Poly(Point(200, 400), Size(100, 70), 0, 0, 360, 2, points);  //用一些点来近似一个椭圆
	for (int i = 0; i < points.size() - 1; i++)  //用直线把这个椭圆画出来
	{
		if (i == points.size() - 1)
		{
			line(img, points[0], points[i], Scalar(255, 255, 255), 2);  //椭圆中后于一个点与第一个点连线
			break;
		}
		line(img, points[i], points[i + 1], Scalar(255, 255, 255), 2);  //当前点与后一个点连线
	}
	//绘制矩形
	rectangle(img, Point(50, 400), Point(100, 450), Scalar(125, 125, 125), -1);
	rectangle(img, Rect(400, 450, 60, 50), Scalar(0, 125, 125), 2);
	//绘制多边形
	Point pp[2][6];
	pp[0][0] = Point(72, 200);
	pp[0][1] = Point(142, 204);
	pp[0][2] = Point(226, 263);
	pp[0][3] = Point(172, 310);
	pp[0][4] = Point(117, 319);
	pp[0][5] = Point(15, 260);
	pp[1][0] = Point(359, 339);
	pp[1][1] = Point(447, 351);
	pp[1][2] = Point(504, 349);
	pp[1][3] = Point(484, 433);
	pp[1][4] = Point(418, 449);
	pp[1][5] = Point(354, 402);
	Point pp2[5];
	pp2[0] = Point(350, 83);
	pp2[1] = Point(463, 90);
	pp2[2] = Point(500, 171);
	pp2[3] = Point(421, 194);
	pp2[4] = Point(338, 141);
	const Point* pts[3] = { pp[0],pp[1],pp2 };  //pts变量的生成
	int npts[] = { 6,6,5 };  //顶点个数数组的生成
	fillPoly(img, pts, npts, 3, Scalar(125, 125, 125), 8);  //绘制3个多边形
	//生成文字
	putText(img, "Learn OpenCV 4", Point(100, 400), 2, 1, Scalar(255, 255, 255));
	imshow("", img);

	waitKey(0);
	return 0;
}

你可能感兴趣的:(OpenCV,计算机视觉,C++,opencv)