Why don't you use imshow instead?
You can plot a 2D image by doing:
plt.imshow(Image1, cmap='gray') # I would add interpolation='none'
Afterwards, you can easily overlay the segmentation by doing:
plt.imshow(Image2_mask, cmap='jet', alpha=0.5) # interpolation='none'
Changing the alpha will change the opacity of the overlay.
Additionaly, why do you create 2 masks? Only one should be enough, you can do:
Image2_mask = ma.masked_array(Image2 > 0, Image2)
Practical example:
import numpy as np
mask = np.zeros((10,10))
mask[3:-3, 3:-3] = 1 # white square in black background
im = mask + np.random.randn(10,10) * 0.01 # random image
masked = np.ma.masked_where(mask == 0, mask)
import matplotlib.pyplot as plt
plt.figure()
plt.subplot(1,2,1)
plt.imshow(im, 'gray', interpolation='none')
plt.subplot(1,2,2)
plt.imshow(im, 'gray', interpolation='none')
plt.imshow(masked, 'jet', interpolation='none', alpha=0.7)
plt.show()

Top answer 1 of 4
100
Why don't you use imshow instead?
You can plot a 2D image by doing:
plt.imshow(Image1, cmap='gray') # I would add interpolation='none'
Afterwards, you can easily overlay the segmentation by doing:
plt.imshow(Image2_mask, cmap='jet', alpha=0.5) # interpolation='none'
Changing the alpha will change the opacity of the overlay.
Additionaly, why do you create 2 masks? Only one should be enough, you can do:
Image2_mask = ma.masked_array(Image2 > 0, Image2)
Practical example:
import numpy as np
mask = np.zeros((10,10))
mask[3:-3, 3:-3] = 1 # white square in black background
im = mask + np.random.randn(10,10) * 0.01 # random image
masked = np.ma.masked_where(mask == 0, mask)
import matplotlib.pyplot as plt
plt.figure()
plt.subplot(1,2,1)
plt.imshow(im, 'gray', interpolation='none')
plt.subplot(1,2,2)
plt.imshow(im, 'gray', interpolation='none')
plt.imshow(masked, 'jet', interpolation='none', alpha=0.7)
plt.show()

