OpenCV

Get started with OpenCV on Android™

As our smartphones become more powerful, we can do more advanced things that previously required a high-end PC. One way to make use of the robust processing power of your smartphone is Computer Vision – the ability for a device to acquire, process, analyse and understand images the same way images are perceived by human eyes. Basically, we can use the powerful CPU in modern smartphones to interpret the images captured through the camera. Examples of use cases are face detection and recognition or simple post-processing of photographs. The best approach to using Computer Vision on Android is through library called OpenCV. Read on as Erik Hellman, research engineer at Sony, explains more.

Erik Hellman, research engineer at Sony.

The OpenCV library of programming functions has become the de-facto industry standard for software based computer vision. It is completely open-sourced (BSD licensed) and has support for most major platforms, including Android. The current version is 2.4.3, and you can download the Open CV SDK for free and start writing your OpenCV-based Android apps from the OpenCV website.

In this tutorial I will cover three things that will help you to get started with OpenCV on Android:

  1. How to acquire images from the Android camera through OpenCV.
  2. How to process an image to retrieve the parts within a certain colour range only.
  3. How to display the result of the processed image.

Before you can do any of the above, you need to load the OpenCV library. OpenCV for Android is designed as a standalone library APK that you install from Google Play. You also need to include the OpenCV SDK as an Android library project to your own Android project. Instructions on how to do this can be found in the OpenCV online documentation.

How to acquire camera images using OpenCV
In this first example, we will rely on the preview frames from the camera since we can retrieve these images relatively fast. We need to pick an appropriate size for the preview frames, as too small frames will result in a bad result when we do the processing, and too large frames will slow down everything to an unacceptable level. Since smartphone cameras often have a different set of supported sizes for preview frames, we need to pick a minimum acceptable size and go through all of them to find the correct one. OpenCV has its own Java API, and doesn’t rely on the Java-based Android Camera API. The following code opens the camera and sets the preview size to an acceptable level.

private void setupCamera() {

    if (mCamera != null) {
       VideoCapture camera = mCamera;
       mCamera = null; // Make it null before releasing...
      camera.release();
    }

    mCamera = new VideoCapture(mCameraId);

    List<Size> previewSizes = mCamera.getSupportedPreviewSizes();
    double smallestPreviewSize = 1280 * 720; // We should be smaller than this...
    double smallestWidth = 480; // Let's not get smaller than this...
    for (Size previewSize : previewSizes) {
        if (previewSize.area() < smallestPreviewSize && previewSize.width >= smallestWidth) {
            mPreviewSize = previewSize;
        }
    }

mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mPreviewSize.width);
mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mPreviewSize.height);
}

An important thing to remember when working with OpenCV is that we need a lot of RAM for the images we are processing. OpenCV uses matrices to represent all images so to be as efficient as possible, we will allocate the needed matrices once and re-use them for every frame.

Once we have the camera setup and the matrices allocated, we can start to grab preview frames for processing. The following code shows how we do this with OpenCV.

while (mDoProcess && mCamera != null) {
    boolean grabbed = mCamera.grab();
    if (grabbed) {
        mCamera.retrieve(mCurrentFrame,
Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGB);
        // Process mCurrentFrame…
    }
}

Screen shot showing the camera frame. In this example app, you can tap a colour to display that colour only.

How to process a camera frame
We now have a preview frame in mCurrentFrame that we can use for processing. In this case, we will use the function inRange to create a mask that covers only a specific colour interval. For the best result, we will first convert the grabbed frame to HSV-format. Once we have the mask, we draw the original frame to a new matrix using the mask we got from the inRange function. The following code shows how to convert the format from RGB to HSV, generate the colour mask and drawing the result.

