Imoverlay in python

I am trying to overlay a matrix on another matrix using matplotlib imshow (). I want the overlay matrix to be red and the “background matrix” to dice, color to wise. Thus, just adding images does not work, since you can only have one color palette. I have yet to find another way to "overlay" besides adding matrices using imshow ().

I am trying, more or less, to replace this matlab module.

Please let me know what alternatives I have!

+4
source share
2 answers

Just use a masked array .

eg.

import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl x = np.arange(100).reshape(10,10) y = np.zeros((10,10)) # Make a region in y that we're interested in... y[4:6, 1:8] = 1 y = np.ma.masked_where(y == 0, y) plt.imshow(x, cmap=mpl.cm.bone) plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest') plt.show() 

enter image description here

+5
source

I usually use OpenCV lot, which uses numpy as a backend, so images are just matrices.

To overlay a binary image on a background image, you basically do:

 overlaycolour = [255,0,0] # for red for each channel c in 1:3 overlayimage[:,:,c] = (1-alpha)*backgroundimage[:,:,c] + alpha*binary[:,:,c]*overlaycolour[c] 

Here alpha is the transparency of the overlay (if alpha is 1, the overlay is simply inserted into the image; if alpha is 0, the overlay is invisible, and if alpha is between them, the overlay is a transparent bit).

In addition, either: - the binary image (i.e., the image to be overlaid) is normalized to 1 s and 0 s, and the overlay color is from 0 to 255, OR the binary image is 255 and 0, and the overlay color - from 0 to 1.

Finally, if the background image is gray, turn it into a color image by simply replicating this single channel for each of the red, green, blue channels.

If you want to display a background image on a specific color map, you will need to do this first and apply it to the overlay function.

I am not sure how this code is executed in PIL, but it should not be too complicated.

+1
source

Source: https://habr.com/ru/post/1391958/


All Articles