使图像空白透明,叠加到 imshow() [英] Making image white space transparent, overlay onto imshow()

查看:52
本文介绍了使图像空白透明,叠加到 imshow()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用imshow()显示的空间数据图.

I have a plot of spatial data that I display with imshow().

我需要能够覆盖产生数据的晶格.我有一个png加载为黑白图像的晶格文件.此图像的部分我要叠加是黑色线条,它们是格子,看不到线条之间的白色背景.

I need to be able to overlay the crystal lattice that produced the data. I have a png file of the lattice that loads as a black and white image.The parts of this image I want to overlay are the black lines that are the lattice and not see the white background between the lines.

我想我需要将每个背景(白色)像素的 alpha 设置为透明(0 ?).

I'm thinking that I need to set the alphas for each background ( white ) pixel to transparent (0 ? ).

我对此很陌生,以至于我真的不知道如何提出这个问题.

I'm so new to this that I don't really know how to ask this question.

import matplotlib.pyplot as plt
import numpy as np

lattice = plt.imread('path')
im = plt.imshow(data[0,:,:],vmin=v_min,vmax=v_max,extent=(0,32,0,32),interpolation='nearest',cmap='jet')

im2 = plt.imshow(lattice,extent=(0,32,0,32),cmap='gray')

#thinking of making a mask for the white background
mask = np.ma.masked_where( lattice < 1,lattice ) #confusion here b/c even tho theimage is gray scale in8, 0-255, the numpy array lattice 0-1.0 floats...?

推荐答案

没有你的数据,我无法测试,但是类似

With out your data, I can't test this, but something like

import matplotlib.pyplot as plt
import numpy as np
import copy

my_cmap = copy.copy(plt.cm.get_cmap('gray')) # get a copy of the gray color map
my_cmap.set_bad(alpha=0) # set how the colormap handles 'bad' values
lattice = plt.imread('path')
im = plt.imshow(data[0,:,:],vmin=v_min,vmax=v_max,extent=(0,32,0,32),interpolation='nearest',cmap='jet')

lattice[lattice< thresh] = np.nan # insert 'bad' values into your lattice (the white)

im2 = plt.imshow(lattice,extent=(0,32,0,32),cmap=my_cmap)

或者,您可以处理 imshow RBGA值的NxMx4 np.array ,这样就不必弄糊涂颜色

Alternately, you can hand imshow a NxMx4 np.array of RBGA values, that way you don't have to muck with the color map

im2 = np.zeros(lattice.shape + (4,))
im2[:, :, 3] = lattice # assuming lattice is already a bool array

imshow(im2)

这篇关于使图像空白透明,叠加到 imshow()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