根据 Matlab 数据在 Python 中创建彩色图像

Create a COLOR image in Python from Matlab data

我正在尝试在 Python 中创建彩色图像。数据来自 Matlab 文件。 这是我的代码。实际上我只能创建灰度图像,但我需要彩色图像。 你能帮帮我吗?

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 scipy.io
from scipy import misc
from PIL import Image
##import Image 
import numpy as np

import timeit
fDataName = 'MatLab_File'

start_time = timeit.default_timer()

# import file into a dictionary
fMatLab = scipy.io.loadmat(fDataName, mat_dtype = True, squeeze_me = True, struct_as_record=False)
# read in the structure
data = fMatLab[fDataName]

sizeZ = data.shape[2]

for i in range(sizeZ):    
   img = scipy.misc.toimage(data[:,:,i], low=-0, high=255, mode='L')
   img.save('imageFinal_%s.png' % i)

stop_time = timeit.default_timer() - start_time
print("--- %s seconds ---" % stop_time)

使用代码创建图像的示例是:

需要的彩色图片如下:

彩色图像是使用此代码创建的:

pyA = data[:,:,1] 
from pylab import * 
from matplotlib import *
matshow(pyA,1) 
show()

我也用过这段代码:

for i in range(sizeZ):    
    img = scipy.misc.toimage(data[:,:,i], low=0, high=255, mode='L')
    img.save('transectxyi_%s.png' % i)
    im = Image.fromarray(data[:,:,i]).convert('RGBA')
    im.save("a.png")

但我得到黑色图像。黑色图像是这样的:

编辑:我用代码解决了我的问题:

def saveImages(data, fileName): 
    img = scipy.misc.toimage(data, low=0, high=255, mode='P')    
    imP = img.convert('RGBA')    
    img = mpimg.pil_to_array(imP)
    imgP = img[:,:,0]    
    plt.imsave(fileName, imgP, vmin=None, vmax=None, cmap=None, format=None, origin=None, dpi=100)
    return imP

您可以像这样使用 PIL

img = Image.fromarray(data, 'RGB')

或者您可以使用:

img = Image.fromarray(data).convert('RGB')

此外,在行 img = scipy.misc.toimage(data[:,:,i], low=-0, high=255, mode='L') 中,你有 low=-0,我猜你想要 low=0 而没有 -

你的img只有一个通道(灰度),所以你需要的是伪彩色可视化。

http://matplotlib.org/users/image_tutorial.html

因此您的 for 循环中的代码可能如下所示:

imgplot = plt.imshow(img)
imgplot.set_cmap('spectral')
#save the plot with savefig() http://matplotlib.org/api/pyplot_api.html