Android和OpenCV的学习绘制几何图形

几何图形绘制

几何图形绘制主要使用ImgProc类里的line、rectangle、polylines、circle、ellipse等函数,也可以使用putText函数绘制文字。图形绘制函数一般在APP中起到画面标注的功能。

 直线绘制line函数原型

public static void line(Nat img,Point pt1,Point pt2,Scalar color ,int thickness,int lineType, int shift);

参数

  • img:需要绘制的图像Mat
  • pt1:直线起点坐标
  • pt2:直线终点坐标
  • color:直线的颜色
  • thickness:直线的宽度
  • lineType,边界的类型,可取值为FILLED ,LINE_4 ,LINE_8 和LINE_AA
  • shift,点坐标中的小数位数

例子 

    private void Line() {
        //Bitmat转Mat
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f001);
        Mat src = new Mat();
        Utils.bitmapToMat(bitmap, src);
        //画线
        Imgproc.line(src, new Point(0, src.height()), new Point(src.width(), 0), new Scalar(255, 0, 0,255), 50, Imgproc.LINE_AA, 0);
        Utils.matToBitmap(src, bitmap);
        img1.setImageBitmap(bitmap);
        src.release();
    }

 矩形绘制rectangle函数原型

public static void rectangle(Mat img, Point pt1, Point pt2, Scalar color, int thickness, int lineType, int shift) 

参数

  • img:需要绘制的图像Mat
  • pt1:矩形左上角
  • pt2:矩形右下角
  • color:直线的颜色
  • thickness:直线的宽度,负值表示填充
  • lineType,边界的类型,可取值为FILLED ,LINE_4 ,LINE_8 和LINE_AA
  • shift,点坐标中的小数位数

例子 

    private void rectangle() {
        //Bitmat转Mat
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f001);
        Mat src = new Mat();
        Utils.bitmapToMat(bitmap, src);
        //矩形
        Imgproc.rectangle(src, new Point(0, 0), new Point(src.width(), src.height()), new Scalar(255, 0, 0, 255), 50, Imgproc.LINE_AA, 0);
        Utils.matToBitmap(src, bitmap);
        img1.setImageBitmap(bitmap);
        src.release();
    }

 

 多边形绘制polylines函数原型

public static void polylines(Mat img, List<MatOfPoint> pts, boolean isClosed, Scalar color, int thickness, int lineType, int shift)

参数

  • img:需要绘制的图像Mat。
  • pts:多边形端点坐标列表。
  • isClosed:是否闭合。
  • color:绘制直线的颜色。
  • thickness:直线的宽度。
  • lineType,边界的类型,可取值为FILLED ,LINE_4 ,LINE_8 和LINE_AA
  • shift,点坐标中的小数位数

例子

    private void polylines() {
        //Bitmat转Mat
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f001);
        Mat src = new Mat();
        Utils.bitmapToMat(bitmap, src);
        //多边形
        List<Point> points = new ArrayList<>();
        points.add(new Point(160, 160));
        points.add(new Point(3200, 320));
        points.add(new Point(160, 3840));
        points.add(new Point(1280, 1920));
        MatOfPoint mPoints = new MatOfPoint();
        mPoints.fromList(points);
        List<MatOfPoint> finalPoints = new ArrayList<MatOfPoint>();
        finalPoints.add(mPoints);
        Imgproc.polylines(src, finalPoints, true, new Scalar(255, 0, 0, 255), 50, Imgproc.LINE_AA, 0);
        Utils.matToBitmap(src, bitmap);
        img1.setImageBitmap(bitmap);
        src.release();
    }

 

 圆形绘制circle函数原型

public static void circle(Mat img, Point center, int radius, Scalar color, int thickness, int lineType, int shift) 

参数 

  • img:需要绘制的图像Mat。
  • center:圆心坐标。
  • radius:圆半径。
  • color:绘制直线的颜色。
  • thickness:直线的宽度。
  • lineType,边界的类型,可取值为FILLED ,LINE_4 ,LINE_8 和LINE_AA
  • shift,点坐标中的小数位数

例子

    private void circle() {
        //Bitmat转Mat
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f001);
        Mat src = new Mat();
        Utils.bitmapToMat(bitmap, src);
        //圆形
        Imgproc.circle(src, new Point(src.width() / 2, src.height() / 2), 600, new Scalar(255, 0, 0, 255), 50,Imgproc.LINE_AA, 0);
        Utils.matToBitmap(src, bitmap);
        img1.setImageBitmap(bitmap);
        src.release();
    }

  

 椭圆绘制eclipse函数原型

public static void ellipse(Mat img, Point center, Size axes, double angle, double startAngle, double endAngle, Scalar color, int thickness, int lineType, int shift)

参数 

  • img:需要绘制的图像Mat。
  • center:圆心坐标。
  • axes:椭圆长轴短轴半径。
  • angle,椭圆旋转的角度。
  • startAngle,椭圆弧起始角度。
  • endAngle,椭圆弧终止角度。
  • color,绘制直线的颜色。
  • thickness,直线宽度,负值表示填充
  • lineType,边界的类型,可取值为FILLED ,LINE_4 ,LINE_8 和LINE_AA
  • shift,点坐标中的小数位数
    private void eclipse() {
        //Bitmat转Mat
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f001);
        Mat src = new Mat();
        Utils.bitmapToMat(bitmap, src);
        //椭圆
        Imgproc.ellipse(src, new Point(src.width() / 2, src.height() / 2), new Size(src.width() / 4, src.height() / 8), 360, 0, 360, new Scalar(255, 0, 0, 255), 50,Imgproc.LINE_AA, 0);
        Utils.matToBitmap(src, bitmap);
        img1.setImageBitmap(bitmap);
        src.release();
    }

 

 文字绘制putText函数原型

public static void putText(Mat img, String text, Point org, int fontFace, double fontScale, Scalar color, int thickness, int lineType, boolean bottomLeftOrigin)

参数 

  • img:需要绘制的图像Mat。
  • text:文字内容,不能是中文。
  • org:文本字符串的左下角位置。
  • fontFace:字体类型,可取值。
  • fontScale:字体大小。
  • color:绘制直线的颜色。
  • thicness,直线宽度。
  • lineType,边界的类型,可取值为FILLED ,LINE_4 ,LINE_8 和LINE_AA。
  • bottomLeftOrigin,如果为true,则图像数据原点位于左下角。否则,位于左上角。

例子

    private void word() {
        //Bitmat转Mat
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.f001);
        Mat src = new Mat();
        Utils.bitmapToMat(bitmap, src);
        //文字
         Imgproc.putText(src, "I'm a boy", new Point(src.width() / 2., src.height() / 3), 2, 10, new Scalar(255, 0, 0, 255), 50, Imgproc.LINE_AA);
        Utils.matToBitmap(src, bitmap);
        img1.setImageBitmap(bitmap);
        src.release();
    }

posted @ 2022-05-12 08:56  卟怪  阅读(141)  评论(0编辑  收藏  举报