mata mata - 1 month ago 4x
Python Question

How can I add two or more images using matplotlib?

I read the images using imread and then I would like to compute the average image. How can I add(and divide) using matplotlib?

I'm searching for something like imadd in matlab.


img1 = matplotlib.image.imread("path")
img2 = matplotlib.image.imread("path1")
img3 = matplotlib.image.imread("path2")



You can use the normal sum operations:

img4 = img1 + img2 + img3

This, however, is not exactly the same as imadd from matlab. Matplotlib works with RGB values from 0 to 1. As so the sum in some pixels will provide values superior to 1 (which for the array type is valid; the same would not be true if the data type were uint8). As so perform the following operation to guarantee that your data comes out correct:

img1 = matplotlib.image.imread("path1")
img2 = matplotlib.image.imread("path2")
img3 = np.clip(img1 + img2, 0, 1)

Notice that all images must have the same size.