OpenCV4教程——3.3 绘制直线

目标

我们将学习在 OpenCV 中进行绘制操作,我们的目标包括:

  • 绘制直线。使用 line() 函数。

绘制直线

Draws a line segment connecting two points.

头文件

#include <opencv2/imgproc.hpp>

原型

C++/Java
void cv::line(InputOutputArray img,
              Point 	       pt1,
              Point 	       pt2,
              const Scalar &   color,
              int              thickness = 1,
              int              lineType = LINE_8,
              int              shift = 0)
Python:
img	= cv.line(	img, pt1, pt2, color[, thickness[, lineType[, shift]]]	)

输入参数

imgImage.
pt1First point of the line segment.
pt2Second point of the line segment.
colorLine color.
thicknessLine thickness.
lineTypeType of the line. See LineTypes.
shiftNumber of fractional bits in the point coordinates.

例子

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

#define w 400

using namespace cv;

void MyLine(Mat img, Point start, Point end);

int main( void ){
    char atom_window[] = "Drawing 1: Atom";
    char rook_window[] = "Drawing 2: Rook";

    Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
    MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
    MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
    MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
    MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );

    imshow( atom_window, atom_image );

    waitKey( 0 );

    return 0;
}

void MyLine( Mat img, Point start, Point end ) {
  int thickness = 2;
  int lineType = LINE_8;
  line( img,
    start,
    end,
    Scalar( 0, 0, 0 ),
    thickness,
    lineType );
}

版权声明:本文为justidle原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。