Monday, April 22, 2019

Migrating Android support library Renderscript code to AndroidX

Android is deprecating the Support compatibility libraries  and replacing them with the new AndroidX libraries. To aid in the migration of Android projects using the deprecated support libraries, Android Studio has a Refactor to AndroidX tool. I used that to migrate an old Android project and while it works for the most part, it did not manage to migrate the files using the support renderscript classes; I presume that is because the AndroidX namespace does not have the equivalent renderscript classes.

To complete the migration to AndroidX, I had to do the following by hand:

  1. In all the code files using the android.support.v8.renderscript.* classes, replace them to android.renderscript.* as shown below.


  2. In the app's build.gradle file, upgrade the renderscriptTargetApi level to 17 and turn off the renderscript support mode (renderscriptSupportModeEnabled to false) as shown below.



  3. Save all the files and rebuild the project.

    The project should manage to sync and build successfully.

Monday, April 15, 2019

Speed up Android Studio startup by disabling unnecessary plugins

Android Studio loads a relatively large number of plugins on start up. Depending on your workflow, some of them may not be needed. For instance, if you don't use Subversion as your source code repository, then the Subversion plugin may not need to be loaded especially if your machine is slow or RAM is limited.

To disable plugins, do the following:

  1. Start Android Studio.


  2. In the Configure drop down menu at the bottom right, choose Plugins.

    The Plugins dialog box appears.

  3. Scroll down the list and toggle off the unneeded plugins e.g. CVS Integration. Click OK.

    The Prompt appears.

  4. Click Restart.

    Android Studio is restarted without the disabled plugins.

Monday, April 8, 2019

Segment individual trees from TLS point clouds with 3DForest

Identifying individual trees from point clouds scanned using Terrestrial LiDAR System (TLS can be easily segmented using this freely downloable software 3DForest. For more information about 3DForest, visit http://www.3dforest.eu/.

Note: although 3DForest has its own ground classification filter, I found it better to perform the ground / non-ground classification using alternative software like PDAL.

To use 3DForest to segment individual trees, the following steps can be done:
  1. Start up 3DForest. Select Project | New Project to create a new project.

    The 3DForest application appears.
  2. In the menu, choose Project | Import | Import Terrain Cloud to load in a LAS file of only ground points.

  3. In the dialog box that pops up, choose a ground class only LAS file, e.g. sample_ground.las.

    The LAS file is converted into a PCD format file and displayed.
  4. Next, choose Project | Import Vegetation Cloud. Choose a non-ground class only LAS file, e.g. sample_vegetation.las.

    The non-ground class LAS file is converted into PCD format and displayed.
  5. In the menu, select Vegetation | Automatic Segmentation to run the individual tree segmentation algorithm.

    The Automatic Segmentation dialog box appears.

  6. In the Input Vegetation Cloud combo box, choose the newly loaded sample_vegetation LAS file.
  7. In the Input Terrain Cloud combo box, choose the loaded sample_ground LAS file.
  8. If necessary, change the parameters, e.g. Prefix of Clouds value from ID to TreeID. Click OK.

    The individual trees are segmented into individual point clouds.


     
  9. If required, select Project | Export | Export Clouds to export the segmented trees to text or PLY formatted files.

Monday, December 3, 2018

Use PDAL to export colored LAS file to Point Cloud XYZ format text file

PDAL can be used to export out a colored laser LAS file like the one in the screenshot below into a text file in Point Cloud XYZ format, which is just an ASCII text file with each point in a row of X, Y, Z, R, G, B values.

To use PDAL to perform the conversion, follow the following steps.
  1. In Windows, open up an OSGeo4W Shell.


  2. In the command prompt, change directory to the folder containing the LAS file e.g. D:\Temp\data\.

  3. Use a text editor to create a PDAL pipeline JSON file, e.g. las2xyz.json, with the following contents.

    {
      "pipeline":[
        {
          "type":"readers.las",
          "filename":"color.las"
        },
        {
          "type":"writers.text",
          "format":"csv",
          "order":"X,Y,Z,RED:0,GREEN:0,BLUE:0",
          "keep_unspecified":"false",
       "quote_header": "false",
       "delimiter": " ",
          "filename":"outputfile.xyz"
        }
      ]
    }
    

    where color.las is the name of the input LAS file
    and outputfile.xyz is obviously the name of the output XYZ file.
  4. In the command prompt, type in the following command to perform the conversion.

    D:\> pdal pipeline las2xyz.json
  5. Press Enter.

    The X, Y, Z, R, G, B values of the LAS file are exported out into the text Point Cloud XYZ file.

Monday, November 26, 2018

How to add a Singapore OneMap tile layer to QGIS

Singapore OneMap provides a wide range of tile maps in difference color schemes that can be used in web map applications (with the proper attribution, of course). For more information about OneMap, visit the website https://docs.onemap.sg/maps/original.html

QGIS has functions to load and display maps from map tile providers such as OneMap. The following shows a few simple steps to connect and add a OneMap original tile layer to the map pane of QGIS.
  1. Start up QGIS.

  2. In the Browser panel, mouse left-click on the Tile Server (XYZ) node. Choose New Connection.

    The New XYZ tile layer dialog box appears.
  3. In the text field, type in the URL of a OneMap tile layer, e.g. https://maps-a-onemap.sg/v3/Original/{z}/{x}/{y}.png.
  4. Click OK.

    The New XYZ tile layer dialog box appears.
  5. In the text field, type in a meaningful name, e.g. OneMap. Click OK.

    The tile layer is added to the Browser panel.
  6. To display it in the map view, mouse double click on the added layer in the Browser panel.

    The OneMap tile layer is displayed.


Monday, October 29, 2018

Setup CMakeLists.txt for Android Studio and OpenCV C++ coding

If you want to develop C++ code making use of Android and OpenCV classes, then you have to configure Android Studio's CMakeLists.txt build file accordingly.

Please follow the previous post https://dominoc925.blogspot.com/2018/10/how-i-setup-opencv4android-java-sdk.html to download and setup the OpenCV4Android Java SDK with and Android project before doing the following steps.
  1. In Android Studio, open up the app's build.gradle file.

    Note: the OpenCV native libraries are under the jniLibs directory relative to the app's build.gradle file according to the source set jniLibs srcDirs parameter.
     

  2. Enter the cmake cpp flags and platform abi filters  in the android defaultConfig externalNativeBuild section, as shown below.

    externalNativeBuild {
        cmake {
          cppFlags "-frtti -fexceptions"        
    
    abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
    }
    }

  3. Next, open up the CMakeLists.txt file in the editor.

     
  4. Add in the cmake macros to add in the path to the OpenCV C++ include files e.g. /path/to/OpenCV-android-sdk/sdk/native/jni/include/

     
  5.  Then, use the macro add_library to add the OpenCV library.

  6. Next, use the macro set_target_properties to tell CMake the location of the OpenCV library.

    set_target_properties(
         lib_opencv        
    
    PROPERTIES
    IMPORTED_LOCATION
    ${CMAKE_CURRENT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libopencv_java3.so
    )


  7. Finally, link the OpenCV library with your code with the target_link_libraries macro.

The full sample of the CMakeLists.txt file is shown below.

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

set (OPENCV4ANDSDK_INCLUDE
        /path/to/OpenCV-android-sdk/sdk/native/jni/include)
include_directories(
    ${OPENCV4ANDSDK_INCLUDE}
)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        src/main/cpp/native-lib.cpp)
add_library(
        lib_opencv
        SHARED
        IMPORTED
)
set_target_properties(
        lib_opencv
        PROPERTIES
        IMPORTED_LOCATION
        ${CMAKE_CURRENT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/libopencv_java3.so
)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)


# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib}
        lib_opencv
        )




