我想写一个数据转换器工具。我需要分析文件中的位流以显示3D体积的2D横截面。
我要查看的数据集可以在这里找到:https://figshare.com/articles/SSOCT_test_dataset_for_OCTproZ/12356705
该文件的标题为:burned_wood_with_tape_1664x512x256_12bit.raw(832 MB)
非常感谢您的指导。如果我能够获得一些使用数据转换将数据集显示为图像的软件,就愿意给予悬赏。
由于我是这个概念的新手,因此我没有代码显示此问题。但是,这是我尝试使用SO上其他问题的启发而尝试的一些方法:

import rawpy
import imageio

path = "Datasets/burned_wood_with_tape_1664x512x256_12bit.raw"
for item in path:
    item_path = path + item
    raw = rawpy.imread(item_path)
    rgb = raw.postprocess()
    rawpy.imshow(rgb)

最佳答案

下面,我实现了下一个可视化。
示例RAW文件burned_wood_with_tape_1664x512x256_12bit.raw包含此文件中的每个A扫描1664个样本,每个B扫描512个A扫描,每个缓冲区16个B扫描,每个卷16个缓冲区和2个卷,每个样本编码为2字节无符号整数(按小端顺序),仅使用12个高位,低4位包含零。样本的中心大约在2 ^ 15左右,准确的说,数据具有这些统计数据min 0 max 47648 mean 32757 standard deviation 454.5
我绘制了尺寸为1664 x 512的灰度图像,一个文件中总共有16 * 16 * 2 = 512个此类图像(帧)。我使用matplotlib库在屏幕上绘制动画帧,还将这些动画渲染到GIF文件中。代码之后是一个降低质量的渲染GIF的示例。
要渲染/绘制具有不同结果分辨率的图像,您需要使用plt.rcParams['figure.figsize']更改代码行,此无花果大小包含(widht_in_inches,height_in_inches),默认情况下DPI(每英寸点数)等于100,这意味着如果要得到的GIF为分辨率720x265,则需要将此图形尺寸设置为(7.2,2.65)。由于生成的图形尺寸中包含轴和填充,因此生成的GIF包含的分辨率动画也较小。
我的下一个代码需要通过python -m pip install numpy matplotlib命令一次安装pip模块。
Try it online!

# Needs: python -m pip install numpy matplotlib
def oct_show(file, *, begin = 0, end = None):
    import os, numpy as np, matplotlib, matplotlib.pyplot as plt, matplotlib.animation
    plt.rcParams['figure.figsize'] = (7.2, 2.65) # (4.8, 1.75) (7.2, 2.65) (9.6, 3.5)
    sizeX, sizeY, cnt, bits = 1664, 512, 16 * 16 * 2, 12
    stepX, stepY = 16, 8
    fps = 5
    try:
        fsize, opened_here = None, False
        if type(file) is str:
            fsize = os.path.getsize(file)
            file, opened_here = open(file, 'rb'), True
        by = (bits + 7) // 8
        if end is None and fsize is not None:
            end = fsize // (sizeX * sizeY * by)
        imgs = []
        file.seek(begin * sizeY * sizeX * by)
        a = file.read((end - begin) * sizeY * sizeX * by)
        a = np.frombuffer(a, dtype = np.uint16)
        a = a.reshape(end - begin, sizeY, sizeX)
        amin, amax, amean, stdd = np.amin(a), np.amax(a), np.mean(a), np.std(a)
        print('min', amin, 'max', amax, 'mean', round(amean, 1), 'std_dev', round(stdd, 3))
        a = (a.astype(np.float32) - amean) / stdd
        a = np.maximum(0.1, np.minimum(a * 128 + 128.5, 255.1)).astype(np.uint8)
        a = a[:, :, :, None].repeat(3, axis = -1)

        fig, ax = plt.subplots()
        plt.subplots_adjust(left = 0.08, right = 0.99, bottom = 0.06, top = 0.97)
        for i in range(a.shape[0]):
            title = ax.text(
                0.5, 1.02, f'Frame {i}',
                size = plt.rcParams['axes.titlesize'],
                ha = 'center', transform = ax.transAxes,
            )
            imgs.append([ax.imshow(a[i], interpolation = 'antialiased'), title])
        ani = matplotlib.animation.ArtistAnimation(plt.gcf(), imgs, interval = 1000 // fps)
        print('Saving animated frames to GIF...', flush = True)
        ani.save(file.name + '.gif', writer = 'imagemagick', fps = fps)
        print('Showing animated frames on screen...', flush = True)
        plt.show()
    finally:
        if opened_here:
            file.close()

oct_show('burned_wood_with_tape_1664x512x256_12bit.raw')
示例输出GIF:

关于python - 使用数据转换器将3D体积显示为图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/64648186/

10-09 23:02
查看更多