3d plot tiff

Python

Visualize the TIFF image as a 3D plot. [dependency] - numpy - matplotlib - pspylib

main 1 file
3d plot tiff..py
plot_3d_tiff .py 2.0 KB
3d plot tiff..py 2002 bytes
import os
import numpy as np
import matplotlib.pyplot as plt
from pspylib.tiff.reader import TiffReader
from matplotlib.colors import BoundaryNorm, LinearSegmentedColormap


def __auto_palette_range(gray, cmap):
    # 95.44 % 범위 설정
    vmin = np.mean(gray) - 1.96 * np.std(gray)
    vmax = np.mean(gray) + 1.96 * np.std(gray)
    _, edges = np.histogram(gray, bins=cmap.N, range=(vmin, vmax))
    boundary_norm = BoundaryNorm(edges, cmap.N)
    return boundary_norm


def plot_3d_tiff(gray, cmap, size=(8, 8)):
    fig = plt.figure(figsize=size, facecolor='white')
    ax = fig.add_subplot(111, projection='3d')

    boundary_norm = __auto_palette_range(gray, cmap)

    y = np.arange(gray.shape[0]-1,-1,-1)
    x = np.arange(gray.shape[1])
    mesh_x, mesh_y = np.meshgrid(x, y)

    ax.set_zlim(gray.min(), gray.max())
    surf = ax.plot_surface(mesh_x, mesh_y, gray, cmap=cmap,
                           linewidth=0, antialiased=False, norm=boundary_norm)

    fig.colorbar(surf, shrink=0.5, aspect=5)
    ax.set_xlabel('X (pixels)')
    ax.set_ylabel('Y (pixels)')
    ax.set_zlabel('Height')
    return fig


if __name__ == "__main__":
    samples_path = r"C:\Park Systems\SmartScan\samples"
    tiff_path = os.path.join(samples_path, "Image", "Cheese.tiff")

    tiff = TiffReader(tiff_path)
    Zdata = np.array(tiff.data.scanData.ZData, dtype=np.float64)
    header = tiff.data.scanHeader.scanHeader

    height = int(header['height'][0])
    width = int(header['width'][0])
    tiff_image = np.reshape(Zdata, (height, width))
    tiff_image = np.flipud(tiff_image)

    ori_cm = tiff.data.metaData.colorMap['colorMap'][0]
    r = np.array(ori_cm[:256])
    g = np.array(ori_cm[256:512])
    b = np.array(ori_cm[512:768])
    rgb_cm = np.stack([r, g, b], axis=1) / 65536.0

    cmap_name = 'meta_cmap'
    meta_cmap = LinearSegmentedColormap.from_list(cmap_name, rgb_cm, N=128)

    plot_3d_tiff(tiff_image, meta_cmap)
    plt.show()
Comments (0)

No comments yet. Be the first to comment!

Snippet Information
Author: jungyu.lee
Language: Python
Created: Oct 23, 2025
Updated: 0 minutes ago
Views: 35
Stars: 1

Links & Files

Additional Files (1):