Android基于OpenCV實(shí)現(xiàn)相機(jī)實(shí)時(shí)圖像檢測(cè)

一.Android攝像頭預(yù)覽

Android OpenCV開發(fā)過(guò)程中,我們有3種可選方式去實(shí)現(xiàn)Android攝像頭預(yù)覽功能:

  • 使用Android系統(tǒng)Camera API
  • 使用CameraX(JetPack組件)
  • 使用OpenCV SDK輔助類(JavaCameraView、JavaCamera2View等)
利用Android OpenCV SDK實(shí)現(xiàn)相機(jī)預(yù)覽

1.申明權(quán)限

  <uses-permission android:name="android.permission.CAMERA" />
  <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.autofocus"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.front"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera.front.autofocus"
        android:required="false" />

2.布局添加JavaCameraView或者JavaCamera2View至布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <org.opencv.android.JavaCamera2View
        android:id="@+id/camera_view"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </org.opencv.android.JavaCamera2View>
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ImageView>

</androidx.constraintlayout.widget.ConstraintLayout>

3.Activity繼承CameraActivity,實(shí)現(xiàn)CameraBridgeViewBase.CvCameraViewListener或者CameraBridgeViewBase.CvCameraViewListener2接口,兩個(gè)接口的差異主要集中在onCameraFrame回調(diào)。

  @Override
    public void onCameraViewStarted(int width, int height) {

    }

    @Override
    public void onCameraViewStopped() {

    }

    @Override
    public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
        // 獲取相機(jī)中的圖像
        Mat rgba = inputFrame.rgba();
        Core.rotate(rgba, rgba, Core.ROTATE_90_CLOCKWISE);
        Bitmap bitmap = Bitmap.createBitmap(rgba.cols(), rgba.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(rgba, bitmap);
        Bitmap location = location(bitmap);
        if (location==null){
            return null;
        }
        runOnUiThread(() -> image_view.setImageBitmap(location));
        return null;
    }

4.復(fù)寫方法getCameraViewList

 @Override
    protected List<? extends CameraBridgeViewBase> getCameraViewList() {
        return Arrays.asList(mCVCamera);
    }

二.物體檢測(cè)

1.在onCameraFrame回調(diào)中拿到圖像后做一次濾鏡去除干擾再進(jìn)行邊緣檢測(cè),這時(shí)候邊緣可能并不是連通的所以我們?cè)賹?duì)圖像做一次膨脹操作。最后進(jìn)行輪廓檢測(cè)將檢測(cè)到的輪廓繪制到原圖上。

  private Bitmap location(Bitmap bmp) {
        Mat originMat=new Mat();
        Utils.bitmapToMat(bmp,originMat);
        Mat resultG = new Mat();
        Mat result = new Mat();
        Imgproc.GaussianBlur(originMat, resultG, new Size(3.0, 3.0), 0);
        Imgproc.Canny(resultG, result, 100.0, 220.0, 3);
        // 膨脹,連接邊緣
        Imgproc.dilate(result, result, new Mat(), new Point(-1,-1), 4, 1, new Scalar(1));
//        Bitmap Bmp = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);
//        Utils.matToBitmap(result,Bmp);

        List<MatOfPoint> contours  = new ArrayList<>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(result, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        //有了輪廓之后,為了方便我們先將輪廓給畫出來(lái),這里的resultMat其實(shí)就是srcMat,為了區(qū)分用了Mat resultMat = srcMat.clone();,
        // 下面代碼的意思是,把contours輪廓列表中的所有輪廓(-1表示所有輪廓)在,resultMat上畫用黑色(new Scalar(0, 0, 0))粗細(xì)為10的線條畫出來(lái)。
        if (contours.isEmpty()){
            return null;
        }
        Mat resultMat = resultG.clone();

        double arcLength=0;
        int index=0;
        for (int i = 0; i < contours.size(); i++) {
            MatOfPoint2f source = new MatOfPoint2f();
            source.fromList(contours.get(i).toList());
            if (Imgproc.arcLength(source, true)>arcLength){
                arcLength=Imgproc.arcLength(source, true);
                index=i;
            }

        }
        MatOfPoint matOfPoint = contours.get(index);
        MatOfPoint2f tempMat=new MatOfPoint2f();
        Imgproc.approxPolyDP(new MatOfPoint2f(matOfPoint.toArray()), tempMat, Imgproc.arcLength(new MatOfPoint2f(matOfPoint.toArray()), true)*0.04, true);
        Point[] points = tempMat.toArray();
        if (points.length!=4){
            return null;
        }
        List<MatOfPoint> matOfPoints  = new ArrayList<>();
        matOfPoints.add(new MatOfPoint(tempMat.toArray()));

        Imgproc.drawContours(resultMat, matOfPoints, -1, new Scalar(0, 0, 255), 4);

        Bitmap resultBmp = Bitmap.createBitmap(resultMat.cols(), resultMat.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(resultMat,resultBmp);

        return resultBmp;

    }

三.效果

沒辦法傳視頻。https://github.com/jzqCode/OpenCvDemo

111.jpg

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容