【问题标题】:Processing an Android camera-intent Bitmap image in OpenCV (needs conversion to Mat object)?在 OpenCV 中处理 Android 相机意图位图图像(需要转换为 Mat 对象)?
【发布时间】:2013-01-29 18:41:25
【问题描述】:

我正在尝试通过以下步骤处理从 Android 的相机意图获取的 bmp 图像:

  1. 从intent获取bmp图片
  2. bmp 图像转换为Mat 对象以进行OpenCV 处理(问题从这里开始)
  3. 做需要的OpenCV处理(通过将Mat对象,obj,作为obj.getNativeObjAddr()发送到本地处理,或者在java中本地执行)。李>
  4. Mat对象转换回bmp

这个问题确实不新鲜。我在网上找到了无数类似的问题,但是似乎没有一个可以解决这种情况。

结果(问题)

目的是在ImageView 对象中显示处理后的图像(经过上述 4 个步骤)。运行后,ImageView 保持不变,logcat 在到达调用Utils.bitmapToMat() 的行时发出以下警告

 W/System.err(3872): java.lang.IllegalArgumentException: mat == null

代码

下面是onActivityResult方法中使用的代码大纲(resultant_bmp是从相机intent中获取的bmp,​​它自己显示成功)。
filePath是文件路径,名称包括resultant_bmp 的扩展名。

起始 if 条件之后的前 3 行来自 here,在上述问题中它的使用似乎可以正常工作。

Bitmap resultant_bmp /*image from camera*/,   
       bmp /*image after opencv processing*/;
Mat rgb_img, gray_img;      

if (OpenCVLoader.initDebug()) { 

     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inPreferredConfig = Config.RGB_565;
     resultant_bmp= BitmapFactory.decodeFile(filePath, options);

     /****************** Problem starts HERE ******************
     Last point reachable before Logcat states:
       W/System.err(4460):java.lang.IllegalArgumentException: mat == null   

     After this point, the program doesn't crash, but expected results 
       in the imageView (last line of code) do not result.
     **********************************************************/  

     Utils.bitmapToMat(resultant_bmp, rgb_img);                                 
     Imgproc.cvtColor(rgb_img, gray_img, Imgproc.COLOR_RGBA2GRAY); 

     /*Do opencv processing (on gray_img) here*/

     Utils.matToBitmap(gray_img, bmp);

     imageView.setImageBitmap(bmp);

}

以往的尝试和研究

  • 我发现了一个类似的问题here 并尝试按照建议将文件libopencv_java.so 推送到设备上的system/lib,在命令行中使用adb 并收到错误:

failed to copy 'libopencv_java.so' to 'system/lib/libopencv_java.so': Read-only file system

  • This question 考虑当前问题中相同的问题域;上面提供的代码遵循与那里建议的类似模式,但仍然不起作用。

  • 基于this的答案(以及this前面提到的一个),我在代码中添加了以下几行:

System.loadLibrary("opencv_java");
System.loadLibrary("libopencv_java");

  • 我已将 OpenCV 库添加到项目属性中;以及指示 NDK 根路径所在位置的 NDKROOT 变量,如进一步解释 here

  • 我已经在 OpenCV 的另一个工作示例中尝试了所需的图像处理(在上面的步骤 3 中),所以问题肯定出在 步骤 2(上面);将 bmp 转换为 Mat 对象。

应该有一个简单的解决方案,但我似乎找不到它。如果可能的话,我们将不胜感激。

感谢您的宝贵时间。

【问题讨论】:

    标签: android opencv bitmap android-ndk android-camera-intent


    【解决方案1】:

    你初始化你的 mat 变量了吗?我在提供的代码中找不到它。

    在尝试转换为 mat 之前添加此内容。

    Mat rgb_img = new Mat();
    

    【讨论】:

    • 非常感谢;这确实是问题所在。不,我没有。添加此行将通过 logcat,但不会显示在 imageView 中(关于步骤 4 而不是 2)。这样做;还必须添加以下行(与 here 一样):bmp = Bitmap.createBitmap(gray_img.cols(), gray_img.rows(), Bitmap.Config.ARGB_8888); 现在可以使用了。
    猜你喜欢
    • 2017-07-15
    • 2012-10-19
    • 1970-01-01
    • 2018-10-09
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多