// Convert mCurrentFrame to HSV format and store
the result in mCurrentFrameHsv
Imgproc.cvtColor(mCurrentFrame, mCurrentFrameHsv,
Imgproc.COLOR_RGB2HSV);
// Generate the color mask based on the lower and upper
color limits
Core.inRange(mCurrentFrameHsv, mLowerColorLimit,
mUpperColorLimit, mInRangeResult);
// Clear the final image before we copy the new filtered image
mFilteredFrame.setTo(new Scalar(0, 0, 0));
// Copy the original frame using the color mask
mCurrentFrame.copyTo(mFilteredFrame, mInRangeResult);

Screen shot showing the processed camera frame when a colour has been selected. As you can see, only the red colour range is displayed.

How to display the result of the processed image
The resulting bitmap can now be used to draw to a SurfaceView or similar to show the result. The easiest way to do this is to convert the OpenCV matrix to an Android Bitmap object. There is a Util class that provides a function for this.

// Convert the matrix to an Android bitmap
Utils.matToBitmap(result, resultBitmap, true);

From this point, you can simply draw the Bitmap as usual on a SurfaceView, or assign it to an ImageView.

Deeper dive and more OpenCV code examples are available
This was a very quick walkthrough of OpenCV. To get a deeper look into this, you can download more extensive versions of these OpenCV code examples. Feel free to use this anyway you want to experiment with different OpenCV functions. There are lots of more tutorials available on the OpenCV website, and you can find additional code examples at other resources online.

So are you interested in OpenCV? Do you plan to try it out? Let us know in the comments below!

More information

Comments 18

