0

I am using Java openCV, and i try to load a photo and run the template matching over it. The template matching method deals with Mat objects, so i need to load the images and convert them into Mat.

The way i do it is:

Bitmap i = BitmapFactory.decodeFile("/sdcard/TVguide/Detection/detected.jpg");
image = Utils.bitmapToMat(i);

This way i load a photo from my android SD card, and try to convert it into Mat object using the openCV method bitmapToMat. The problem is that on the conversion, the application crashes.

Here is the LogCat:

            03-14 15:15:57.636: W/dalvikvm(1059): Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lorg/opencv/android/Utils;
            03-14 15:15:57.636: D/AndroidRuntime(1059): Shutting down VM
            03-14 15:15:57.636: W/dalvikvm(1059): threadid=1: thread exiting with uncaught exception (group=0x40015578)
            03-14 15:15:57.640: E/AndroidRuntime(1059): FATAL EXCEPTION: main
            03-14 15:15:57.640: E/AndroidRuntime(1059): java.lang.ExceptionInInitializerError
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at com.marakana.Preview$3.onPictureTaken(Preview.java:191)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at android.hardware.Camera$EventHandler.handleMessage(Camera.java:565)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at android.os.Handler.dispatchMessage(Handler.java:99)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at android.os.Looper.loop(Looper.java:123)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at android.app.ActivityThread.main(ActivityThread.java:3687)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at java.lang.reflect.Method.invokeNative(Native Method)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at java.lang.reflect.Method.invoke(Method.java:507)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at dalvik.system.NativeStart.main(Native Method)
            03-14 15:15:57.640: E/AndroidRuntime(1059): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load opencv_java: findLibrary returned null
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at java.lang.Runtime.loadLibrary(Runtime.java:429)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at java.lang.System.loadLibrary(System.java:554)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     at org.opencv.android.Utils.<clinit>(Utils.java:86)
            03-14 15:15:57.640: E/AndroidRuntime(1059):     ... 10 more
            03-14 15:16:03.472: I/Process(1059): Sending signal. PID: 1059 SIG: 9

I can't understand if i am doing something wrong or it is just a buggy openCV.

Thanks Eyal

Eyal
  • 1,748
  • 2
  • 17
  • 31

2 Answers2

3

You need to convert your bitmap to the RGBA format:

Bitmap bmp32 = i.copy(Bitmap.Config.ARGB_8888, true);

Actually it is the most discussed problem on Android-OpenCV user-group: https://groups.google.com/group/android-opencv/

The fix for this problem is already available in OpenCV trunk and will be included into the next release of OpenCV.

Also you can read image using the OpenCV API:

Mat image = Highgui.imread("/sdcard/TVguide/Detection/detected.jpg");
Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
  • I have tried both of the suggestions but it still returns the same logCat.. Maybe is has something to do with the line: java.lang.UnsatisfiedLinkError: Couldn't load opencv_java: findLibrary returned null – Eyal Mar 13 '12 at 16:45
  • UnsatisfiedLinkError means that your .apk does not include native binaries. You can unzip the .apk and check the presence of `libopencv_java.so`. Also it is possible that .apk has only binaries for ARMv7 while the phone has ARMv5/ARMv6 CPU. – Andrey Kamaev Mar 13 '12 at 16:56
  • The phone is Samsung Galaxy S, i believe that it should work, because some classmates have used the same phone with openCV. I don't realy understand what do you mean by .apk, but when i go to C:\Development\OpenCV-2.3.1\libs\armeabi-v7a, the libopencv_java.so exists. It also exists in armeabi. – Eyal Mar 13 '12 at 16:59
  • I just want to clarify - i am using openCV in my workspace by importing the whole library into my workspace. – Eyal Mar 13 '12 at 17:38
  • Solved! thanks.. i just didn't add a reference to openCV in my app. To do this: Do the right mouse click on your app in Package Explorer, go to Properties -> Android -> Library -> Add and choose the OpenCV library project. – Eyal Mar 13 '12 at 17:55
0

loading using OpenCV is quicker (20%-40%) than loading using BitmapFactory and transforming to OpenCV Mat

Mat image = Highgui.imread("/sdcard/TVguide/Detection/detected.jpg");

Abe
  • 11
  • 1