2 of 4
12
Completing the Imanol Luengo's answer : masking image could be directly handled in imshow alpha option by putting an alpla image ie.
plt.imshow(Image1, cmap='gray') # I would add interpolation='none'
plt.imshow(Image2, cmap='jet', alpha=0.5*(Image2>0) ) # interpolation='none'
Matplotlib
matplotlib.org βΊ stable βΊ gallery βΊ images_contours_and_fields βΊ layer_images.html
Layer images with alpha blending β Matplotlib 3.11.2 documentation
import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
10:06
OpenCV Python Image Overlay - YouTube
00:59
Overlaying images for easy comparison (in python) - YouTube
Overlaying images for easy comparison (in python)
How to Overlay 2 Images Using Python Tutorial! - YouTube
33:53
PNG Overlay and Image Rotation using OpenCV | CVZone - YouTube
19:38
Adding a Picture Overlay on Video with Python and OpenCV | #108 ...
Plotly
community.plotly.com βΊ π plotly python
How to overlay two images with opacity - π Plotly Python - Plotly Community Forum
June 7, 2023 - I have a simple problem, I want to overlay two rgb images (np.arrays with three channels and same size) over each other, with opacity setting for the top so I can see both. fig_visual_check = go.Figure() fig_visual_check = fig_visual_check.add_trace(go.Image(z=images_merged_visual[0], opacity=0.5)) fig_visual_check = fig_visual_check.add_trace(go.Image(z=images_merged_visual[1], opacity=1)) fig_visual_check However, the result is just a black image.
Matplotlib
matplotlib.org βΊ 3.1.1 βΊ gallery βΊ images_contours_and_fields βΊ layer_images.html
Layer Images β Matplotlib 3.1.2 documentation
May 1, 2020 - import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
Stack Overflow
stackoverflow.com βΊ questions βΊ 76623437 βΊ how-to-overlay-merge-tow-images-using-plt-image
matplotlib - How to overlay/ merge tow images using plt.image - Stack Overflow
Copyimport matplotlib.pyplot as plt import numpy as np from PIL import Image cm = plt.cm.RdBu_r cm.set_under('white') # load both images frame1 = np.asarray(Image.open('polarbear.png')) frame2 = np.asarray(Image.open('dog.png')) # overlay both images using an alpha fig, ax = plt.subplots() ax.imshow(frame1, cmap=cm, vmin=12, alpha=0.5) ax.imshow(frame2, alpha=0.5) ax.set_axis_off() # display result #plt.savefig('plot_output.png') plt.show() ... Sign up to request clarification or add additional context in comments. ... the thing I can not maintain the 'white' background as I am plotting two trajectories 2023-07-05T20:58:47.683Z+00:00
Moonbooks
moonbooks.org βΊ Articles βΊ How-to-overlay--superimpose-two-images-using-python-and-pillow-
How to overlay / superimpose two images using python and pillow ?
August 24, 2022 - from PIL import Image import numpy as np img = Image.open("data_mask_1354_2030.png") background = Image.open("background_1354_2030.png") background.paste(img, (0, 0), img) background.save('how_to_superimpose_two_images_01.png',"PNG")
CSDN
devpress.csdn.net βΊ python βΊ 630452787e66823466199c6f.html
Overlay an image segmentation with numpy and matplotlib_python_Mangs-Python
August 23, 2022 - I am coming from a Matlab environment and I am quite new to Python. ... Changing the alpha will change the opacity of the overlay. Additionaly, why do you create 2 masks? Only one should be enough, you can do: ... import numpy as np mask = np.zeros((10,10)) mask[3:-3, 3:-3] = 1 # white square in black background im = mask + np.random.randn(10,10) * 0.01 # random image masked = np.ma.masked_where(mask == 0, mask) import matplotlib.pyplot as plt plt.figure() plt.subplot(1,2,1) plt.imshow(im, 'gray', interpolation='none') plt.subplot(1,2,2) plt.imshow(im, 'gray', interpolation='none') plt.imshow(masked, 'jet', interpolation='none', alpha=0.7) plt.show()
GitHub
github.com βΊ RemiLehe βΊ transparent_imshow
GitHub - RemiLehe/transparent_imshow: Pixel-dependent transparency with matplotlib's imshow
This is a simple wrapper around matplotlib's imshow function, which allows to produce images with pixel-dependent transparency. This can be particularly useful to overlay several images. The solution implemented was inspired by this post. Below is a simple example that shows how to overlay two arrays of data, by using the function transp_imshow which is provided in this package.
Starred by 3 users
Forked by 2 users
Languages: Python 100.0% | Python 100.0%
Finxter
blog.finxter.com βΊ 5-effective-ways-to-plot-a-layered-image-in-matplotlib-in-python
5 Effective Ways to Plot a Layered Image in Matplotlib in Python β Be on the Right Side of Change
March 6, 2024 - In this snippet, a base layer is created as a grayscale image showing the sine-cosine function values. The contourf() function then overlays this with filled contour lines using a different color map and sets the transparency using the alpha parameter. The add_axes() method permits precise control over the axes of the plots, allowing for the addition of extra layers in exact positions. This approach is helpful when precise alignment of layers is needed. ... import matplotlib.pyplot as plt import numpy as np # Create the base figure fig = plt.figure() # Add first subplot, this will be the base layer ax1 = fig.add_subplot(111) # Add a second subplot as an overlay, position identical to ax1 ax2 = fig.add_axes(ax1.get_position(), frameon=False) # Generate some data x = np.arange(0, 10, 0.1) y1 = np.exp(x) y2 = np.sin(x) ax1.plot(x, y1, 'b-') ax2.plot(x, y2, 'r-', alpha=0.5) plt.show()
Matplotlib
matplotlib.org βΊ 2.0.2 βΊ examples βΊ pylab_examples βΊ layer_images.html
pylab_examples example code: layer_images.py β Matplotlib 2.0.2 documentation
""" Layer images above one another using alpha blending """ from __future__ import division import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x/2 + x**5 + y**3)*np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
TutorialsPoint
tutorialspoint.com βΊ overlay-an-image-segmentation-with-numpy-and-matplotlib
Overlay an image segmentation with Numpy and Matplotlib
Use imshow() method to display data as an image, i.e., on a 2D regular raster. To display the figure, use show() method. from matplotlib import pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True mask = np.zeros((10, 10)) ...
Matplotlib
matplotlib.org βΊ 3.5.0 βΊ gallery βΊ images_contours_and_fields βΊ layer_images.html
Layer Images β Matplotlib 3.5.0 documentation
import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
Matplotlib
matplotlib.org βΊ 3.1.0 βΊ gallery βΊ images_contours_and_fields βΊ layer_images.html
Layer Images β Matplotlib 3.1.0 documentation
November 12, 2020 - import matplotlib.pyplot as plt import numpy as np def func3(x, y): return (1 - x / 2 + x**5 + y**3) * np.exp(-(x**2 + y**2)) # make these smaller to increase the resolution dx, dy = 0.05, 0.05 x = np.arange(-3.0, 3.0, dx) y = np.arange(-3.0, 3.0, dy) X, Y = np.meshgrid(x, y) # when layering multiple images, the images need to have the same # extent.
Stack Exchange
gis.stackexchange.com βΊ questions βΊ 443995 βΊ python-imshow-will-not-overlay-only-display-last-lines-map
matplotlib - Python imshow will not overlay -- only display last line's map - Geographic Information Systems Stack Exchange
October 30, 2022 - The key would be transparency. You need to make one of the images transparent. That could possibly done by setting an alpha value, see this Stackexchange answer: https://stackoverflow.com/questions/10127284/overlay-imshow-plots-in-matplotli...

