如何在Python中通过逐步过渡将图像正确显示为3D图?

前端之家收集整理的这篇文章主要介绍了如何在Python中通过逐步过渡将图像正确显示为3D图? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图将3D图像之间的差异可视化,以便更轻松地区分正负差异.

我已经成功完成了图像的基本绘制,但是,在matplotlib值之间插入值.我需要这些是像素之间的步进更改.

我经常使用非常低分辨率的图像进行测试,例如16 x 16,因此插值会产生很大的影响.

16乘以16的图像的块状文件
https://wetransfer.com/downloads/c916f76e0d86a61c00c2ed4cfe4ae97520190210192200/60d87c

解决此问题的一种方法是重复这些值,但是这似乎效率很低,需要清理之后的滴答声.

enter image description here

生成上面图像的代码

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

SubIm = np.load("Subtract_Image.npy")

def ImPlot2D3D(img,cmap=plt.cm.jet):

    Z = img[::1,::1]

    fig = plt.figure(figsize=(14,7))

    # 2D Plot
    ax1 = fig.add_subplot(1,2,1)
    im = ax1.imshow(Z,cmap=cmap)
    ax1.set_title('2D')
    ax1.grid(False)

    # 3D Plot
    ax2 = fig.add_subplot(1,projection='3d')
    X,Y = np.mgrid[:Z.shape[0],:Z.shape[1]]
    ax2.plot_surface(X,Y,Z,cmap=cmap)
    ax2.set_title('3D')

    plt.show()


ImPlot2D3D(SubIm)

我已经研究了3D条形图,但是它们都使用合并方案,因此我无法使其用于图像.

最佳答案
最终设法回答了我自己的问题.

解决此问题的蛮力方法是重复数组中的值,从而使’matplotlib’所做的值之间的插值影响较小,并且更好地近似了阶跃变化.
这可以使用numpy.repeat来实现.由于这是3D数组,因此我们必须在一个轴上进行迭代,而在另一个轴上进行迭代.否则,该数组将被重复拉平并返回此平面数组.

结果:

Results:

def ImPlot2D3D(img,cmap=plt.cm.jet,step=False,ratio=10):

    if step:
        img = (img.repeat(ratio,axis=0)).repeat(ratio,axis=1)

    Z = img[::1,cmap=cmap)
    ax2.set_title('3D')

    # Scale the ticks back down to original values
    if step:
        ticks_x = ticker.FuncFormatter(lambda x,pos: '{0:g}'.format(x / ratio))
        ticks_y = ticker.FuncFormatter(lambda y,pos: '{0:g}'.format(y / ratio))
        ax1.xaxis.set_major_formatter(ticks_x)
        ax1.yaxis.set_major_formatter(ticks_y)
        ax2.xaxis.set_major_formatter(ticks_x)
        ax2.yaxis.set_major_formatter(ticks_y)

    plt.show()

import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

SubIm = np.load("Subtract_Image.npy")
ImPlot2D3D(SubIm,step=True)
原文链接:https://www.f2er.com/python/533118.html

猜你在找的Python相关文章