【问题标题】:Create .PNG images in Python from MatLab data从 MatLab 数据在 Python 中创建 .PNG 图像
【发布时间】:2014-09-25 23:15:40
【问题描述】:

实际上我正在尝试在 Python 中创建 .png 图像。数据来自 Matlab 文件。 这是我的代码。当我运行时出现错误:

Traceback(最近一次调用最后一次): 文件“readFromMatLab.py”,第 20 行,在 矩阵[x][y][z] = 数据[x][y][z] IndexError: 列表索引超出范围

Matlab 文件的数据为 512x512x200 Double 数组。

> {'__version__': '1.0', 'St3D': array([[[ -4.98510788e-02, 
> -4.98139346e-02,  -4.97636073e-02, ...,
>           -5.19862428e-02,  -5.20095813e-02,  -5.20122990e-02],
>         [ -4.98249255e-02,  -4.97792210e-02,  -4.97507640e-02, ...,
>           -5.19832396e-02,  -5.19884452e-02,  -5.20089354e-02],
>         [ -4.98121755e-02,  -4.97751679e-02,  -4.97488529e-02, ...,
>           -5.19605824e-02,  -5.19734534e-02,  -5.20023879e-02],
>         ...,
>        [[  9.10799464e-05,   1.75287655e-04,   2.26928715e-04, ...,
>            1.10619951e-04,   1.04038395e-04,   7.44506576e-05],
>         [  6.29097917e-05,   1.20765020e-04,   1.91577341e-04, ...,
>            8.24078623e-05,   8.96774520e-05,   7.44268856e-05],
>         [  4.14273859e-05,   7.96562916e-05,   1.20801256e-04, ...,
>            9.05750282e-05,   8.13201896e-05,   6.77554603e-05],
>         ..., 
>         [  1.72297366e-04,   1.68849830e-04,   2.21771692e-04, ...,
>            2.30046391e-04,   2.51247428e-04,   2.58021432e-04],
>         [  2.06350049e-04,   1.92126121e-04,   2.58923928e-04, ...,
>            2.48977658e-04,   2.78131275e-04,   2.76242136e-04],
>         [  2.42915268e-04,   2.47607632e-04,   2.89283796e-04, ...,
>            2.58819021e-04,   2.76203977e-04,   2.82977241e-04]]]), '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri
> Sep 19 17:03:17 2014', '__globals__': []}


#!/usr/bin/python
# -*- coding: utf-8 -*-
import pprint
import scipy.io
import numpy
from scipy import misc
from PIL import Image

# import file into a dictionary
fMatLab = scipy.io.loadmat('St3D', mat_dtype = True, squeeze_me = True, struct_as_record=False)

# read in the structure
data = fMatLab['St3D']

matriz = [[[0 for col in range(data.shape[0])] for row in range(data.shape[1])] for x in range(data.shape[2])]

for x in range(0,data.shape[0]):
    for y in range(0,data.shape[1]):
        for z in range(0,data.shape[2]):
            matriz[x][y][z] = data[x][y][z]

for i in range(len(matriz)):
    #im = numpy.random.random_integers(0, 255, 512*512).reshape((512, 512))
    misc.imsave('transect_%s.png' % i, matriz[i])

from glob import glob
filelist = glob('transect*.png')
filelist.sort()

【问题讨论】:

  • 你可能想要matriz = data.transpose((2,1,0)),并且没有循环或三重理解;但是为什么要这样做呢?

标签: python matlab scipy


【解决方案1】:

在我看来,您正试图将misc.imsave 应用于沿数据特定维度的每个切片。但是,代码效率很低,尤其是从matriz =matriz[x][y][z] =。您已经将矩阵作为 numpy 数组存储在变量 data 中,因此不需要列表理解或循环。如果你想在一个维度上循环一个 numpy 数组的切片,例如第三个维度,以应用一个函数,只需执行以下操作:

for i in xrange(data.shape[2]):
    some_function(data[:, :, i])

编辑 在你的情况下,这将转化为

data = fMatLab['St3D']
for i in xrange(data.shape[2]):
    misc.imsave('transect_%s.png' % i, data[:, :, i])

最后,最后三行不应缩进。

【讨论】:

  • 你试着说:data = fMatLab['St3D'] for i in range(2): misc.imsave('transect_%s.png' % i, data[:,:,i] )
  • 是的,但使用data.shape[2],而不仅仅是'2'(见编辑)。您想遍历整个第三维。尝试输入data.shape 以了解我的意思,或查看numpy docs
  • 它对我有用。现在我需要获得一张高质量的图像并优化流程。您知道如何获得更快的解决方案吗?我用img = scipy.misc.toimage(data[:,:,i], mode='I') 测试代码,但图像完全是黑色的。谢谢
  • 我很乐意考虑一下。最好的选择是,首先,因为我的回答对你有用,接受并支持它,其次,提出一个关于提高性能的新问题。如果您在此处的 cmets 中放一个指向它的链接,我会检查出来。
  • 谢谢它对我有用。您的回复为我的问题提供了解决方案。
猜你喜欢
  • 2015-03-07
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2011-01-12
  • 2011-08-30
  • 2013-03-26
  • 2021-09-03
  • 2013-10-09
相关资源
最近更新 更多