下一个教程: 使用 OpenCV 创建随机生成器和文本
原作者 | Ana Huamán |
---|---|
兼容性 | OpenCV >= 3.0 |
目标
在本教程中,您将学习如何
- 使用 OpenCV 函数 line() 绘制直线
- 使用 OpenCV 函数 ellipse() 绘制椭圆
- 使用 OpenCV 函数 rectangle() 绘制矩形
- 使用 OpenCV 函数 circle() 绘制圆形
- 使用 OpenCV 函数 fillPoly() 绘制填充多边形
OpenCV 理论
在本教程中,我们将大量使用两种结构:cv::Point 和 cv::Scalar:
点
它代表一个二维点,由其图像坐标 x 和 y 指定:
- C++
Point pt;
pt.x = 10;
pt.y = 8;
- Java
Point pt = new Point();
pt.x = 10;
pt.y = 8;
或
- C++
Point pt = Point(10, 8);
- Java
Point pt = new Point(10, 8);
标量
- 代表一个 4 元素向量。Scalar 类型在 OpenCV 中广泛用于传递像素值。
- 在本教程中,我们将广泛使用它来表示 BGR 颜色值(3 个参数)。如果不使用最后一个参数,就没有必要定义它。
- 我们来看一个例子,如果要求我们提供一个颜色参数,我们给出的是
Scalar( a, b, c )
我们将定义一种 BGR 颜色,例如 蓝色 = a,绿色 = b,红色 = c
代码
- C++
该代码位于 OpenCV 示例文件夹中。或者您可以从此处获取
#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 );
}
- Java
该代码位于 OpenCV 示例文件夹中。或者您可以从此处获取
import org.opencv.core.*;
import org.opencv.core.Point;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import java.util.*;
import java.util.List;
class GeometricDrawingRun{
private static final int W = 400;
public void run(){
String atom_window = "Drawing 1: Atom";
String rook_window = "Drawing 2: Rook";
Mat atom_image = Mat.zeros( W, W, CvType.CV_8UC3 );
Mat rook_image = Mat.zeros( W, W, CvType.CV_8UC3 );
MyEllipse( atom_image, 90.0 );
MyEllipse( atom_image, 0.0 );
MyEllipse( atom_image, 45.0 );
MyEllipse( atom_image, -45.0 );
MyFilledCircle( atom_image, new Point( W/2, W/2) );
MyPolygon( rook_image );
Imgproc.rectangle( rook_image,
new Point( 0, 7*W/8 ),
new Point( W, W),
new Scalar( 0, 255, 255 ),
-1,
8,
0 );
MyLine( rook_image, new Point( 0, 15*W/16 ), new Point( W, 15*W/16 ) );
MyLine( rook_image, new Point( W/4, 7*W/8 ), new Point( W/4, W ) );
MyLine( rook_image, new Point( W/2, 7*W/8 ), new Point( W/2, W ) );
MyLine( rook_image, new Point( 3*W/4, 7*W/8 ), new Point( 3*W/4, W ) );
HighGui.imshow( atom_window, atom_image );
HighGui.moveWindow( atom_window, 0, 200 );
HighGui.imshow( rook_window, rook_image );
HighGui.moveWindow( rook_window, W, 200 );
HighGui.waitKey( 0 );
System.exit(0);
}
private void MyEllipse( Mat img, double angle ) {
int thickness = 2;
int lineType = 8;
int shift = 0;
Imgproc.ellipse( img,
new Point( W/2, W/2 ),
new Size( W/4, W/16 ),
angle,
0.0,
360.0,
new Scalar( 255, 0, 0 ),
thickness,
lineType,
shift );
}
private void MyFilledCircle( Mat img, Point center ) {
int thickness = -1;
int lineType = 8;
int shift = 0;
Imgproc.circle( img,
center,
W/32,
new Scalar( 0, 0, 255 ),
thickness,
lineType,
shift );
}
private void MyPolygon( Mat img ) {
int lineType = 8;
int shift = 0;
Point[] rook_points = new Point[20];
rook_points[0] = new Point( W/4, 7*W/8 );
rook_points[1] = new Point( 3*W/4, 7*W/8 );
rook_points[2] = new Point( 3*W/4, 13*W/16 );
rook_points[3] = new Point( 11*W/16, 13*W/16 );
rook_points[4] = new Point( 19*W/32, 3*W/8 );
rook_points[5] = new Point( 3*W/4, 3*W/8 );
rook_points[6] = new Point( 3*W/4, W/8 );
rook_points[7] = new Point( 26*W/40, W/8 );
rook_points[8] = new Point( 26*W/40, W/4 );
rook_points[9] = new Point( 22*W/40, W/4 );
rook_points[10] = new Point( 22*W/40, W/8 );
rook_points[11] = new Point( 18*W/40, W/8 );
rook_points[12] = new Point( 18*W/40, W/4 );
rook_points[13] = new Point( 14*W/40, W/4 );
rook_points[14] = new Point( 14*W/40, W/8 );
rook_points[15] = new Point( W/4, W/8 );
rook_points[16] = new Point( W/4, 3*W/8 );
rook_points[17] = new Point( 13*W/32, 3*W/8 );
rook_points[18] = new Point( 5*W/16, 13*W/16 );
rook_points[19] = new Point( W/4, 13*W/16 );
MatOfPoint matPt = new MatOfPoint();
matPt.fromArray(rook_points);
List<MatOfPoint> ppt = new ArrayList<MatOfPoint>();
ppt.add(matPt);
Imgproc.fillPoly(img,
ppt,
new Scalar( 255, 255, 255 ),
lineType,
shift,
new Point(0,0) );
}
private void MyLine( Mat img, Point start, Point end ) {
int thickness = 2;
int lineType = 8;
int shift = 0;
Imgproc.line( img,
start,
end,
new Scalar( 0, 0, 0 ),
thickness,
lineType,
shift );
}
}
public class BasicGeometricDrawing {
public static void main(String[] args) {
// 加载本地库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new GeometricDrawingRun().run();
}
}
- Python
该代码位于 OpenCV 示例文件夹中。或者您可以从此处获取
import cv2 为 cv
import numpy 为 np
W = 400
def my_ellipse(img, angle):
thickness = 2
line_type = 8
cv.ellipse(img、
(w // 2, w // 2)、
(w // 4, w // 16)、
angle、
0,
360,
(255, 0, 0),
thickness、
line_type)
def my_filled_circle(img, center):
thickness = -1
line_type = 8
cv.circle(img、
center、
W // 32,
(0, 0, 255),
thickness、
line_type)
def my_polygon(img):
line_type = 8
# 创建一些点
ppt = np.array([[W / 4, 7 * W / 8], [3 * W / 4, 7 * W / 8]、
[3 * W / 4, 13 * W / 16], [11 * W / 16, 13 * W / 16]、
[19 * W / 32,3 * W / 8],[3 * W / 4,3 * W / 8]、
[3 * W / 4,W / 8],[26 * W / 40,W / 8]、
[26 * w / 40,w / 4],[22 * w / 40,w / 4]、
[22 * W / 40,W / 8],[18 * W / 40,W / 8]、
[18*W/40,W/4],[14*W/40,W/4]、
[14*W/40,W/8],[W/4,W/8]、
[W / 4,3 * W / 8],[13 * W / 32,3 * W / 8]、
[5 * W / 16,13 * W / 16],[W / 4,13 * W / 16]],np.int32)
ppt = ppt.reshape((-1, 1, 2))
cv.fillPoly(img, [ppt], (255, 255, 255), line_type)
# 只绘制线条:
# cv.polylines(img, [ppt], True, (255, 0, 255), line_type)
def my_line(img, start, end):
thickness = 2
line_type = 8
cv.line(img、
start、
end、
(0, 0, 0),
thickness、
line_type)
atom_window = "Drawing 1: Atom" (绘制 1:原子
rook_window = "Drawing 2: Rook" (绘制 2:车
# 创建黑色空图像
size = W, W, 3
atom_image = np.zeros(size, dtype=np.uint8)
rook_image = np.zeros(size, dtype=np.uint8)
# 1.a. 创建椭圆
my_ellipse(atom_image, 90)
my_ellipse(atom_image, 0)
my_ellipse(atom_image, 45)
my_ellipse(atom_image, -45)
# 1.b. 创建圆
my_filled_circle(atom_image, (W // 2, W // 2))
# 2. 绘制车
# ------------------
# 2.a. 创建凸多边形
my_polygon(rook_image)
cv.rectangle(rook_image、
(0, 7 * W // 8),
(W, W)、
(0, 255, 255),
-1,
8)
# 2.c. 创建几行
my_line(rook_image, (0, 15 * W // 16), (W, 15 * W // 16))
my_line(rook_image, (W // 4, 7 * W // 8), (W // 4, W))
my_line(rook_image, (W // 2, 7 * W // 8), (W // 2, W))
my_line(rook_image, (3 * W // 4, 7 * W // 8), (3 * W // 4, W))
cv.imshow(atom_window, atom_image)
cv.moveWindow(atom_window, 0, 200)
cv.imshow(rook_window, rook_image)
cv.moveWindow(rook_window, W, 200)
cv.waitKey(0)
cv.destroyAllWindows()
说明
由于我们计划绘制两个示例(一个原子和一个车),因此必须创建两个图像和两个窗口来显示它们。
- C++
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 );
- Java
String atom_window = "Drawing 1: Atom";
String rook_window = "Drawing 2: Rook";
Mat atom_image = Mat.zeros( W, W, CvType.CV_8UC3 );
Mat rook_image = Mat.zeros( W, W, CvType.CV_8UC3 );
- Python
# 窗口名称
atom_window = "Drawing 1: Atom" 原子窗口
rook_window = "绘图 2: 车"
# 创建黑色空图像
size = W, W, 3
atom_image = np.zeros(size, dtype=np.uint8)
rook_image = np.zeros(size, dtype=np.uint8)
我们创建了绘制不同几何图形的函数。例如,为了绘制原子,我们使用了 MyEllipse 和 MyFilledCircle:
- C++
MyEllipse( atom_image, 90 );
MyEllipse( atom_image, 0 );
MyEllipse( atom_image, 45 );
MyEllipse( atom_image, -45 );
MyFilledCircle( atom_image, Point( w/2, w/2) );
- Java
MyEllipse( atom_image, 90.0 );
MyEllipse( atom_image, 0.0 );
MyEllipse( atom_image, 45.0 );
MyEllipse( atom_image, -45.0 );
MyFilledCircle( atom_image, new Point( W/2, W/2) );
- Python
# 1. 画一个简单的原子:
# -----------------------
# 1.a. 创建椭圆
my_ellipse(atom_image, 90)
my_ellipse(atom_image, 0)
my_ellipse(atom_image, 45)
my_ellipse(atom_image, -45)
# 1.b. 创建圆
my_filled_circle(atom_image, (W // 2, W // 2))
为了绘制车,我们使用了 MyLine、rectangle和 MyPolygon:
- C++
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 ) );
- Java
MyPolygon( rook_image );
Imgproc.rectangle( rook_image,
new Point( 0, 7*W/8 ),
new Point( W, W),
new Scalar( 0, 255, 255 ),
-1,
8,
0 );
MyLine( rook_image, new Point( 0, 15*W/16 ), new Point( W, 15*W/16 ) );
MyLine( rook_image, new Point( W/4, 7*W/8 ), new Point( W/4, W ) );
MyLine( rook_image, new Point( W/2, 7*W/8 ), new Point( W/2, W ) );
MyLine( rook_image, new Point( 3*W/4, 7*W/8 ), new Point( 3*W/4, W ) );
- Python
# 2. 画一个车
# ------------------
# 2.a. 创建一个凸多边形
my_polygon(rook_image)
cv.rectangle(rook_image、
(0, 7 * W // 8),
(W, W)、
(0, 255, 255),
-1,
8)
# 2.c. 创建几行
my_line(rook_image, (0, 15 * W // 16), (W, 15 * W // 16))
my_line(rook_image, (W // 4, 7 * W // 8), (W // 4, W))
my_line(rook_image, (W // 2, 7 * W // 8), (W // 2, W))
my_line(rook_image, (3 * W // 4, 7 * W // 8), (3 * W // 4, W))
让我们检查一下这些函数的内部结构:
MyLine
- C++
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 );
}
- Java
private void MyLine( Mat img, Point start, Point end ) {
int thickness = 2;
int lineType = 8;
int shift = 0;
Imgproc.line( img,
start,
end,
new Scalar( 0, 0, 0 ),
thickness,
lineType,
shift );
}
- Python
def my_line(img, start, end):
thickness = 2
line_type = 8
cv.line(img,
start,
end,
(0, 0, 0),
thickness,
line_type)
- 我们可以看到,MyLine 只是调用了函数 line() ,它的功能如下:
- 绘制一条从点开始到点结束的直线
- 直线显示在图像 img 中
- 线条颜色由 ( 0, 0, 0 ) 定义,它是与黑色相对应的 RGB 值
- 线条粗细设置为 thickness(本例中为 2)
- 线条为 8 连线(lineType = 8)
MyEllipse
- C++
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 );
}
- Java
private void MyEllipse( Mat img, double angle ) {
int thickness = 2;
int lineType = 8;
int shift = 0;
Imgproc.ellipse( img,
new Point( W/2, W/2 ),
new Size( W/4, W/16 ),
angle,
0.0,
360.0,
new Scalar( 255, 0, 0 ),
thickness,
lineType,
shift );
}
- Python
def my_ellipse(img, angle):
thickness = 2
line_type = 8
cv.ellipse(img,
(W // 2, W // 2),
(W // 4, W // 16),
angle,
0,
360,
(255, 0, 0),
thickness,
line_type)
- 从上面的代码中,我们可以看到函数 ellipse() 是这样绘制一个椭圆的:
- 椭圆显示在图像 img 中
- 椭圆中心位于点 (w/2, w/2),并被围在一个大小为 (w/4, w/16) 的框中
- 椭圆旋转了一个角度
- 椭圆的弧度在 0 和 360 度之间
- 图形的颜色为 ( 255, 0, 0 ),即 BGR 值中的蓝色。
- 椭圆的厚度为 2。
MyFilledCircle
- C++
void MyFilledCircle( Mat img, Point center )
{
circle( img,
center,
w/32,
Scalar( 0, 0, 255 ),
FILLED,
LINE_8 );
}
- Java
private void MyFilledCircle( Mat img, Point center ) {
int thickness = -1;
int lineType = 8;
int shift = 0;
Imgproc.circle( img,
center,
W/32,
new Scalar( 0, 0, 255 ),
thickness,
lineType,
shift );
}
- Python
def my_filled_circle(img, center):
thickness = -1
line_type = 8
cv.circle(img,
center,
W // 32,
(0, 0, 255),
thickness,
line_type)
- 与椭圆函数类似,我们可以看到圆也接收参数:
- 显示圆的图像 (img)
- 圆心以点中心表示
- 圆的半径:w/32
- 圆的颜色:( 0, 0, 255 ),在 BGR 中表示红色
- 由于厚度 =-1,因此绘制的圆将是填充的。
MyPolygon
- C++
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 );
}
- Java
private void MyPolygon( Mat img ) {
int lineType = 8;
int shift = 0;
Point[] rook_points = new Point[20];
rook_points[0] = new Point( W/4, 7*W/8 );
rook_points[1] = new Point( 3*W/4, 7*W/8 );
rook_points[2] = new Point( 3*W/4, 13*W/16 );
rook_points[3] = new Point( 11*W/16, 13*W/16 );
rook_points[4] = new Point( 19*W/32, 3*W/8 );
rook_points[5] = new Point( 3*W/4, 3*W/8 );
rook_points[6] = new Point( 3*W/4, W/8 );
rook_points[7] = new Point( 26*W/40, W/8 );
rook_points[8] = new Point( 26*W/40, W/4 );
rook_points[9] = new Point( 22*W/40, W/4 );
rook_points[10] = new Point( 22*W/40, W/8 );
rook_points[11] = new Point( 18*W/40, W/8 );
rook_points[12] = new Point( 18*W/40, W/4 );
rook_points[13] = new Point( 14*W/40, W/4 );
rook_points[14] = new Point( 14*W/40, W/8 );
rook_points[15] = new Point( W/4, W/8 );
rook_points[16] = new Point( W/4, 3*W/8 );
rook_points[17] = new Point( 13*W/32, 3*W/8 );
rook_points[18] = new Point( 5*W/16, 13*W/16 );
rook_points[19] = new Point( W/4, 13*W/16 );
MatOfPoint matPt = new MatOfPoint();
matPt.fromArray(rook_points);
List<MatOfPoint> ppt = new ArrayList<MatOfPoint>();
ppt.add(matPt);
Imgproc.fillPoly(img,
ppt,
new Scalar( 255, 255, 255 ),
lineType,
shift,
new Point(0,0) );
}
- Python
def my_polygon(img):
line_type = 8
# 创建一些点
ppt = np.array([[W / 4, 7 * W / 8], [3 * W / 4, 7 * W / 8]、
[3 * W / 4, 13 * W / 16], [11 * W / 16, 13 * W / 16]、
[19 * W / 32,3 * W / 8],[3 * W / 4,3 * W / 8]、
[3 * W / 4,W / 8],[26 * W / 40,W / 8]、
[26 * w / 40,w / 4],[22 * w / 40,w / 4]、
[22 * W / 40,W / 8],[18 * W / 40,W / 8]、
[18*W/40,W/4],[14*W/40,W/4]、
[14*W/40,W/8],[W/4,W/8]、
[W / 4,3 * W / 8],[13 * W / 32,3 * W / 8]、
[5 * W / 16,13 * W / 16],[W / 4,13 * W / 16]],np.int32)
ppt = ppt.reshape((-1, 1, 2))
cv.fillPoly(img, [ppt], (255, 255, 255), line_type)
# 只绘制线条:
# cv.polylines(img, [ppt], True, (255, 0, 255), line_type)
- 要绘制填充多边形,我们使用函数 fillPoly() 。我们注意到
- 多边形将绘制在 img 上
- 多边形的顶点是 ppt 中的点集
- 多边形的颜色由 ( 255, 255, 255 ) 定义,这是白色的 BGR 值
Rectangle
- C++
rectangle( rook_image,
Point( 0, 7*w/8 ),
Point( w, w),
Scalar( 0, 255, 255 ),
FILLED,
LINE_8 );
- Java
Imgproc.rectangle( rook_image,
new Point( 0, 7*W/8 ),
new Point( W, W),
new Scalar( 0, 255, 255 ),
-1,
8,
0 );
- Python
# 2.b. 创建矩形
cv.rectangle(rook_image、
(0, 7 * W // 8),
(W, W)、
(0, 255, 255),
-1,
8)
- 最后,我们有了 cv::rectangle 函数(我们没有为这个家伙创建特殊函数)。我们注意到
- 矩形将绘制在 rook_image 上
- 矩形的两个相对顶点定义为 ( 0, 7*w/8 ) 和 ( w, w )
- 矩形的颜色由 ( 0, 255, 255 ) 定义,这是黄色的 BGR 值。
- 由于厚度值为 FILLED (-1),因此矩形将被填充。
运行结果
编译并运行程序后会得到如下结果: