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

输入参数

img Image.
pt1 First point of the line segment.
pt2 Second point of the line segment.
color Line color.
thickness Line thickness.
lineType Type of the line. See LineTypes.
shift Number 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 );
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章