Android OpenCv 提取图像的RGB三原色分割图像Split Core.split

函数简析

我们都知道,彩色图片每个像素点都对应三个值 如 [R,G,B],Core.split()这个函数则是帮我们这三个值分开,即分别提取 R,G,B各通道的灰度值

效果演示

Android OpenCv 提取图像的RGB三原色分割图像Split Core.split

代码解析

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="match_parent"
        android:layout_height="300dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_loadimg"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="导入图片"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/iv_img_one"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <ImageView
            android:id="@+id/iv_img_two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <ImageView
            android:id="@+id/iv_img_three"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

MainActivity

package com.wust.opencv0;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    private ImageView iv_img;
    private Button btn_loadimg;
    private Bitmap bitmap;
    private List<ImageView> imageViewList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_img = findViewById(R.id.iv_img);
        btn_loadimg = findViewById(R.id.btn_loadimg);

        ImageView iv_img_one = findViewById(R.id.iv_img_one);
        ImageView iv_img_two = findViewById(R.id.iv_img_two);
        ImageView iv_img_three = findViewById(R.id.iv_img_three);
        imageViewList = new ArrayList<>();
        imageViewList.add(iv_img_one);
        imageViewList.add(iv_img_two);
        imageViewList.add(iv_img_three);


        btn_loadimg.setOnClickListener(this);
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btn_loadimg:
                loadImage();
                break;
        }
    }

    private void loadImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent,1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            try {
                Uri uri = data.getData();
                ContentResolver cr = getContentResolver();
                bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
                iv_img.setImageBitmap(bitmap);
                minMaxLoc(bitmap); //在这个地方调用了 minMaxLoc() 讲解Core.split()只是个小 
                                   //插曲,所以,函数命名看起来不是很规范
            } catch (Exception e) {
                Log.e("MainActivity",e.getMessage(),e);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()){
            Log.e("MainActivity","内置OpenCv没有找到,请下载OpenCV Manage");
        }else {
            Log.e("MainActivity","内置OpenCv找到了");
        }
    }

    private void minMaxLoc(Bitmap sorce){
        Mat sorceMat = new Mat();
        Utils.bitmapToMat(sorce,sorceMat);
        List<Mat> bgrList = new ArrayList();
        //最为关键的就是这句话,第一个参数:4通道图像对应的 Mat,第二个参数:装 Mat 的 List
        Core.split(sorceMat,bgrList);  
        for (int i = 0; i < 3; i++) {  //因为 bgrList 里面有4个(包含透明度) 为了与 
                                       //imageViewList长度对 
                                       //应,所以写死了 为 3 
            Mat letMat = bgrList.get(i);
            System.out.println("我被执行了" + letMat);
            Bitmap letBitmap = Bitmap.createBitmap(letMat.width(),letMat.height(), Bitmap.Config.ARGB_8888);
            Utils.matToBitmap(letMat,letBitmap);
            imageViewList.get(i).setImageBitmap(letBitmap);
        }
    }
}

 

上一篇:php AES加密解密的例子


下一篇:188. 买卖股票的最佳时机IV