Android - OpenCv - 灰度

先搭建好开发环境:Android - OpenCv - 开发环境(导入module)
已搭建的直接如下实操:

效果

在这里插入图片描述

在这里插入图片描述

布局

 	<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bt"
            android:text="原图"
            android:onClick="Bt"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bt_gray"
            android:text="灰度"
            android:onClick="Gray"/>

    </LinearLayout>


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/iv"/>

在这里插入图片描述

Java

  • OpenCv库初始化
 private void staticLoadCVLibraries(){
        boolean load = OpenCVLoader.initDebug();
        if(load) {
            Log.i("CV", "OpenCV init success");
        }else {
            Log.e("CV", "OpenCV init failed");
        }
    }
  • 灰度操作
		Mat src = new Mat();
        Mat temp = new Mat();
        Mat dst = new Mat();
        //bitmap 转 矩阵
        Utils.bitmapToMat(bitmap, src);
        //色彩空间转换
        Imgproc.cvtColor(src, temp, Imgproc.COLOR_BGRA2BGR);
        Log.i("CV", "image type:" + (temp.type() == CvType.CV_8UC3));
        // COLOR_BGR2GRAY : 颜色_BGR2灰色
        Imgproc.cvtColor(temp, dst, Imgproc.COLOR_BGR2GRAY);
        //矩阵 转 bitmap
        Utils.matToBitmap(dst, bitmap);
        //显示图片
        imageView.setImageBitmap(bitmap);

在这里插入图片描述

完整代码(Java)

public class MainActivity extends AppCompatActivity {

    ImageView imageView;
    Bitmap bitmap;
    Button gray,bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        staticLoadCVLibraries();
        gray = findViewById(R.id.bt_gray);
        bt = findViewById(R.id.bt);
        imageView = findViewById(R.id.iv);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
        imageView.setImageBitmap(bitmap);
    }

    // OpenCV库初始化
    private void staticLoadCVLibraries(){
        boolean load = OpenCVLoader.initDebug();
        if(load) {
            Log.i("CV", "OpenCV init success");
        }else {
            Log.e("CV", "OpenCV init failed");
        }
    }

    public void Bt(View v){
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
        imageView.setImageBitmap(bitmap);
    }

    //灰度
    public void Gray(View v){
        Mat src = new Mat();
        Mat temp = new Mat();
        Mat dst = new Mat();
        //bitmap 转 矩阵
        Utils.bitmapToMat(bitmap, src);
        //色彩空间转换
        Imgproc.cvtColor(src, temp, Imgproc.COLOR_BGRA2BGR);
        Log.i("CV", "image type:" + (temp.type() == CvType.CV_8UC3));
        // COLOR_BGR2GRAY : 颜色_BGR2灰色
        Imgproc.cvtColor(temp, dst, Imgproc.COLOR_BGR2GRAY);
        //矩阵 转 bitmap
        Utils.matToBitmap(dst, bitmap);
        imageView.setImageBitmap(bitmap);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值