Sort by: Newest | Oldest | Most popular

  1. By Sumit Srivastava

    #1

    Hi,

    I have been trying to use QT to develop OpenCV Android app.

    Created an Android app using OpenCV and QT. The call of method “grab” or “read” of VideoCapture object freezes the further execution of code. Nothing happens after that.

    Note: Consider all required header files included. The code works perfectly on Desktop (Linux)

    int main(int argc, char *argv[]) {
    QApplication app(argc,argv);
    qDebug() << "CP 1";
    Ptr capture;
    capture = new VideoCapture(CV_CAP_ANDROID_FRONT);
    if(capture->isOpened())
    qDebug() << "CP 1.11";
    Mat img;
    qDebug() <read(img); // **code doesn’t moves ahead of this point**
    qDebug() << "CP 1.3";

    Mat gray;
    Mat prev;
    Mat diff;
    cvtColor(img, prev, CV_BGR2GRAY);
    }

    Could you help with it?

  2. By pe la

    #2

    I want to access the QX100’s Liveview with OpenCV. Any ideas?

  3. By Angie Perena

    #3

    I want to learn it because im working on our thesis about Sign Language. We have a feature image recognition that we will take a picture of a hand with the desire gesture then our app will identify what is the meaning of that hand gesture.

  4. By James Hyunwoong Cho

    #4

    I tried to test example on kikat(4.4.2) but there is crash so i cannot run, I used opencv 2.4.8 and changed
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_8, this, new OpenCVLoaderCallback(this));
    I think it seems no problems with jelly bean(4.1.2), I could run successfully. If there is anything I can check, please let me know.

    Error log (crash on GalaxyS4 kikat)

    06-12 19:03:26.103: W/ApplicationPackageManager(24037): getCSCPackageItemText()
    06-12 19:03:26.184: I/Adreno-EGL(24037): : EGL 1.4 QUALCOMM build: (CL3869936)
    06-12 19:03:26.184: I/Adreno-EGL(24037): OpenGL ES Shader Compiler Version: 17.01.11.SPL
    06-12 19:03:26.184: I/Adreno-EGL(24037): Build Date: 01/17/14 Fri
    06-12 19:03:26.184: I/Adreno-EGL(24037): Local Branch:
    06-12 19:03:26.184: I/Adreno-EGL(24037): Remote Branch:
    06-12 19:03:26.184: I/Adreno-EGL(24037): Local Patches:
    06-12 19:03:26.184: I/Adreno-EGL(24037): Reconstruct Branch:
    06-12 19:03:26.294: D/OpenGLRenderer(24037): Enabling debug mode 0
    06-12 19:03:26.314: W/ContextImpl(24037): Implicit intents with startService are not safe: Intent { act=org.opencv.engine.BIND } android.content.ContextWrapper.bindService:529 org.opencv.android.AsyncServiceHelper.initOpenCV:24 org.opencv.android.OpenCVLoader.initAsync:64
    06-12 19:03:26.444: D/OpenCVManager/Helper(24037): Service connection created
    06-12 19:03:26.444: D/OpenCVManager/Helper(24037): Trying to get library path
    06-12 19:03:26.554: D/OpenCVManager/Helper(24037): Trying to get library list
    06-12 19:03:26.614: D/OpenCVManager/Helper(24037): Library list: “”
    06-12 19:03:26.614: D/OpenCVManager/Helper(24037): First attempt to load libs
    06-12 19:03:26.614: D/OpenCVManager/Helper(24037): Trying to init OpenCV libs
    06-12 19:03:26.614: D/OpenCVManager/Helper(24037): Trying to load library /data/app-lib/org.opencv.engine-2/libopencv_java.so
    06-12 19:03:26.614: D/dalvikvm(24037): Trying to load lib /data/app-lib/org.opencv.engine-2/libopencv_java.so 0x42a28018
    06-12 19:03:26.924: D/dalvikvm(24037): Added shared lib /data/app-lib/org.opencv.engine-2/libopencv_java.so 0x42a28018
    06-12 19:03:26.924: D/OpenCVManager/Helper(24037): OpenCV libs init was ok!
    06-12 19:03:26.924: D/OpenCVManager/Helper(24037): First attempt to load libs is OK
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): General configuration for OpenCV 2.4.9 =====================================
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Version control: 2.4.9
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Platform:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Host: Linux 3.2.0-56-generic x86_64
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Target: Linux 1 armv7-a
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): CMake: 2.8.12.1
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): CMake generator: Ninja
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): CMake build tool: /usr/local/bin/ninja
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Configuration: Release
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C/C++:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Built as dynamic libs?: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C++ Compiler: /opt/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-g++ (ver 4.6)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C++ flags (Release): -fexceptions -frtti -Wno-psabi –sysroot=/opt/android-ndk-r8c/platforms/android-9/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fdata-sections -ffunction-sections -Wa,–noexecstack -fweb -fwrapv -frename-registers -fsched2-use-superblocks -fsched2-use-traces -fsched-stalled-insns-dep=100 -fsched-stalled-insns=2 -fno-tree-vectorize -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -mfpu=neon -mthumb -fomit-frame-pointer -fno-strict-aliasing -O3 -DNDEBUG -DNDEBUG
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C++ flags (Debug): -fexceptions -frtti -Wno-psabi –sysroot=/opt/android-ndk-r8c/platforms/android-9/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fdata-sections -ffunction-sections -Wa,–noexecstack -fweb -fwrapv -frename-registers -fsched2-use-superblocks -fsched2-use-traces -fsched-stalled-insns-dep=100 -fsched-stalled-insns=2 -fno-tree-vectorize -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -mfpu=neon -marm -fno-omit-frame-pointer -fno-strict-aliasing -O0 -g -DDEBUG -D_DEBUG -O0 -DDEBUG -D_DEBUG
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C Compiler: /opt/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C flags (Release): -fexceptions -Wno-psabi –sysroot=/opt/android-ndk-r8c/platforms/android-9/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fdata-sections -ffunction-sections -Wa,–noexecstack -fweb -fwrapv -frename-registers -fsched2-use-superblocks -fsched2-use-traces -fsched-stalled-insns-dep=100 -fsched-stalled-insns=2 -fno-tree-vectorize -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -mfpu=neon -mthumb -fomit-frame-pointer -fno-strict-aliasing -O3 -DNDEBUG -DNDEBUG
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C flags (Debug): -fexceptions -Wno-psabi –sysroot=/opt/android-ndk-r8c/platforms/android-9/arch-arm -fpic -funwind-tables -finline-limit=64 -fsigned-char -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=neon -fdata-sections -ffunction-sections -Wa,–noexecstack -fweb -fwrapv -frename-registers -fsched2-use-superblocks -fsched2-use-traces -fsched-stalled-insns-dep=100 -fsched-stalled-insns=2 -fno-tree-vectorize -fsigned-char -W -Wall -Werror=return-type -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wno-narrowing -fdiagnostics-show-option -fomit-frame-pointer -mfpu=neon -marm -fno-omit-frame-pointer -fno-strict-aliasing -O0 -g -DDEBUG -D_DEBUG -O0 -DDEBUG -D_DEBUG
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Linker flags (Release): -Wl,–fix-cortex-a8 -Wl,–no-undefined -Wl,-allow-shlib-undefined -Wl,–gc-sections -fuse-ld=gold -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Linker flags (Debug): -Wl,–fix-cortex-a8 -Wl,–no-undefined -Wl,-allow-shlib-undefined -Wl,–gc-sections -fuse-ld=gold -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Precompiled headers: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): OpenCV modules:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): To be built: core androidcamera flann imgproc highgui features2d calib3d ml objdetect video contrib photo java dynamicuda legacy gpu ocl stitching superres ts videostab
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Disabled: world
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Disabled by dependency: –
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Unavailable: nonfree python viz facedetect imuvstab tegra vstab
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Android:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Android ABI: armeabi-v7a with NEON
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): STL type: gnustl_static
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Native API level: android-9
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): SDK target: android-11
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Android NDK: /opt/android-ndk-r8c (toolchain: arm-linux-androideabi-4.6)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): android tool: /opt/android-sdk-linux/tools/android (Android SDK Tools, revision 22.6.)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Google Play package: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Android examples: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): GUI:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): GTK+ 2.x: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): GThread : NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): GtkGlExt: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): OpenGL support: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): VTK support: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Media I/O:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): ZLib: z (ver 1.2.3)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): JPEG: build (ver 62)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): PNG: build (ver 1.5.12)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): TIFF: build (ver 42 – 4.0.2)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): JPEG 2000: build (ver 1.900.1)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): OpenEXR: build (ver 1.7.1)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Video I/O:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): AndroidNativeCamera: YES, use prebuilt libraries
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Other third-party libraries:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use IPP: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use Eigen: YES (ver 3.1.2)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use TBB: YES (ver 4.1 interface 6102)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use OpenMP: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use GCD NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use Concurrency NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use C=: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use Cuda: YES (ver 6.0)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use OpenCL: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): NVIDIA CUDA
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use CUFFT: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use CUBLAS: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): USE NVCUVID: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): NVIDIA GPU arch: 32
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): NVIDIA PTX archs:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use fast math: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): OpenCL:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Version: dynamic
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Include path: /home/reports/ci/slave_desktop/50-SDK/opencv/3rdparty/include/opencl/1.2
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use AMD FFT: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Use AMD BLAS: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Python:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Interpreter: /usr/bin/python2 (ver 2.7.3)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Java:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): ant: /usr/bin/ant (ver 1.8.2)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Java tests: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Documentation:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Build Documentation: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Sphinx: /usr/local/bin/sphinx-build (ver 1.1.3)
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): PdfLaTeX compiler: /usr/bin/pdflatex
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Tests and samples:
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Tests: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Performance tests: YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): C/C++ Examples: NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): Install path: /home/reports/ci/slave_desktop/50-SDK/build/builds/o4t/install
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): cvconfig.h is in: /home/reports/ci/slave_desktop/50-SDK/build/builds/o4t
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): —————————————————————–
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): General configuration for OpenCV4Tegra =====================================
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): inner version 2.4.9
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): memory allocator YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): hardware link YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): compact sources YES
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): logging enabled NO
    06-12 19:03:26.924: I/OpenCVManager/Helper(24037): —————————————————————–
    06-12 19:03:26.924: D/OpenCVManager/Helper(24037): Init finished with status 0
    06-12 19:03:26.924: D/OpenCVManager/Helper(24037): Unbind from service
    06-12 19:03:26.934: D/OpenCVManager/Helper(24037): Calling using callback
    06-12 19:03:26.934: D/OpenCV::camera(24037): CvCapture_Android::CvCapture_Android(0)
    06-12 19:03:26.934: D/OpenCV::camera(24037): Library name: libopencv_java.so
    06-12 19:03:26.934: D/OpenCV::camera(24037): Library base address: 0x79c89000
    06-12 19:03:26.954: D/OpenCV::camera(24037): Libraries folder found: /data/app-lib/org.opencv.engine-2/
    06-12 19:03:26.954: D/OpenCV::camera(24037): CameraWrapperConnector::connectToLib: folderPath=/data/app-lib/org.opencv.engine-2/
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r4.3.0.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r4.0.3.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r4.4.0.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r2.3.3.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r4.2.0.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r2.2.0.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r4.0.0.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r4.1.1.so
    06-12 19:03:26.954: E/OpenCV::camera(24037): ||libnative_camera_r3.0.1.so
    06-12 19:03:26.954: D/OpenCV::camera(24037): try to load library ‘libnative_camera_r4.4.0.so’
    06-12 19:03:26.954: D/OpenCV::camera(24037): Loaded library ‘/data/app-lib/org.opencv.engine-2/libnative_camera_r4.4.0.so’
    06-12 19:03:26.954: D/OpenCV_NativeCamera(24037): CameraHandler::initCameraConnect(0x7a3aff5d, 0, 0x7894abd0, 0x0)
    06-12 19:03:26.954: D/OpenCV_NativeCamera(24037): Current process name for camera init: com.sonymobile.opencvtutorial
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Instantiated new CameraHandler (0x7a3aff5d, 0x7894abd0)
    06-12 19:03:27.164: I/OpenCV_NativeCamera(24037): initCameraConnect: [ae-bracket-hdr=Off;ae-bracket-hdr-values=Off,HDR,AE-Bracket;anti-shake=0;antibanding=off;antibanding-values=off,50hz,60hz,auto;auto-exposure-lock=false;auto-exposure-lock-supported=true;auto-whitebalance-lock=false;auto-whitebalance-lock-supported=true;cam_mode=0;camera-mode=1;camera-mode-values=0,1;capture-burst-captures-values=2;capture-burst-exposures=;capture-burst-exposures-values=-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12;capture-burst-interval=1;capture-burst-interval-max=10;capture-burst-interval-min=1;capture-burst-interval-supported=true;capture-burst-retroactive=0;capture-burst-retroactive-max=2;contextualtag-cityid=0;contrast=5;denoise=denoise-off;denoise-values=denoise-off,denoise-on;display_mode=landscape;dual_mode=0;dualrecording-hint=-1;effect=none;effect-values=none,mono,negative,solarize,sepia,posterize,whiteboard,blackboard,aqua,emboss,sketch,neon,vintage-warm,vintage-cold,washed;exposure-compensation=0;exposure-compensation-step=0.5;face-detect
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Cameras: (null)
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Picture Sizes: 4128×3096,4128×2322,3264×2448,3264×1836,2048×1536,2048×1152,1280×720,640×480
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Picture Formats: jpeg,raw
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Preview Sizes: 1920×1080,1440×1080,1280×720,1056×864,960×720,720×480,640×480,320×240,176×144
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Preview Formats: yuv420sp,yuv420sp-adreno,yuv420p,nv12
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Preview Frame Rates: 15,30
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Thumbnail Sizes: 512×288,480×288,432×288,512×384,352×288,320×240,176×144,0x0
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Whitebalance Modes: auto,incandescent,fluorescent,daylight,cloudy-daylight
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Effects: none,mono,negative,solarize,sepia,posterize,whiteboard,blackboard,aqua,emboss,sketch,neon,vintage-warm,vintage-cold,washed
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Scene Modes: auto,asd,action,portrait,landscape,night,night-portrait,theatre,beach,snow,sunset,steadyphoto,fireworks,sports,party,candlelight,back-light,flowers,AR,text,fall-color,dusk-dawn
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Focus Modes: auto,infinity,macro,continuous-picture,continuous-video
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Antibanding Options: off,50hz,60hz,auto
    06-12 19:03:27.164: D/OpenCV_NativeCamera(24037): Supported Flash Modes: off,auto,on,torch
    06-12 19:03:27.235: D/OpenCV_NativeCamera(24037): initCameraConnect: autofocus is set to mode “continuous-video”
    06-12 19:03:27.235: D/OpenCV_NativeCamera(24037): initCameraConnect: preview format is set to yuv420sp
    06-12 19:03:27.245: D/OpenCV_NativeCamera(24037): initCameraConnect: preview format is set to 640×480
    06-12 19:03:27.245: A/libc(24037): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 24166 (Thread-23905)

  5. By Sudhindra Magadi

    #5

    Hi ,What are the Real world problems that we can solve with Open CV and mobile phones ?

  6. By Camila Colanjo

    #6

    package com.example.testecores;

    import java.util.List;

    import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
    import org.opencv.android.Utils;
    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.Scalar;
    import org.opencv.highgui.Highgui;
    import org.opencv.highgui.VideoCapture;
    import org.opencv.imgproc.Imgproc;

    import android.graphics.Bitmap;
    import android.hardware.Camera.Size;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    private void setupCamera() {

    VideoCapture mCamera;
    if (mCamera != null) {
    VideoCapture camera = mCamera;
    mCamera = null; // Make it null before releasing…
    camera.release();
    }

    VideoCapture mCameraId;
    mCamera = new VideoCapture(mCameraId);

    List previewSizes = mCamera.getSupportedPreviewSizes();
    double smallestPreviewSize = 1280 * 720; // We should be smaller than this…
    double smallestWidth = 480; // Let’s not get smaller than this…
    org.opencv.core.Size mPreviewSize;
    for (org.opencv.core.Size previewSize : previewSizes) {
    if (previewSize.area() = smallestWidth) {
    mPreviewSize = previewSize;
    }
    }

    mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mPreviewSize.width);
    mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mPreviewSize.height);
    }
    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    Object mCurrentFrameHsv;
    Mat mCurrentFrame;
    // Convert mCurrentFrame to HSV format and store
    // the result in mCurrentFrameHsv
    Imgproc.cvtColor(mCurrentFrame, (Mat) mCurrentFrameHsv, Imgproc.COLOR_RGB2HSV);
    Object mLowerColorLimit;
    Object mInRangeResult;
    Object mUpperColorLimit;
    // Generate the color mask based on the lower and upper
    // color limits
    Core.inRange(mCurrentFrameHsv, mLowerColorLimit, mUpperColorLimit, mInRangeResult);
    Mat mFilteredFrame;
    // Clear the final image before we copy the new filtered image
    mFilteredFrame.setTo(new Scalar(0, 0, 0));
    // Copy the original frame using the color mask
    mCurrentFrame.copyTo(mFilteredFrame, mInRangeResult);
    Bitmap resultBitmap;
    Mat result;
    Utils.matToBitmap(result, resultBitmap, true);
    return null;
    }
    }
    This is not working

  7. By thin norakseiha

    #8

    I do interesting OpenCV, but I have problem with document guide or tutorial.

  8. By Eddie Patton

    #10

    Hi
    I compiled and ran your code for Nexus 7 and “Unfortunately, OpenCVTutorial has stopped”

    I can email you the full log, but here is a snipped when thing start to go bad. If you get a chance to look at it, I’d love to get it running.

    Cheers2u

    01-15 11:15:29.407: I/ActivityManager(509): Start proc com.example.opencvtutorial_sony for activity com.example.opencvtutorial_sony/.OpenCVTutorial_sony: pid=27137 uid=10049 gids={50049}
    01-15 11:15:29.427: D/dalvikvm(27137): Debugger has detached; object registry had 1 entries
    01-15 11:15:29.447: W/dalvikvm(27137): VFY: unable to find class referenced in signature (Lorg/opencv/core/Size;)
    01-15 11:15:29.447: W/dalvikvm(27137): VFY: unable to resolve instance field 1237
    01-15 11:15:29.447: D/dalvikvm(27137): VFY: replacing opcode 0x53 at 0x0006
    01-15 11:15:29.447: W/dalvikvm(27137): Unable to resolve superclass of Lcom/example/opencvtutorial_sony/OpenCVTutorial_sony$OpenCVLoaderCallback; (742)

  9. By Tharaka Nirmana

    #11

    Hi, I am a final year student doing IT. For my final year research, I have to do an android project. It is something to do with the lottery system. All that I want is to extract the lottery numbers and the draw date from the lottery after capturing it from the android camera. I have been trying and trying but still not satisfied. I contacted some indian developers but still I was unable to get what I want. I did post on stack over flow as well:

    1. http://stackoverflow.com/questions/16663504/extract-text-from-a-captured-image?noredirect=1#comment23973954_16663504

    2. http://stackoverflow.com/questions/16874949/extracting-information-from-captured-image-in-android/16880026?noredirect=1#comment24352493_16880026

    3. http://stackoverflow.com/questions/17237676/object-detection-for-android-with-tesseract-or-opencv?noredirect=1#comment24978228_17237676

    Please help me out with this. It would be a great, very great help for me and my group members!!!

    Thank you!

  10. By Narasimha Kaushik

    #12

    Dear Mr Hellman,

    I am keen on developing vision apps on the go on my android phone. I have downloaded the OpenCV manager which comes with the built-in OpenCV 2.4.5. Now which compiler-IDE do I install and how do I integrate the OpenCV library to get started with vision apps?

  11. By Kamil Sindi

    #13

    For some odd reason I’m getting the error:

    E/dalvikvm(22477): Could not find class ‘com.sonymobile.opencvtutorial.OpenCVTutorial$OpenCVLoaderCallback’, referenced from method com.sonymobile.opencvtutorial.OpenCVTutorial.surfaceCreated

    I can clearly see the subclass OpenCVLoaderCallback. Can’t figure out what I’m getting this error.

    • By Kamil Sindi

      #14

      Fyi I get this warning before: Unable to resolve superclass of Lcom/sonymobile/opencvtutorial/OpenCVTutorial$OpenCVLoaderCallback; (60)

      I’m using Android 2.4.1 on Nexus 4 and OpenCV 2.4.3.1.

      • By Erik Hellman

        #15

        Besides the code linked in this tutorial you also need to download the OpenCV Android SDK from opencv.org and include the SDK Library Project before building. Once you’ve downloaded and unpacked the SDK zip-file you can look in the sub-folder sdk/java and you’ll see a standard Android project. This is the Library Project you need to include. If you are using Eclipse you can find the guide for that at http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject

        • By Kamil Sindi

          #16

          Thanks. That was the problem. The library wasn’t being reference correctly.

  12. By Zachary Klippenstein

    #17

    The ant build file in the sample zip apparently points to the author’s home directory:

    BUILD FAILED
    /Users//OpenCVTutorial/build.xml:90: Cannot find /Users/erikhe
    lman/android-sdk-macosx/tools/ant/build.xml imported from /Users//OpenCVTutorial/build.xml

    • By Zachary Klippenstein

      #18

      Never mind, running android update project fixes it.

1-18 of 18 comments.