0.概述
本文档简单记录OpenCV中不同图形的绘制.
1.绘图
1.1 线条绘制
line(img, pt1, pt2, color, thickness, lineType, shift)
- img:这是图像文件。
- pt1:线段的起点。线段两端的第一个点。它是两个坐标(x 坐标、y 坐标))的元组。
- pt2:线段的终点。线段两端的第二个点。它是两个坐标(x 坐标、y 坐标))的元组。
- color:要绘制的线的颜色。它是一个代表 3 种颜色(B、G、R)的元组,即(蓝色、绿色、红色)。
- thickness:绘制的线的粗细。
- lineType:行的类型。有 3 种类型的线路:
- LINE_4:使用 4 个连接的 Bresenham 算法绘制线条。
- LINE_8:使用 8 个连接的 Bresenham 算法绘制的线。
- LINE_AA:它绘制使用高斯滤波器形成的抗锯齿线。
- nshift:它是点坐标中的小数位数
1)在自定义的背景上绘制线条
// C++ program for the above approach
#include <iostream>
#include <opencv2/core/core.hpp>
// Library to include for
// drawing shapes
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
// Driver Code
int main(int argc, char** argv)
{
// Create a blank image of size
// (500 x 500) with black
// background (B, G, R) : (0, 0, 0)
Mat image(500, 500, CV_8UC3,
Scalar(0, 0, 0));
// Check if the image is created
// successfully
if (!image.data) {
cout << "Could not open or find"
<< " the image";
return 0;
}
Point p1(0, 0), p2(100, 0);
Point p3(200, 0), p4(500, 500);
int thickness = 2;
// Line drawn using 8 connected
// Bresenham algorithm
line(image, p1, p4, Scalar(255, 0, 0),
thickness, LINE_8);
// Line drawn using 4 connected
// Bresenham algorithm
line(image, p2, p4, Scalar(0, 255, 0),
thickness, LINE_4);
// Antialiased line
line(image, p3, p4, Scalar(0, 0, 255),
thickness, LINE_AA);
// Show our image inside window
imshow("Output", image);
waitKey(0);
return 0;
}
2)在加载的图片上绘制线条
// C++ program for the above approach
#include <iostream>
#include <opencv2/core/core.hpp>
// Library to include for
// drawing shapes
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
// Driver code
int main(int argc, char** argv)
{
// Path of the image file
Mat image = imread(
"C:/Users/harsh/Downloads/geeks.png",
IMREAD_COLOR);
// Check if the image is loaded
// successfully
if (!image.data) {
std::cout << "Could not open or "
"find the image";
return 0;
}
Point p1(0, 0), p2(250, 250);
int thickness = 2;
// Line drawn using 8 connected
// Bresenham algorithm
line(image, p1, p2, Scalar(255, 0, 0),
thickness, LINE_8);
// Show our image inside window
imshow("Output", image);
waitKey(0);
return 0;
}
1.2 椭圆绘制
ellipse(image, centerCoordinates, axesLength, angle, startAngle, endAngle, color, thickness, lineType, shift)
- image:它是要绘制椭圆的图像。
- centerCoordinates: 椭圆中心的坐标。(两个坐标的元组(X 坐标、Y 坐标))
- axesLength:包含椭圆的长轴和短轴(长轴长度、短轴长度)的元组。
- angle:椭圆旋转角度(以度为单位)。
- startAngle: 椭圆弧的起始角度(度)。
- endAngle:椭圆弧的结束角(度)。
- color:它是要绘制的椭圆边界线的颜色。表示 3 种颜色(B、G、R)的元组,即(蓝色、绿色、红色)。
- thickness:它是椭圆边界线的粗细,单位为 px。-1 px 的厚度将按指定的颜色填充椭圆形状。
- lineType:行的类型。有 3 种类型的线路:
- LINE_4:使用 4 个连接的 Bresenham 算法绘制线条。
- LINE_8:使用 8 个连接的 Bresenham 算法绘制线条。
- LINE_AA:它使用高斯滤波器绘制抗锯齿线。
- shift:点坐标中的小数位数。
a) 自形成的背景图像上绘制椭圆
// C++ program to demonstrating ellipse
// over a self-formed background image
#include <iostream>
#include <opencv2/core/core.hpp>
// Drawing shapes
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
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;
}
// Drawing the ellipse
ellipse(image, Point(256, 256),
Size(100, 50), 0, 0,
360, Scalar(0, 255, 255),
-1, LINE_AA);
// Showing image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
2)加载图像上的矩形,GFG 徽标周围绘制一个椭圆
// C++ program to demonstrate rectangle
// over a loaded image with an ellipse
// around the GFG logo
#include <iostream>
#include <opencv2/core/core.hpp>
// Drawing shapes
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
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;
}
// Drawing the ellipse
ellipse(image, Point(115, 110),
Size(105, 55), 0, 0,
360, Scalar(0, 255, 255),
1, LINE_AA);
// Show our image inside a window
imshow("Output", image);
waitKey(0);
return 0;
}
1.3 矩形绘制
rectangle( img, pt1, pt2, color, thickness, line Type, shift)
- img:它是要在其上绘制矩形的图像。
- 开始(pt1):它是矩形的左上角,表示为两个坐标的元组,即(x-坐标,y-坐标)。
- 结束(pt2):它是矩形的右下角,表示为两个坐标的元组,即(x-坐标,y-坐标)。
- color:它是要绘制的矩形边界线的颜色。表示 3 种颜色(B、G、R)的元组,即(蓝色、绿色、红色)。
- thickness:它是矩形边界线的粗细,单位为 px。px 为-1的厚度将按指定的颜色填充矩形形状。
- lineType:行的类型。有 3 种类型的线路:
- LINE_4:使用 4 个连接的 Bresenham 算法绘制线条。
- LINE_8:使用 8 个连接的 Bresenham 算法绘制线条。
- LINE_AA:它使用高斯滤波器绘制抗锯齿线。
- shift:点坐标中的小数位数。
1) 自形成的背景图像上绘制矩形
// C++ program to demonstrate rectangle
// over a self-formed background image
#include <iostream>
#include <opencv2/core/core.hpp>
// Drawing shapes
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
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;
}
2)在logo外绘制矩形
// C++ program to demonstrate rectangle
// over a loaded image of GFG logo
#include <iostream>
#include <opencv2/core/core.hpp>
// Drawing shapes
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
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++ program to demonstrate rectangle
// filled with any color
#include <iostream>
#include <opencv2/core/core.hpp>
// Drawing shapes
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
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;
}