如何使用 C++ 在 OpenCV 中绘制矩形?
绘制矩形需要四个点。观察下图。

图中有四个点 x1、x2、y1 和 y2。这四个点构成了四个坐标。要使用 OpenCV 来绘制矩形,我们必须定义这些点并使用一个矩阵显示所需矩形。我们必须声明其他相关值,比如线条颜色和线条宽度。
此方法的基本语法如下
语法
rectangle(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(40, 40);//Declaring the starting coordinate//
Point ending(160, 100);//Declaring the ending coordinate
Scalar line_Color(0, 0, 0);//Color of the rectangle//
int thickness = 2;//thickens of the line//
namedWindow("whiteMatrix");//Declaring a window to show the rectangle//
rectangle(whiteMatrix, starting, ending, line_Color, thickness);//Drawing the rectangle//
imshow("WhiteMatrix", whiteMatrix);//Showing the rectangle//
waitKey(0);//Waiting for Keystroke
return 0;
}输出

广告
数据结构
联网
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP