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 );
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章