Friday, January 30, 2015

Using gdalwarp to mosaic adjacent geo-referenced images

I had to merge adjacent 1 km by 1 km GeoTiff ortho-rectified mosaic images into 2 km by 2 km tiles and I really did not want to run the ortho-rectification and mosaicking processes again just to resize the tiles. After some digging around I found that GDAL has a few tools to perform the merging. I chose to use the gdalwarp executable, as it allows me to define a list of images to merge and the destination image bounds.

The following example shows how to use gdalwarp to merge 2 adjacent GeoTiff images into one.
Two adjacent image tiles to merge

  1. Open up a Command Prompt. Type in the gdalwarp command to merge two files e.g. left.tif and right.tif and output to a new file mosaic.tif:

    C:< gdalwarp left.tif right.tif mosaic.tif
  2. Press Return.

    Processing messages appear and the files are merged.

    The resultant merged GeoTiff image

Wednesday, January 21, 2015

How to mirror or project an Android device screen to a Mac

I wanted to be able to project the screen of an Android handset on to a Macbook to demo an app to an audience. I found a post for a Windows PC on this site but not for a Mac. The post describes downloading and using the Android SDK platform tools along with a Jar file to do the projection to a PC. I thought that the same Jar file could be used on a Mac so I tried out the idea, and it turned out to be functional. So here are the steps to get it to work on a Mac. I am assuming that the following has been done:

  • The Java SE Runtime Environment (JRE) has been installed
  • The Android SDK has been installed to a folder on the Mac e.g. /home/users/admin/Application/sdk/

  1. Download the Jar file Put the Jar file to the same location as the Android SDK's adb executable e.g. /home/users/admin/Application/sdk/platform-tools/.

    Note: the actual souce code is available on
  2. Connect an Android device to the Mac. If necessary, enable USB debugging on the device.
  3. Open up a Terminal.

  4. In the Terminal, type in the cd command to change to the location of the Jar file.

    $ cd /home/users/admin/Application/sdk/platform-tools
  5. Set the Path environment variable to include the Android SDK's platform-tools folder. In the Terminal, type in the export command.

    $ export PATH=$PATH:/home/users/admin/Application/sdk/platform-tools
  6. In the Terminal, run the Jar file.

    $ java -jar \[DMZilla\]-Android-Screen-Monitor-\(ASM\).jar
    The Select a Android Device prompt appears.
  7. In the Select a Android Device prompt, click the device label. Click OK.

    The Android Screen Monitor displays the screen contents of the Android device.
  8. Now on the Android device, run any app you want to present and project to the Mac screen.

Monday, January 5, 2015

Enabling ProGuard obfuscation in Android Studio

In Android Studio, it is possible to use ProGuard to minify and obfuscate the Java source code from decompilation. The settings are different from the Eclipse ADT as Android Studio uses the Gradle build system. But still as straightforward provide you know where and what to change. At the minimum, you only need to enable ProGuard but sometimes it may be necessary to define classes that you do not want ProGuard to obfuscate - this can be done in a ProGuard rules file. The following steps illustrate these two tasks.

Enable ProGuard

  1.  In Android Studio, open up an Android project.
  2. Change to Project View.

  3. Edit the app's build.gradle file.

    Note: make sure the correct build.gradle file is edited. Typically, it is [Android project]/app/build.gradle.
  4. Change the following line:

    minifyEnable false


    minifyEnable true
Set ProGuard Rules(optional)
  1. In Project View, select the file.

    Note: Typically it is [Android project]/app/

  2. Add in the following lines to tell ProGuard not to obfuscate certain classes. 

