opencv for andorid 的各种骚操作基础篇--灰度

最近项目有个需求需要实现物体追踪和人脸识别 ,需要Android实现对图像的处理用到opencv4android 做点笔记。
首先Android中使用opencv要下载个opencv官网接着配置环境 以前文章写过 这里就不赘述了opencv环境配置及免安装第三方软件
接下来就是布局,这里只需要一个按钮和一张图片就可以了,有两张图片一个是去灰度前,一个是后

<Button
        android:id="@+id/top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="start"
        android:onClick="startTest"
        android:layout_centerHorizontal="true"
        />
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:src="@drawable/z"
        android:layout_centerHorizontal="true"
        android:background="@color/black"
        android:layout_below="@+id/top"
        />
    <ImageView
        android:id="@+id/img_result"
        android:layout_width="wrap_content"
        android:layout_height="150dp"
        android:src="@drawable/z"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/img"
        android:layout_marginTop="20dp"
        android:background="@color/black"
        />

接着是核心代码 里面有注释

  public static Bitmap setGray(Context context) {
        //获取bitmap数据
        Bitmap bitmap =getBitmap(R.drawable.z,context);
        //bitmap 对象转换成 mat 对象  opencv使用的是mat对象
        Mat src=new Mat();
        Utils.bitmapToMat(bitmap,src);
        //转化灰度结果 保存的mat
        Mat dst=new Mat();
        //转化成灰度图片
        Imgproc.cvtColor(src,dst,Imgproc.COLOR_BGR2GRAY);
        //转化会bitmat 对象
        Utils.matToBitmap(dst,bitmap);
        //释放资源
        src.release();
        dst.release();
        return bitmap;
    }

最后在同个imageView.setImageBitmap()设置回去 搞定!

if (mBitmap != null) {
            Log.i(TAG, "startTest: !null");
            mImgResult.setImageBitmap(mBitmap);
        }

这个是源码opencvDemon

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码划云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值