android使用opencv扩展包

1.在 https://github.com/richdyang/opencv3-android-sdk-with-contrib 下载包含android opencv扩展包的SDK。

2.配置。配置方法同官网下载的SDK。

测试代码如下

package com.opencvcontribtest;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.opencv.ximgproc.Ximgproc;

import static org.opencv.imgproc.Imgproc.THRESH_BINARY;
import static org.opencv.ximgproc.Ximgproc.BINARIZATION_SAUVOLA;

public class MainActivity extends AppCompatActivity
{
    static
    {
        if (!OpenCVLoader.initDebug())
        {
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.img1);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.i2);

        Mat mat = new Mat();
        Utils.bitmapToMat(bitmap, mat);
        Imgproc.cvtColor(mat, mat, Imgproc.COLOR_RGB2GRAY);
        Imgproc.medianBlur(mat, mat, 5);
        Mat mat1 = new Mat();
        
        //扩展包算法
        Ximgproc.niBlackThreshold(mat, mat1,255, BINARIZATION_SAUVOLA, 5, 0.5, THRESH_BINARY);

        Bitmap newBmp = null;
        newBmp = Bitmap.createBitmap(mat1.cols(), mat1.rows(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(mat1, newBmp);
        mat.release();
        mat1.release();
        imageView.setImageBitmap(newBmp);
    }
}
 
 

猜你喜欢

转载自blog.csdn.net/xyx2999/article/details/79466041