Image Processing (imgproc module)
Basic
- Basic Drawing
- Random generator and text with OpenCV
- Smoothing Images
- Eroding and Dilating
- More Morphology Transformations
- Hit-or-Miss
- Extract horizontal and vertical lines by using morphological operations
- Image Pyramids
- Basic Thresholding Operations
- Thresholding Operations using inRange
基本绘图
使用 OpenCV 的随机生成器和文本
平滑图像
腐蚀和膨胀
更多形态转换
击中或没打中
使用形态学运算提取水平和垂直线
图像金字塔
基本阈值操作
使用 inRange 进行阈值操作
Basic Drawing
Goals
在本教程中,您将学习如何:
使用 OpenCV 函数 line() 画一条线
使用 OpenCV 函数 ellipse() 绘制椭圆
使用 OpenCV 函数 rectangle() 绘制一个矩形
使用 OpenCV 函数 circle() 画一个圆
使用 OpenCV 函数 fillPoly() 绘制填充多边形
OpenCV Theory
在本教程中,我们将大量使用两种结构: cv::Point 和 cv::Scalar :
Point
它表示一个 2D 点,由其图像坐标 x 和 y 指定。 我们可以将其定义为:
Point pt;
pt.x = 10;
pt.y = 8;
or
Scalar
表示一个 4 元素向量。 Scalar 类型在 OpenCV 中广泛用于传递像素值。
在本教程中,我们将广泛使用它来表示 BGR 颜色值(3 个参数)。 如果不打算使用最后一个参数,则无需定义它。
让我们看一个例子,如果我们被要求提供颜色参数并且我们给出:
Scalar( a, b, c )
我们将定义 BGR 颜色,例如:Blue = a、Green = b 和 Red = c
Code
此代码位于您的 OpenCV 示例文件夹中。 否则你可以从这里获取raw.githubusercontent.com
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#define w 400
using namespace cv;
void MyEllipse( Mat img, double angle );
void MyFilledCircle( Mat img, Point center );
void MyPolygon( Mat img );
void MyLine( Mat img, Point start, Point end );
int main( void ){
char atom_window[] = "Drawing 1: Atom";
char rook_window[] = "Drawing 2: Rook";
Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 );
MyFilledCircle( atom_image, Point( w/2, w/2) );
MyPolygon( rook_image );
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
FILLED,
LINE_8 );
MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
imshow( atom_window, atom_image );
moveWindow( atom_window, 0, 200 );
imshow( rook_window, rook_image );
moveWindow( rook_window, w, 200 );
waitKey( 0 );
return(0);
}
void MyEllipse( Mat img, double angle )
{
int thickness = 2;
int lineType = 8;
ellipse( img,
Point( w/2, w/2 ),
Size( w/4, w/16 ),
angle,
0,
360,
Scalar( 255, 0, 0 ),
thickness,
lineType );
}
void MyFilledCircle( Mat img, Point center )
{
circle( img,
center,
w/32,
Scalar( 0, 0, 255 ),
FILLED,
LINE_8 );
}
void MyPolygon( Mat img )
{
int lineType = LINE_8;
Point rook_points[1][20];
rook_points[0][0] = Point( w/4, 7*w/8 );
rook_points[0][1] = Point( 3*w/4, 7*w/8 );
rook_points[0][2] = Point( 3*w/4, 13*w/16 );
rook_points[0][3] = Point( 11*w/16, 13*w/16 );
rook_points[0][4] = Point( 19*w/32, 3*w/8 );
rook_points[0][5] = Point( 3*w/4, 3*w/8 );
rook_points[0][6] = Point( 3*w/4, w/8 );
rook_points[0][7] = Point( 26*w/40, w/8 );
rook_points[0][8] = Point( 26*w/40, w/4 );
rook_points[0][9] = Point( 22*w/40, w/4 );
rook_points[0][10] = Point( 22*w/40, w/8 );
rook_points[0][11] = Point( 18*w/40, w/8 );
rook_points[0][12] = Point( 18*w/40, w/4 );
rook_points[0][13] = Point( 14*w/40, w/4 );
rook_points[0][14] = Point( 14*w/40, w/8 );
rook_points[0][15] = Point( w/4, w/8 );
rook_points[0][16] = Point( w/4, 3*w/8 );
rook_points[0][17] = Point( 13*w/32, 3*w/8 );
rook_points[0][18] = Point( 5*w/16, 13*w/16 );
rook_points[0][19] = Point( w/4, 13*w/16 );
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
fillPoly( img,
ppt,
npt,
1,
Scalar( 255, 255, 255 ),
lineType );
}
void MyLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = LINE_8;
line( img,
start,
end,
Scalar( 0, 0, 0 ),
thickness,
lineType );
}
Explanation
由于我们计划绘制两个示例(一个atom原子和一个rook车),我们必须创建两个图像和两个窗口来显示它们。
char atom_window[] = "Drawing 1: Atom";
char rook_window[] = "Drawing 2: Rook";
Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
我们创建了绘制不同几何形状的函数。 例如,为了绘制原子,我们使用了 MyEllipse 和 MyFilledCircle:
MyEllipse( atom_image, 90 ); //四个方向的椭圆
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 );
MyFilledCircle( atom_image, Point( w/2, w/2) );
为了绘制车,我们使用了 MyLine、rectangle和 MyPolygon:
MyPolygon( rook_image );
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
FILLED,
LINE_8 );
MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
让我们检查一下每个函数内部的内容:
MyLine
void MyLine( Mat img, Point start, Point end )
{
int thickness = 2;
int lineType = LINE_8;
line( img,
start,
end,
Scalar( 0, 0, 0 ),
thickness,
lineType );
}
如我们所见,MyLine 只是调用函数 line() ,它执行以下操作:
从 Point start 到 Point end 画一条线
该行在图像 img 中显示
线条颜色由 (0, 0, 0) 定义,它是对应于黑色的 RGB 值
线条粗细设置为粗细(在本例中为 2)
线是 8 连接线(lineType = 8)
MyEllipse
void MyEllipse( Mat img, double angle )
{
int thickness = 2;
int lineType = 8;
ellipse( img,
Point( w/2, w/2 ),
Size( w/4, w/16 ),
angle,
0,
360,
Scalar( 255, 0, 0 ),
thickness,
lineType );
}
从上面的代码中,我们可以观察到函数 ellipse() 绘制了一个椭圆,这样:
椭圆在图像 img 中显示
椭圆中心位于点 (w/2, w/2) 并被包围在一个大小为 (w/4, w/16) 的盒子中
椭圆是旋转的角度度数
椭圆延伸 0 到 360 度之间的弧
图形的颜色为 ( 255, 0, 0 ),表示 BGR 值中的蓝色。
椭圆的厚度为 2。
MyFilledCircle
void MyFilledCircle( Mat img, Point center )
{
circle( img,
center,
w/32,
Scalar( 0, 0, 255 ),
FILLED,
LINE_8 );
}
与 ellipse 函数类似,我们可以观察到 circle 作为参数接收:
将显示圆圈的图像(img)
圆的中心表示为点中心
圆的半径:w/32
圆圈的颜色: ( 0, 0, 255 ) 在 BGR 中表示红色
由于厚度 = -1,圆形将被填充。
void MyPolygon( Mat img )
{
int lineType = LINE_8;
Point rook_points[1][20];
rook_points[0][0] = Point( w/4, 7*w/8 );
rook_points[0][1] = Point( 3*w/4, 7*w/8 );
rook_points[0][2] = Point( 3*w/4, 13*w/16 );
rook_points[0][3] = Point( 11*w/16, 13*w/16 );
rook_points[0][4] = Point( 19*w/32, 3*w/8 );
rook_points[0][5] = Point( 3*w/4, 3*w/8 );
rook_points[0][6] = Point( 3*w/4, w/8 );
rook_points[0][7] = Point( 26*w/40, w/8 );
rook_points[0][8] = Point( 26*w/40, w/4 );
rook_points[0][9] = Point( 22*w/40, w/4 );
rook_points[0][10] = Point( 22*w/40, w/8 );
rook_points[0][11] = Point( 18*w/40, w/8 );
rook_points[0][12] = Point( 18*w/40, w/4 );
rook_points[0][13] = Point( 14*w/40, w/4 );
rook_points[0][14] = Point( 14*w/40, w/8 );
rook_points[0][15] = Point( w/4, w/8 );
rook_points[0][16] = Point( w/4, 3*w/8 );
rook_points[0][17] = Point( 13*w/32, 3*w/8 );
rook_points[0][18] = Point( 5*w/16, 13*w/16 );
rook_points[0][19] = Point( w/4, 13*w/16 );
const Point* ppt[1] = { rook_points[0] };
int npt[] = { 20 };
fillPoly( img,
ppt,
npt,
1,
Scalar( 255, 255, 255 ),
lineType );
}
要绘制填充多边形,我们使用函数 fillPoly() 。 我们注意到:
多边形将在 img 上绘制
多边形的顶点是ppt中的点集
多边形的颜色由 (255, 255, 255) 定义,即白色的 BGR 值
rectangle
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
FILLED,
LINE_8 );
最后我们有了 cv::rectangle 函数(我们没有为这个东西创建一个特殊的函数)。 我们注意到:
矩形将在 rook_image 上绘制
矩形的两个相对顶点由 (0, 7*w/8) 和 (w, w) 定义
矩形的颜色由 (0, 255, 255) 给出,它是黄色的 BGR 值
由于厚度值由 FILLED (-1) 给出,矩形将被填充。
Result
编译和运行你的程序应该会给你这样的结果:
OpenCV: Basic Drawinghttps://docs.opencv.org/4.5.5/d3/d96/tutorial_basic_geometric_drawing.html