Blending images

We can make our results even more visually appealing and informative if we can combine the colorized image with the shaded relief image. Again, since we are dealing with arrays, we may deduce that this kind of composition can be achieved by performing an arithmetic operation between the two arrays.

In image processing, this is called alpha blending. Basically, a transparency is applied to both of the images and then they are blended into a new one. In the next steps, we are going to create a function that performs this operation:

  1. First, to avoid generating the shaded relief multiple times, let's save it on the disk and edit the if __name__ == '__main__': block of the raster_data.py file:
    if __name__ == '__main__':
        raster_data = RasterData('output/dem.tif')
        raster_data.adjust_values().create_hillshade(
            10, 60).write_image('output/shaded.png')
  2. Run the code and check whether the image was correctly written on the disk.
  3. Now, add the alpha_blend method to the RasterData class:
    #...
        def alpha_blend(self, raster_data, alpha=0.5):
            """Blend this raster data with another one.
    
            :param raster_data: RasterData instance.
            :param alpha: Amount of transparency to apply.
            """
            shade = cv2.cvtColor(raster_data.data, cv2.COLOR_GRAY2BGR)
            result = (1-alpha) * self.data + alpha * shade
            self.data = result
            return self
  4. Finally, edit the if __name__ == '__main__': block again to test the code:
    if __name__ == '__main__':
        shaded = RasterData('output/shaded.png')
        classified = RasterData('output/classified.png')
        classified.alpha_blend(shaded).write_image(
            'output/color_shade.png')
  5. Run the code and check the image in the output folder:
    Blending images

You should see this beautiful output. Note how the combination of the shaded relief with the colorized image produces a map that transmits a lot of information even for untrained eyes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.253