Skip to main content

merge a 3-band and 1-band image

Doodler can use 1, 3, and 4-band input imagery. If the imagery is 3-band, it is assumed to be RGB and is, by default, augmented with 3 additional derivative bands.

But how do you make a 4-band image from a 3-band image and a 1-band image?

That additional 1-band might be that acquired with an additional sensor, but might more commonly be a DEM (Digital Elevation Model) corresponding to the scene.

I know of two ways. If you have gdal binaries installed, first strip the image into its component bands using gdal_translate

gdal_translate -b 1 data/images/4_rgb.png red.png
gdal_translate -b 2 data/images/4_rgb.png green.png
gdal_translate -b 3 data/images/4_rgb.png blue.png

Then merge them together using gdal_merge.py

gdal_merge.py -separate -o merged.tiff -co PHOTOMETRIC=MINISBLACK red.png green.png blue.png data/images/4_elev.png

The equivalent in python can be acheived without the gdal bindings, using the libraries already in your doodler conda environment

First, import libraries

import tifffile
import cv2
import numpy as np

Read RGB image

im1 = cv2.imread('data/images/4_rgb.png')

Read elevation and get just the first band (if this is 3-band)

im2 = cv2.imread('data/images/4_elev.png')[:,:,0]

If you had a 1-band elevation image, it would be this instead...

im2 = cv2.imread('data/images/4_elev.png')

Merge bands - creates a numpy array with 4 channels

merged = np.dstack((im1, im2))

Write the image to file

tifffile.imsave('test.tiff', merged)

You can use the following to read it back in

merged = tifffile.imread('test.tiff')

And verify with 'shape' - it should be 4 bands

merged.shape