栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

OpenCV4.3 Java 编程入门:绘制基本图形

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

OpenCV4.3 Java 编程入门:绘制基本图形

文章目录
  • 1 绘制直线
  • 2 绘制圆形
  • 3 绘制椭圆
  • 4 绘制多边
  • 5 绘制矩形
  • 6 绘制箭头
  • 7 绘制图标

opencv 中使用 Point 在图像中定义2D点,使用 Scalar 表示颜色值。

在 imgproc 模块中,提供了多种绘制基本图形的方法:

  • circle() :绘制圆形
  • ellipse():绘制椭圆
  • line():绘制直线
  • polylines​():绘制多边形
  • rectangle​():绘制矩形
  • arrowedLine() : 绘制箭头
1 绘制直线

方法定义:

	
    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();
    }

效果:

2 绘制圆形

方法定义:

    
    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();
    }

效果:

3 绘制椭圆

方法定义:

	
    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();
    }

效果:

4 绘制多边
	
    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();
    }

效果:

5 绘制矩形

方法定义:

	
    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();
    }

效果:

6 绘制箭头

方法定义:

	
    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();
    }

效果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830890.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号