Monday, October 22, 2018

How I setup OpenCV4Android Java SDK with a blank Android project

Setting up an Android Studio project with OpenCV3 can be a little tricky. The official documentation is still using Eclipse and has not been updated with Android Studio. So setting up OpenCV with Android Studio required some trial and error experimentation. I managed to get a template Android Studio project to load the native OpenCV library with the following steps.

Download OpenCV4AndroidSdk
  1. If you have not done so, download OpenCV4Android from this website https://sourceforge.net/projects/opencvlibrary/files/opencv-android/
  2. Extract the files into a folder e.g. /path/to/Open-CV-android-sdk/
Create an Android Project with C++ Support
  1.  Run Android Studio. Choose Create a New Project.

  2. In the Create New Project dialog box, toggle on Include C++ support.
  3. Choose appropriate options and click Next in the following screens.
  4. Click Finish.

    The new project is created.
Import the OpenCV module into Gradle
  1. In the Android Studio File menu, select File | New | Import Module.

    The Import Module from Source dialog box appears.
  2. In the Source directory field, click the ... button and browse to the downloaded OpenCV4Android files and select the java folder e.g. /path/to/OpenCV-android-sdk/sdk/java/.
  3. Optional. In the Module name field, type in a name e.g. :opencv.
  4. Click Next and Finish.

    The module is imported (with most likely error messages)
  5. Options. You can download the missing dependencies by clicking the Install missing platforms and sync project link. Or you can edit the OpenCV4Android's build.gradle file to enter the installed dependencies.
Fix OpenCV4Android configuration files
  1. In Android Studio, open up the file build.gradle from the imported module opencv module e.g. /path/to/opencv/build.gradle. It may be necessary to change the Project pane to display as project view.

  2. In the editor, changed the compiledSdkVersion to 27 (or any installed version), the targetSdkVersion to 27 (or any installed version).
  3. Then sync the gradle project.

    The project is synced but now there is an error about the manifest file.
  4. Open up the manifest file /path/to/opencv/src/main/AndroidManifest.xml in the editor.

  5. Now, comment out the uses-sdk line as shown in the screenshot above.
  6. Sync the project again.

    The OpenCV module is successfully configured.
Add the OpenCV module as a dependency to the Android App project
  1. In Android Studio, select App in the Project pane.
  2. Select File | Project Structure.

    The Project Structure dialog box appears.
  3. Select app. Then click the Dependencies tab. Click +. Choose module dependency.
  4. Choose the imported Opencv module.

    OpenCV is appended to the list.
  5. Click OK.

    The module is added the Android App project.
Copy over the OpenCV native platform binaries to the Android App project
  1.  Open up a File Explorer. Select all the native platform folders under the /path/to/OpenCV-android-sdk/sdk/native/libs/*.

  2. Right click and select Copy.
  3. In Android Studio, right click on the app folder (in project view). Choose Paste.

    The Copy dialog box appears.
  4. In the To directory field, change the text to /path/to/myandroid/app/jniLibs/.
  5. Click OK.

    The native binaries are copied to the project.
  6. To enable the Android project to know where the native binaries are, open up the app's build.gradle file in the editor.

  7. Add in the android class, add in the following sourceSets lines:

    Note: 'jniLibs' means the jniLibs directory relative to this build.gradle file location.
    sourceSets {
        main {
            jniLibs.srcDirs = ['jniLibs']
        }
    }
  8. Sync the project.
Load the native library
  1.  In the MainActivity Kotlin class, type in the following to load in the OpenCV library.

    System.loadLibrary("opencv_java3")
  2. If everything is configured properly, then the Android app can be launched successfully.

    Now you can make any java/Kotlin calls to the built in OpenCV interfaces.
Related Posts with Thumbnails