使用python在3d图中叠加图像 [英] Image overlay in 3d plot using python

查看:46
本文介绍了使用python在3d图中叠加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 matplotlib 生成的 3d 线条图.我想在特定的 xy(或 yz、xz)切片上覆盖图像.我如何使用 python 做到这一点?谢谢.

I have a 3d plot of lines generated by matplotlib. I want to overlay an image at a specific xy (or yz, xz) slice. How do I do that using python? Thanks.

我有一个简单的 3d 绘图代码:

I have a simple 3d plot code as:

fig = plt.figure(1),<br>
ax = Axes3D(fig)<br>
ax.plot(f[:,0], f[:,1], f[:,2], color='r')

我也有一个图像Im"(一个二维数组),所以我需要类似的东西:

I also have an image "Im" (a 2d array), so I need something like:

ax.overlay(Im, slice='xy', sliceNo=10)

推荐答案

我曾经在背景图像上做了一个 3d 表面图叠加:

I did a 3d surface plot overlay on top of a background image once:

如果这与您想要的相似,我可以尝试从中制作一个可行的示例.

If this is similar to what you want, I could try to make a working example out of it.

或者,如果您只想在 3d 空间中显示图像,您可以使用曲面图:

Alternatively, if you just want to display an image in 3d space, you can use a surface plot:

from pylab import *
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.cbook import get_sample_data
from matplotlib._png import read_png
fn = get_sample_data("lena.png", asfileobj=False)
img = read_png(fn)
x, y = ogrid[0:img.shape[0], 0:img.shape[1]]
ax = gca(projection='3d')
ax.plot_surface(x, y, 10, rstride=5, cstride=5, facecolors=img)
show()

当然,为了获得更好的图像质量,步幅值可以减少到 1,但这样绘图会花费 loooong =)

Of course, the stride values can be decreased to 1 for better image quality, but then drawing will take loooong =)

上面代码的结果图像:

2020 年 11 月

Edit Nov 2020:

因为它似乎很有趣,所以这是我用来生成第一张图像的代码(这是光激发后多晶硅晶片中少数电荷载流子的衰减):

Since it seems to be of interest, here's the code I used to generate the first image (which is the minority charge carrier decay in a multicrystalline silicon wafer after photo-excitation):

bg_img = Image.open(datadir + "DSC_1495_dark.jpg")
bg_img = bg_img.crop((0, 0, 4000, 2848))
dpi = pl.rcParams['figure.dpi']
figsize = float(bg_img.size[0]) / dpi, float(bg_img.size[1]) / dpi

fig = pl.figure(figsize=figsize)
ax = pl.axes([0, 0, 1, 1], frameon=False)
ax.set_axis_off()
im = pl.imshow(bg_img)

ax = pl.axes([0.01, -0.005, 1.01, 1], projection='3d')
data = (loadtxt(datadir + "pl-image.txt")[14:950, 14:950] - 30) / 270
height, width = data.shape
bin = 1
print data.min(), data.max()
X = arange(data.shape[1])
Y = arange(data.shape[0])
tau = data[:, data.shape[1] // 2][:, None]
T = 5.0
t = linspace(0, T, data.shape[1])[None, :]
f = 1 / (1 + exp(-T / (2 * tau)))
Z = where(t < T / 2, 1 - f * exp(-t / tau), f * exp(-(t - T / 2) / tau))
X, Y = meshgrid(X, Y)
colors = rbow(data)
colors[:, :, -1] = 0.6
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
        linewidth=0, antialiased=True, shade=True)
ax.set_xlim3d(0, data.shape[0] + 36.0 / bin)
ax.set_ylim3d(18.0 / bin, data.shape[0] + 30.0 / bin)
ax.set_zlim3d(-0.8, 1.1)
ax.grid(False)
ax.view_init(38, -55.5)
ax.dist = 9.4
for a in (ax.w_xaxis, ax.w_yaxis, ax.w_zaxis):
    for t in a.get_ticklines() + a.get_ticklabels():
        t.set_visible(False)
    a.line.set_visible(False)
    a.pane.set_visible(False)
pl.savefig(picdir + "3d-plot.png", transparent=True)

这篇关于使用python在3d图中叠加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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