透過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;
}

輸出

更新於: 2021 年 3 月 10 日

1K+ 觀看

開啟你的 職業生涯

完成學習,獲取認證

開始
廣告
© . All rights reserved.