问题描述
我想写一个数据转换器工具。我需要分析文件中的位流以显示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左右,准确的说,这些数据的最小值为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,这意味着如果您想得到分辨率为720x265的GIF,则需要将此图形尺寸设置为(7.2,2.65)。由于生成的图形尺寸中包括轴和填充,因此生成的GIF包含的分辨率动画也较小。
我的下一个代码需要通过命令python -m pip install numpy matplotlib
一次安装pip模块。
# 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,sizeX)
amin,amax,amean,stdd = np.amin(a),np.amax(a),np.mean(a),np.std(a)
print('min',amin,'max','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:
我认为这根本不是有效的RAW文件。
如果您尝试使用此代码:
import rawpy
import imageio
path = 'Datasets/burned_wood_with_tape_1664x512x256_12bit.raw'
raw = rawpy.imread(path)
rgb = raw.postprocess()
您将收到以下错误:
----> 5 raw = rawpy.imread(path)
6 rgb = raw.postprocess()
~\Anaconda3\envs\py37tf2gpu\lib\site-packages\rawpy\__init__.py in imread(pathOrFile)
18 d.open_buffer(pathOrFile)
19 else:
---> 20 d.open_file(pathOrFile)
21 return d
rawpy\_rawpy.pyx in rawpy._rawpy.RawPy.open_file()
rawpy\_rawpy.pyx in rawpy._rawpy.RawPy.handle_error()
LibRawFileUnsupportedError: b'Unsupported file format or not RAW file'