1. 程式人生 > >android使用opencv擴充套件包

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