03_OpenCv灰度化图像

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gzx110304/article/details/82587966

OpenCv灰度化图像

一.在android中,使用Bitmap对象来描述一张图片,而在OpenCv For Android中使用Mat对象来描述一张图片,因此,在android中使用OpenCv处理图像时,就会存在Mat对象与Bitmap对象的相互转化

二.android中将drawable资源转化为Bitmap对象

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource(
    context.getResources(), 
    drawableId, 
    options);

三.Bitmap对象与Mat对象的相互转化

Utils.bitmapToMat(bitmap, mat);//Bitmap转Mat
Utils.matToBitmap(mat, bitmap);//Mat转Bitmap

四.灰度化图像

Mat src = new Mat();
Mat dst = new Mat();

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.test, options);
Utils.bitmapToMat(bitmap, src);
Imgproc.cvtColor(src, dst, Imgproc.COLOR_BGRA2GRAY);
//dst中存储的即为灰度化后的图像
Utils.matToBitmap(dst, bitmap);

dst.release();
src.release();

五.在使用OpenCv SDK之前一定要先通过调用OpenCVLoader.initDebug();来进行初始化

猜你喜欢

转载自blog.csdn.net/gzx110304/article/details/82587966
今日推荐