通过C++在OpenCV中如何画线?
若要画一条线,我们需要两点 - 起点和终点。我们还需要一个画布来画线。
使用 OpenCV,我们的画布中的矩阵,我们需要定义线的起点和终点。我们还需要为线分配一个颜色。线的粗细也必须说明。如果我们要使用 OpenCV 画一条线,我们需要声明一个矩阵、两个点、颜色和线宽。
使用 OpenCV,我们必须包含<imgproc.hpp> 头文件,因为 line() 函数在此头文件中定义。
该方法的基本语法如下 -
语法
line(whiteMatrix, starting, ending, line_Color, thickness);
以下程序演示如何使用 OpenCV 在图像上画一条线 -
示例
#include<iostream>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main() {
Mat whiteMatrix(200, 200, CV_8UC3, Scalar(255, 255, 255));//Declaring a white matrix//
Point starting(50, 50);//Starting Point of the line
Point ending(150, 150);//Ending Point of the line
Scalar line_Color(0, 0, 0);//Color of the line
int thickness = 2;//thickens of the line
namedWindow("GrayImage");//Declaring a window to show the line
line(whiteMatrix, starting, ending, line_Color, thickness);//using line() function to draw the line//
imshow("GrayImage", whiteMatrix);//showing the line//
waitKey(0);//Waiting for KeyStroke
return 0;
}输出

广告
数据结构
网络技术
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP