I want to add several images such that their change is shown in different colors. Such as the following in which I simply added two images in their grayscale. But I would like to have a colored representation of change when several images are added together. For instance for 50 images, I have their difference depicted in 50 different colors.

My code to add grayscale images is as follows:

imageA = cv2.imread('/2013.tif')
imageB = cv2.imread('/2014.tif')

grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

result = cv2.add(imageA,imageB)

image A

image B


Is there any opensource approach using Python/R for that?


I am not sure this is the solution you are looking for, but I think it works. If you want to show the change between rasters, this accomplishes that goal. This is the workflow for comparing the change between 2 rasters.

The workflow is: 1. Convert rasters to vector using the GDAL: Polygonize tool 2. Use the QGIS: Difference tool to compare change(found under vector > geoprocessing > difference)

Here are a couple screenshots to illustrate the point. Step 1, two rasters overlaid on top of eachother: enter image description here

Step 2, convert them both to polygons. They still look mostly the same, except now they are vector objects: enter image description here

Step 3, use Difference tool to show change between them. This is the output of that tool by itself: enter image description here

Here is a short python script to do this, showing an example of how to use these tools for a single instance. If you are comparing 50 rasters, you will have to loop through multiple files.

import processing

#print(help(processing.algorithmHelp('gdal:polygonize')   #for help documentation

#run GDAL:polygonize tool
polygonize_parameters = {
    'INPUT': '/Users/ep9k/Desktop/RasterLayer.tif',    #change this file path to your own
    'BAND': 1,
    'FIELD': 'DN',
    'OUTPUT': '/Users/ep9k/Desktop/Polygonized.gpkg'}

processing.runAndLoadResults('gdal:polygonize', polygonize_parameters)

#print(help(processing.algorithmHelp('qgis:difference')   #for help documentation

#run qgis:difference tool
difference_parameters = {
    'INPUT': '/Users/ep9k/Desktop/Polygon1.gpkg',
    'OVERLAY': '/Users/ep9k/Desktop/Polygon2.gpkg',
    'OUTPUT': 'ogr:dbname=\'/Users/ep9k/Desktop/Difference.gpkg\' table=\"Diff\" (geom) sql='}

processing.runAndLoadResults('qgis:difference', difference_parameters)

One last tip, if you run these tools by themselves using the QGIS widget, you can steal the python code from the log tab.

Is this the sort of answer you are looking for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.