java for mat,Java Mat.size方法代码示例

import org.opencv.core.Mat; //导入方法依赖的package包/类

private void example() {

RenderScript mRS = RenderScript.create(this);

// Loads input image

Bitmap inputBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.houseimage);

// Puts input image inside an OpenCV mat

Mat inputMat = new Mat();

Utils.bitmapToMat(inputBitmap, inputMat);

Mat outputMat = new Mat(inputMat.size(), inputMat.type());

// Testing bitmap, used to test that the OpenCV mat actually has bitmap data inside

Bitmap initialBitmap = Bitmap.createBitmap(inputMat.width(), inputMat.height(), Bitmap.Config.ARGB_8888);

Utils.matToBitmap(inputMat, initialBitmap);

// Retrieve OpenCV mat data address

long inputMatDataAddress = inputMat.dataAddr();

long outputMatDataAddress = outputMat.dataAddr();

// Creates a RS type that matches the input mat one.

Element element = Element.RGBA_8888(mRS);

Type.Builder tb = new Type.Builder(mRS, element);

tb.setX(inputMat.width());

tb.setY(inputMat.height());

Type inputMatType = tb.create();

// Creates a RenderScript allocation that uses directly the OpenCV input mat address

Allocation inputAllocation = createTypedAllocationWithDataPointer(mRS, inputMatType, inputMatDataAddress);

Allocation outputAllocation = createTypedAllocationWithDataPointer(mRS, inputMatType, outputMatDataAddress);

// Define a simple convolve script

// Note: here, ANY kernel can be applied!

ScriptIntrinsicConvolve3x3 convolve3x3 = ScriptIntrinsicConvolve3x3.create(mRS, element);

float convolveCoefficients[] = new float[9];

convolveCoefficients[0] = 1;

convolveCoefficients[2] = 1;

convolveCoefficients[5] = 1;

convolveCoefficients[6] = 1;

convolveCoefficients[8] = 1;

convolve3x3.setCoefficients(convolveCoefficients);

convolve3x3.setInput(inputAllocation);

convolve3x3.forEach(outputAllocation);

mRS.finish();

// Converts the result to a bitmap

Bitmap cvOutputBitmap = Bitmap.createBitmap(outputMat.width(), outputMat.height(), Bitmap.Config.ARGB_8888);

Utils.matToBitmap(outputMat, cvOutputBitmap);

// Testing bitmap, used to test the RenderScript ouput allocation contents

// Note: it is placed here because the copyTo function clears the input buffer

Bitmap rsOutputBitmap = Bitmap.createBitmap(outputMat.width(), outputMat.height(), Bitmap.Config.ARGB_8888);

outputAllocation.copyTo(rsOutputBitmap);

// Testing bitmap, used to test that RenderScript input allocation pointed to the OpenCV mat

// Note: it is placed here because the copyTo function clears the input buffer

Bitmap rsInitialBitmap = Bitmap.createBitmap(inputMat.width(), inputMat.height(), Bitmap.Config.ARGB_8888);

inputAllocation.copyTo(rsInitialBitmap);

// Display input and output

ImageView originalImageIV = (ImageView) findViewById(R.id.imageView);

ImageView inputRSImageIV = (ImageView) findViewById(R.id.imageView2);

ImageView outputRSImageIV = (ImageView) findViewById(R.id.imageView3);

ImageView outputCVIV = (ImageView) findViewById(R.id.imageView4);

originalImageIV.setImageBitmap(initialBitmap);

inputRSImageIV.setImageBitmap(rsInitialBitmap);

outputRSImageIV.setImageBitmap(rsOutputBitmap);

outputCVIV.setImageBitmap(cvOutputBitmap);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值