Android+java关于ArrayList<MatOfPoint>的使用

ArrayList<MatOfPoint>

初学Android opencv学到绘制多边形

polyines(Mat img,List<MatOfPoint> pts,boolean isClosed,Scalar color,int thickness)

img 就是需要绘制的图像

pts 就是绘制图像各个点的坐标

isClosed 就是问你绘制的图形是否闭合

color 线的颜色

thickness 线的宽度如果为负数则会封闭

因为本人出学过于愚笨顾写此文章来记下ArrayList<MatOfPoint>的使用

package com.example.opencv;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import java.io.IOException;
import java.util.ArrayList;

public class deom_1 extends AppCompatActivity {
    private ImageView imageView1, imageView2, imageView3;
    private Button button1;
    private Mat mat1, mat2, result;
    private Bitmap bitmap;
    private ArrayList<MatOfPoint> list = new ArrayList<>();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mat1.release();
        //mat2.release();
        result.release();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.deom_1);
        initLoadOpenCV();
        imageView1 = findViewById(R.id.imageView1);
        imageView2 = findViewById(R.id.imageView2);
        imageView3 = findViewById(R.id.imageView3);
        button1 = findViewById(R.id.button1);
        mat1 = new Mat();
        // mat2 = new Mat();
        result = new Mat();
        try {
            mat1 = Utils.loadResource(this, R.drawable.gura1);
            //mat2 = Utils.loadResource(this, R.drawable.gura2);
        } catch (IOException e) {
            e.printStackTrace();
            // return;
        }

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Imgproc.cvtColor(mat1, result, Imgproc.COLOR_RGB2GRAY);
                list.add(new MatOfPoint(new Point(10, result.height()-10),
                        new Point(result.width()/4,result.height()/4),
                        new Point(result.width()*3/4,result.height()/4),
                        new Point(result.width()-10,result.height()-10)));
                //Imgproc.threshold(result,mat1,125,255,Imgproc.THRESH_TOZERO_INV);
                Imgproc.adaptiveThreshold(result, result, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 13, 5);
                //Core.bitwise_or(mat1,mat2,result);
                //Imgproc.line(mat1,new Point(0,result.height()),new Point(result.width(),0),new Scalar(0,0,0),1);
                //Imgproc.rectangle(mat1,new Point(result.width()/4,result.height()/4),new Point(result.width()*3/4,result.height()*3/4),new Scalar(0,255,0),2);
                Imgproc.polylines(mat1,list,true,new Scalar(255,0,0),2);
                //Imgproc.circle(mat1,new Point(result.width()/2,result.height()/2),result.height()/4,new Scalar(255,20,100),1);
                //Imgproc.putText(mat1,"fuck",new Point(result.width()/4,result.height()/2),1,1,new Scalar(255,210,12),1);
                bitmap = Bitmap.createBitmap(result.width(), result.height(), Bitmap.Config.ARGB_8888);
                Imgproc.cvtColor(mat1,mat1,Imgproc.COLOR_BGR2RGB);
                Utils.matToBitmap(mat1, bitmap);
                imageView3.setImageBitmap(bitmap);

            }
        });
    }

    public void initLoadOpenCV() {
        boolean success = OpenCVLoader.initDebug();
        if (success) {
            Log.d("init", "initLoadOpenCV: openCV load success");
        } else {
            Log.e("init", "initLoadOpenCV: openCV load failed");
        }
    }
}

 代码还有待规范见谅

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值