在本文中,任务是使用C++中的OpenCV绘制矩形。将使用OpenCV C++库中的rectangle()函数。
句法:
rectangle( img, pt1, pt2, color, thickness, line Type, shift)
参数:
- 图像:这是要在其上绘制矩形的图像。
- start(pt1):它是矩形的左上角,表示为两个坐标(x坐标,y坐标)的元组。
- end(pt2):它是矩形的右下角,表示为两个坐标(x坐标,y坐标)的元组。
- color:它是要绘制的矩形的边界线的颜色。表示3种颜色(B,G,R)(即(蓝色,绿色,红色))的元组。
- 厚度:它是矩形边界线的粗细(以px为单位) 。 -1 px的厚度将通过指定的颜色填充矩形形状。
- lineType:线的类型。有3种类型的线:
- LINE_4:使用4个连接的Bresenham算法绘制了线。
- LINE_8:使用8个连接的Bresenham算法绘制了线。
- LINE_AA:绘制使用高斯滤波器形成的抗锯齿线。
- shift:点坐标中的小数位数。
返回值:返回图像。
程序1:
下面是C++程序,演示了如何在自形成的背景图像上绘制矩形:
C++
// C++ program to demonstrate rectangle
// over a self-formed background image
#include
#include
// Drawing shapes
#include
#include
using namespace cv;
using namespace std;
// Driver Code
int main(int argc, char** argv)
{
// Creating a blank image with
// white background
Mat image(500, 500, CV_8UC3,
Scalar(255, 255, 255));
// Check if the image is created
// successfully or not
if (!image.data) {
std::cout << "Could not open or "
<< "find the image\n";
return 0;
}
// Top Left Corner
Point p1(30, 30);
// Bottom Right Corner
Point p2(255, 255);
int thickness = 2;
// Drawing the Rectangle
rectangle(image, p1, p2,
Scalar(255, 0, 0),
thickness, LINE_8);
// Show our image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
C++
// C++ program to demonstrate rectangle
// over a loaded image of GFG logo
#include
#include
// Drawing shapes
#include
#include
using namespace cv;
using namespace std;
// Driver Code
int main(int argc, char** argv)
{
// Reading the Image
Mat image = imread("C:/Users/harsh/Downloads/geeks.png",
IMREAD_COLOR);
// Check if the image is created
// successfully or not
if (!image.data) {
std::cout << "Could not open or "
<< "find the image\n";
return 0;
}
// Top Left Coordinates
Point p1(30, 70);
// Bottom Right Coordinates
Point p2(115, 155);
int thickness = 2;
// Drawing the Rectangle
rectangle(image, p1, p2,
Scalar(255, 0, 0),
thickness, LINE_8);
// Show our image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
C++
// C++ program to demonstrate rectangle
// filled with any color
#include
#include
// Drawing shapes
#include
#include
using namespace cv;
using namespace std;
// Driver Code
int main(int argc, char** argv)
{
// Creating a blank image with
// white background
Mat image(500, 500, CV_8UC3,
Scalar(255, 255, 255));
// Check if the image is created
// successfully or not
if (!image.data) {
std::cout << "Could not open or "
<< "find the image\n";
return 0;
}
// Top Left Corner
Point p1(30, 30);
// Bottom Right Corner
Point p2(255, 255);
int thickness = -1;
// Drawing the Rectangle
rectangle(image, p1, p2,
Scalar(0, 255, 0),
thickness, LINE_8);
// Show our image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
输出:
程式2:
下面是C++程序,演示了如何在GFG徽标的图像上绘制矩形:
C++
// C++ program to demonstrate rectangle
// over a loaded image of GFG logo
#include
#include
// Drawing shapes
#include
#include
using namespace cv;
using namespace std;
// Driver Code
int main(int argc, char** argv)
{
// Reading the Image
Mat image = imread("C:/Users/harsh/Downloads/geeks.png",
IMREAD_COLOR);
// Check if the image is created
// successfully or not
if (!image.data) {
std::cout << "Could not open or "
<< "find the image\n";
return 0;
}
// Top Left Coordinates
Point p1(30, 70);
// Bottom Right Coordinates
Point p2(115, 155);
int thickness = 2;
// Drawing the Rectangle
rectangle(image, p1, p2,
Scalar(255, 0, 0),
thickness, LINE_8);
// Show our image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
输出:
程序3:
下面是C++程序,演示了如何绘制一个填充有颜色的矩形:
C++
// C++ program to demonstrate rectangle
// filled with any color
#include
#include
// Drawing shapes
#include
#include
using namespace cv;
using namespace std;
// Driver Code
int main(int argc, char** argv)
{
// Creating a blank image with
// white background
Mat image(500, 500, CV_8UC3,
Scalar(255, 255, 255));
// Check if the image is created
// successfully or not
if (!image.data) {
std::cout << "Could not open or "
<< "find the image\n";
return 0;
}
// Top Left Corner
Point p1(30, 30);
// Bottom Right Corner
Point p2(255, 255);
int thickness = -1;
// Drawing the Rectangle
rectangle(image, p1, p2,
Scalar(0, 255, 0),
thickness, LINE_8);
// Show our image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
输出:
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。