I am trying to process a bmp image obtained from Android's camera intent through the following steps:
- Obtain
bmp
image from intent - Convert
bmp
image toMat
object for OpenCV processing (problem starts here) - Do needed OpenCV processing (through sending
Mat
object, obj, as obj.getNativeObjAddr()
to native processing, or perform it locally in java). - Convert
Mat
object back tobmp
The problem is indeed not novel. I have found countless similar questions online, none of which seem to resolve the situation however.
Results (problem)
The intention is to display the processed image (that undergoes the above 4 steps) in an ImageView
object. After running, the ImageView however remains unchanged and logcat emits the following warning on reaching the line calling Utils.bitmapToMat()
W/System.err(3872): java.lang.IllegalArgumentException: mat == null
Code
Below is the code's outline that is used in the onActivityResult
method (resultant_bmp
is the acquired bmp from the camera intent. It displays successfully on its own).
filePath
is the file path, name and extension included, of resultant_bmp
.
The first 3 lines following the starting if
condition are from here, where its use seems to work fine in the mentioned question.
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);
}
Previous attempts& research
- I found a similar problem here and tried, as recommended, to push the file
libopencv_java.so
tosystem/lib
on the device, using adb in command line and received the error:
failed to copy 'libopencv_java.so' to 'system/lib/libopencv_java.so': Read-only file system
This question considers the same problem domain in the current question; the code provided above follows a similar pattern as that suggested there, which still doesn't work.
Based on this answer (as well as this aforementioned one), I added the following lines to the code:
System.loadLibrary("opencv_java");
System.loadLibrary("libopencv_java");
I have added the OpenCV library to the project properties; as well as the NDKROOT variable indicating where the NDK root path lies, as explained further here.
I've tried the image processing required (in step 3 above) in another working sample of OpenCV's, so the problem definitely lies within step 2 (above); conversion of a
bmp
to aMat
object.
There should be a simple solution around this, but I cannot seem to find it. Help would be much appreciated if possible.
Thank you for your time.