OpenCV4教程——3.3 绘制直线

本文将指导你如何在OpenCV中使用line()函数绘制直线。通过详细解析函数参数,包括点坐标、颜色、厚度等,以及提供C++和Python的代码示例,帮助读者掌握基本的图像处理技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目标

我们将学习在 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 );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值