- 1 绘制直线
- 2 绘制圆形
- 3 绘制椭圆
- 4 绘制多边
- 5 绘制矩形
- 6 绘制箭头
- 7 绘制图标
opencv 中使用 Point 在图像中定义2D点,使用 Scalar 表示颜色值。
在 imgproc 模块中,提供了多种绘制基本图形的方法:
- circle() :绘制圆形
- ellipse():绘制椭圆
- line():绘制直线
- polylines():绘制多边形
- rectangle():绘制矩形
- arrowedLine() : 绘制箭头
方法定义:
public static void line(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType, int shift);
下面是该方法的使用示例:
@Test
public void lineTest() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
Imgproc.line(mat, new Point(20,30), new Point(380,380), new Scalar(255), 10, Imgproc.FILLED);
Imgproc.line(mat, new Point(100,30), new Point(300,380), new Scalar(255), 10, Imgproc.LINE_4);
Imgproc.line(mat, new Point(350,30), new Point(100,380), new Scalar(255), 20, Imgproc.LINE_8);
Imgproc.line(mat, new Point(380,30), new Point(20,380), new Scalar(255), 5, Imgproc.LINE_AA);
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
示例 2
@Test
public void lineTest2() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
for (int i = 0; i < 20; i++) {
Imgproc.line(mat, new Point(i*20,0), new Point(400-i*20,400), new Scalar(i*256/20.0), 2, Imgproc.FILLED);
Imgproc.line(mat, new Point(400,i*20), new Point(0,400-i*20), new Scalar(i*256/20.0), 2, Imgproc.LINE_4);
Imgproc.line(mat, new Point(400-i*20,400), new Point(i*20,0), new Scalar(i*256/20.0), 2, Imgproc.LINE_8);
Imgproc.line(mat, new Point(0,400-i*20), new Point(400,i*20), new Scalar(i*256/20.0), 2, Imgproc.LINE_AA);
}
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
示例 3:
@Test
public void lineTest3() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
for (int i = 0; i < 100; i++) {
double x = Math.random()*300 + i;
double y = Math.random()*300 + i;
Imgproc.line(mat, new Point(x,y), new Point(400- x,400 -y ), new Scalar(256 - Math.random()*100), 2, Imgproc.LINE_8);
}
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
效果:
方法定义:
public static void circle(Mat img, Point center, int radius, Scalar color, int thickness, int lineType, int shift);
示例代码:
public void circleTest() {
Mat mat = Mat.zeros(new Size(400,400), CV_8UC3);
for (int i = 0; i < 50; i++) {
Point center = new Point(Math.random()*400,Math.random()*400);
int radius = (int)(Math.random()*100);
Scalar color = new Scalar(Math.random()*255, Math.random()*255, Math.random()*255);
Imgproc.circle(mat, center, radius, color, 2, Imgproc.LINE_8);
}
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
效果:
方法定义:
public static void ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle, Scalar color, int thickness, int lineType, int shift);
下图解释了绘制蓝色圆弧的参数的含义。
示例代码:
@Test
public void ellipseTest() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
Point center = new Point(200,200);
Size size = new Size(60, 120);
Scalar color = new Scalar(200);
Imgproc.ellipse(mat, center, size, 45, 0, 270, color, -1, Imgproc.LINE_8);
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
效果:
public static void polylines(Mat img, List pts, boolean isClosed, Scalar color, int thickness, int lineType, int shift);
示例代码:
@Test
public void polylinesTest() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
Scalar color = new Scalar(255);
MatOfPoint point1 = new MatOfPoint();
point1.fromArray(new Point(0,0), new Point(30,40), new Point(80,150),new Point(100,300), new Point(130,280), new Point(180,150),new Point(300,500));
Imgproc.polylines(mat, Arrays.asList(point1), true, color, 1, Imgproc.LINE_8);
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
效果:
方法定义:
public static void rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType, int shift);
示例代码:
@Test
public void rectangleTest() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
Scalar color = new Scalar(255);
Imgproc.rectangle(mat, new Point(100,100), new Point(300,300), color, -1, Imgproc.LINE_8);
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
效果:
方法定义:
public static void arrowedLine(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int line_type, int shift, double tipLength);
示例代码:
@Test
public void arrowedLineTest() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
Scalar color = new Scalar(255);
Imgproc.arrowedLine(mat, new Point(30,40), new Point(130,140), color, 1, Imgproc.LINE_8);
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
7 绘制图标
方法示例:
public static void drawMarker(Mat img, Point position, Scalar color, int markerType, int markerSize, int thickness, int line_type);
示例代码:
@Test
public void drawMarkerTest() {
Mat mat = Mat.zeros(new Size(400,400), CV_8U);
Scalar color = new Scalar(255);
Imgproc.drawMarker(mat, new Point(100,200), color, 0, 20);
Imgproc.drawMarker(mat, new Point(150,200), color, 1, 20);
Imgproc.drawMarker(mat, new Point(200,200), color, 2, 20);
Imgproc.drawMarker(mat, new Point(250,200), color, 3, 20);
Imgproc.drawMarker(mat, new Point(300,200), color, 4, 20);
Imgproc.drawMarker(mat, new Point(350,200), color, 5, 20);
HighGui.imshow("demo", mat);
HighGui.waitKey();
}
效果:



