我们都知道,彩色图片每个像素点都对应三个值 如 [R,G,B],Core.split()这个函数则是帮我们这三个值分开,即分别提取 R,G,B各通道的灰度值
效果演示代码解析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); } }}
总结
以上是内存溢出为你收集整理的Android OpenCv 提取图像的RGB三原色分割图像Split Core.split全部内容,希望文章能够帮你解决Android OpenCv 提取图像的RGB三原色分割图像Split Core.split所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)