在android中使用opencv,在android中使用opencv

我有一个代码捕获/选择一个图像,并将其设置为一个imageview。我想要的是,将这个图像存储在一个变量中,将它传递给一个函数,并在textView中设置一个整数输出。现在,如果我手动使用存储在drawable中的图像(转换成位图,然后mat),我会得到所需的结果。但是,当我尝试使用从相机/图库中动态获取的位图图像(转换为图像后)时,会出现异常。据我所知,捕获/选定的图像没有被转换或存储在一个变量,因为它显示bmp = null。 的代码是: 对于捕捉图像:在android中使用opencv

public void onCaptureImageResult(Intent data) {

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

File destination = new File(Environment.getExternalStorageDirectory(),

System.currentTimeMillis() + ".jpg");

FileOutputStream fo;

try {

destination.createNewFile();

fo = new FileOutputStream(destination);

fo.write(bytes.toByteArray());

fo.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

thumbnail1=thumbnail;

ivImage.setImageBitmap(thumbnail);

}

为了选择图像:

public void onSelectFromGalleryResult(Intent data) {

bm = null;

if (data != null) {

try {

bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());

} catch (IOException e) {

e.printStackTrace();

}

}

ivImage.setImageBitmap(bm);

}

用于处理图像和设置输出:

public void countDents(Mat src) {

int count = 0;

Mat source;

source = src;

int cnt = 0, cnt2 = 0;

Mat middle, destination1, destination2;

List contours1 = new ArrayList<>();

List contours2 = new ArrayList<>();

middle = new Mat();

destination1 = new Mat();

destination2 = new Mat();

/*Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.car);

Utils.bitmapToMat(img, source);*/

Imgproc.cvtColor(source, middle, Imgproc.COLOR_RGB2GRAY);

Imgproc.equalizeHist(middle, middle);

// ivImage.setImageBitmap(img);

Imgproc.threshold(middle, destination1, 150, 255, Imgproc.THRESH_BINARY);

Imgproc.threshold(middle, destination2, 150, 255, Imgproc.THRESH_BINARY_INV);

Imgproc.findContours(destination1, contours1, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

Imgproc.findContours(destination2, contours2, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

// Utils.matToBitmap(destination2, img);

for (int i = 0; i < contours1.size(); i++) {

if (Imgproc.contourArea(contours1.get(i)) > 5 && Imgproc.contourArea(contours1.get(i)) < 200) {

cnt++;

}

}

for (int i = 0; i < contours2.size(); i++) {

if (Imgproc.contourArea(contours2.get(i)) > 5 && Imgproc.contourArea(contours2.get(i)) < 200) {

cnt2++;

count = cnt + cnt2;

String c = Integer.toString(count);

TVdents.setText(c);

}

}

的onClick功能对于其中功能被称为:

public void onClick(View v) {

Utils.bitmapToMat(thumbnail1,tmp);

Utils.bitmapToMat(bm,tmp);

countDents(tmp);

}

十一月7日至25日:19:21.727 19363-19363/com.example.smartlayer.imageprocessing E/AndroidRuntime:致命异常:主 工艺:com.example.smartlayer.imageprocessing,PID:19363 的java .lang.IllegalArgumentException:BMP == NULL 在org.opencv.android.Utils.bitmapToMat(Utils.java:90) 在org.opencv.android.Utils.bitmapToMat(Utils.java:102) 在 融为一体。 example.smartlayer.imageprocessing.MainActivity $ 2.onClick(MainActivity.java:80) at android.view.View.performClick(View.j AVA:4785) 在android.view.View $ PerformClick.run(View.java:19884) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(处理程序。 java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5343) at java.lang.reflect.Method.invoke(Native Method ) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905) at com.an droid.internal.os.ZygoteInit.main(ZygoteInit.java:700)

如果INT的问题,问的代码或方式有差异,请让我知道,我会纠正。

2017-07-25

Harshita

+0

你说“** BMP = NULL **”。但是,在代码中没有看到“bmp” - 请指出发生这种空图像的位置。 –

+0

它在标准的utils类,而不是我的代码。它在logcat上显示发生异常时的情况:** java.lang.IllegalArgumentException:bmp == null ** –

+0

然后请发布您的LogCat。当你跟踪堆栈跟踪时,代码中的哪一行会给出错误? –

你可能感兴趣的:(在android中使用opencv,在android中使用opencv)