-keepclassmembers class com.dom925.xxxx {
   public *;

Now when a release build is generated using Android Studio, the app will be obfuscated. 

Monday, December 29, 2014

Trainsity Osaka Android App

Find your way around Osaka city's subway using the high resolution vector maps of the train network. The following metro rail lines are available:

  • Midosuji, 
  • Tanimachi, 
  • Yotsubashi, 
  • Chuo, 
  • Sennichimae, 
  • Sakaisuji, 
  • Nagahori-Tsurumi-ryokuchi, 
  • Imazatosuji, 
  • Newtram Nanko Port Town, 
  • Kita Osaka Kyuko, 
  • Kintetsu Keihanna, 
  • Hankyu Kyoto, 
  • Hankyu Senri, 
  • Osaka Monorail, and 
  • Kitakyu Namboku lines. 
The maps have small file size footprints but with many levels of zoom and can work offline without connecting to the Internet. Users can click the train station box labels to open Google Maps or Google StreetView, where they can use all the functions of the Google apps to visualize the surrounding area and/or perform routing for directions. There is also an offline function to calculate the best way to travel from a station to another station with a direction breakdown and timings (which may vary from the actual travelling time).

On a mobile handset, the app will display a list of train maps, which when tapped will open up a detail view of the subway transit map, as shown below.

Tapping the statin boxes will bring up an option menu where users can choose to display the station in Google Maps or Street View.

There is also an option to find the best direction to or from the tapped stations.

The app is also optimized for tablet sized devices. Both the list and the vector map are displayed at the same time, as shown below.

 The user can toggle the map to full screen mode by tapping the action bar icon at the top right corner.

The app can be downloaded from the Google Play Store. Just click the button below.
Get it on Google Play

Monday, December 15, 2014

Create a Colored Infra Red (CIR) image from a multi-channel RGBI TIFF file using ImageMagick

Some remote sensing imagery TIFF files contain multiple bands of data such as red, green, blue, and infra-red data. In order to visualize these bands, it is necessary to combine them into an RGB format such as what is called a Colored Infra Red (CIR) display. The open source software ImageMagick can be used to generate the CIR display from the multi-channel TIFF files.

This post illustrates how to create a CIR image from an input RGBI TIFF file. In order to generate a CIR image, the input channels - RGBI has to be reordered to IRG i.e.

  • the file's output red channel is replaced with the input Infra red channel, 
  • the output green channel is replaced with the input red channel, 
  • the output's green channel is replaced with the input green channel, 
  • and the input blue channel is essentially thrown away. 

The ImageMagick command to do this is quite simple, as shown below.

$ convert input_rgbi.tif -channel rgba -separate -swap 1,2 -swap 0,1 -swap 0,3 -channel rgb -combine output_cir.tif

Note: the sequence of options is important.

  • The option -channel rgba instructs the command to read in 4 channels.
  • The option -separate splits the input channels
  • The option -swap 1,2 swaps the 2nd and 3rd channels from RGBI to RBGI
  • The option -swap 0,1 swaps the 1st and 2nd channels from RBGI to BRGI
  • The option -swap 0,3 swaps the 1st and 4th channels from BRGI to IRGB
  • The option -rgb removes the 4th channel to become IRG
  • The option -combine merges all the 3 channels back
The input RGBI file (only the RGB channels are displayed)
The CIR image generated from ImageMagick

Monday, December 8, 2014

Correcting color cast problems in aerial photographs using curves in Gimp

In some cases, it may be necessary to correct color cast problems from aerial photographs due to atmospheric haze or sensor problems. An example of an image with a color cast problem is shown below. You can download the file from,_1966.jpg.

Note: aerial photograph courtesy of

The Curves correction function in just about any image processing software can be used to easily correct the color cast problem. The open source application Gimp is used in this example.

The key to solving the problem is to use the fact that the color gray has the same RGB values. If a gray colored feature in the image e.g. an asphalt road has unequal RGB values, than the image has a color cast problem. To correct the problem, all you have to do is to get the RGB values for that gray point in the image to be the same using the Curves function.  It may be necessary to do the correction in the middle, dark and light tones. The following example illustrates how to correct the color cast problem.

  1. Start Gimp and open the aerial photograph. Optional. Make a duplicate layer of the image.

  2. If the Pointer window is not displayed, then select Windows | Dockable Dialogs | Pointer.

    Note: the circled area displays the RGB in pixel or percentage values.
  3. Move the pointer around the mid tone gray colored feature e.g. paved road, as shown in the screenshot below. Observe the RGB values in the Pointer window.

    Note: in this example, the RGB values are 142, 140, 189 or 55%, 55%, 74%. The numbers say that there is a blue cast in the mid tone values i.e. the blue values are too high and will have to be lowered. The red and green values are okay. 
  4. Select Colors | Curves.

    The Adjust Color Curves dialog box appears.
  5. Since blue is the problem color, select Blue in the Channel field. Then drag down the blue curve at around 189 pixel value as shown in the screenshot below. You can also move the cursor to the mid tone gray feature and check the new RGB values in the Pointer window. The correction is complete when the RGB values are about equal.

    Note: if the preview is toggled on, the effect of the changes will be displayed in the image window.
  6. In this example, the light tones do not seem to show much color cast. However, the dark tones don't seem to have enough red values as shown in the screenshot below. The dark area under the bridge show the RGB values of 1, 18, 17 or 0%, 7%, 7%.

  7. In the Adjust Color Curves dialog box, change the Channel to Red. Click a point in the middle of the curve to anchor the mid and light tone red values since the problem is only in the dark tones. Then drag the 0 pixel curve point up to 17 pixel value.

    Note: after every change, always move the cursor to the dark area and check the RGB values in the Pointer window.
  8. When the numbers and preview look pleasing, click OK to confirm the changes.

    The image shows the corrected colors.

    Contrast this to the original color casted version.

Monday, December 1, 2014

After adding an Android Library Module to a project, Android Studio does not launch the default app activity

I was following the official Android Studio documentation on building an Android project with Gradle but every time I run the app on my device, the activity for the library module kept on appearing no matter what changes I made to the app's default activity. The instruction added a button to the default activity and I was expecting a button to appear but what came out was as shown below.

The documentation did not mention anything about this. And after thinking through the problem, I figured that the Run configuration in the Android Studio needs to be told to run the app's activity. The following steps show how to change the Run Configuration.

  1. In Android Studio, select Edit Configuration.

    The Run/Debug Configuration dialog box appears.

  2. In the Activity group box, toggle on Launch. Then click browse [...].

    The Select Activity Class dialog appears.

  3. Select the desired app's activity e.g. MainActivity. Click OK.

  4. Click OK.
  5. In Android Studio, run the app again.

    This time, the correct activity with the added button appears.
Related Posts with Thumbnails