首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入文本文件并导出3D图像

导入文本文件并导出3D图像
EN

Stack Overflow用户
提问于 2016-07-15 03:18:59
回答 1查看 299关注 0票数 0

我需要做的是加载一个txt文件,并为成像制作一个3D矩阵。

使用下面的代码,当文本文件是一个包含大量数据的大文件时,我会收到一个错误。

代码语言:javascript
复制
# Load a .txt data output from tomoview into a volume

import numpy as np
from tifffile import imsave
import io


#number of elements in array
i = 128

#open the text file with all headers
data = io.open('BreastPhantomTest.txt','r',encoding='cp1252')

#create a list of all lines with data in them - the typical format is for 16 lines of header, 
#followed by 'n' lines of data each 'm' tab delimited data points long 
#where 'n' is the number of points in the scan direction and 'm' is the number of digitization points
#This repeats for each 'i element.

#line = data.readlines() ##-- this method will get slow for larger datasets I believe
datatrimmed=[]
#assume lines with data start with a non-zero number this should scale up in data size.
#We can also use this method to get other parameters out of the header.
for line in data:
    #line = line.rstrip()
    if not line[0].isdigit():continue #take only the digits
    #if not line[0]!='0':continue #take only the lines that don't start with a zero.
    f = line.split()
    datatrimmed.append(f)



m = len(f)    
volume = np.reshape(datatrimmed,(i,m,-1)) #create the volume, leaving the number of scan points to float
volume1=np.float32(volume)
imsave('volume.tiff',volume1)

我收到的错误是:

ValueError:新数组的总大小必须保持不变,如“np.reshape=np.reshape(数据处理,(i,m,-1)) #创建卷,让扫描点数浮动”

EN

回答 1

Stack Overflow用户

发布于 2016-07-15 03:54:22

这似乎是你的价值,因为我是太大的重塑,你正在做的。

Numpy的装载量会为你处理很多这方面的工作。可以指定要跳过的标题行数。

代码语言:javascript
复制
import numpy as np

with open('BreastPhantomTest.txt','r', encoding='cp1252') as f:
    data = np.loadtxt(f, skiprows=16)

data = data[np.where(data[:, 0] != 0)].astype('float32') # filter out lines starting with 0

i = 128
m = data.shape[1]
assert data.shape[0] <= i, "Your value for i is too big"
data = data.reshape(i, m, -1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38387352

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档