gpt4 book ai didi

python-3.x - 通过python将.mat文件扩展名图像转换为.jpg

转载 作者:行者123 更新时间:2023-12-03 21:17:10 25 4
gpt4 key购买 nike

我目前正在尝试从 转换图像.mat 文件到 .jpg 从本站下载的文件- BrainTumorDataset .
目录下的所有文件都是 .mat 文件,现在我想转换 中的所有文件.jpg 通过 python 格式通过 CNN 制作项目(使用深度神经网络的脑肿瘤分类)。我在谷歌搜索但后来我没有从那里得到任何东西,只有一些关于如何在 python 中加载 .mat 文件的主题,但这也没有帮助我。我找到了一个 answer在 StackOverflow 中,但这不适用于此数据集,而且答案是在 python 中加载 .mat 图像,但我想转换 .mat 图片在 .jpg 格式。

最佳答案

我设法转换了一个图像,使用循环来转换所有图像。

请阅读评论。

import matplotlib.pyplot as plt
import numpy as np
import h5py
from PIL import Image

#reading v 7.3 mat file in python
#https://stackoverflow.com/questions/17316880/reading-v-7-3-mat-file-in-python

filepath = '1.mat';
f = h5py.File(filepath, 'r') #Open mat file for reading

#In MATLAB the data is arranged as follows:
#cjdata is a MATLAB struct
#cjdata.image is a matrix of type int16

#Before update: read only image data.
####################################################################
#Read cjdata struct, get image member and convert numpy ndarray of type float
#image = np.array(f['cjdata'].get('image')).astype(np.float64) #In MATLAB: image = cjdata.image
#f.close()
####################################################################

#Update: Read all elements of cjdata struct
####################################################################
#Read cjdata struct
cjdata = f['cjdata'] #<HDF5 group "/cjdata" (5 members)>

# In MATLAB cjdata =
# struct with fields:
# label: 1
# PID: '100360'
# image: [512×512 int16]
# tumorBorder: [38×1 double]
# tumorMask: [512×512 logical]

#get image member and convert numpy ndarray of type float
image = np.array(cjdata.get('image')).astype(np.float64) #In MATLAB: image = cjdata.image

label = cjdata.get('label')[0,0] #Use [0,0] indexing in order to convert lable to scalar

PID = cjdata.get('PID') # <HDF5 dataset "PID": shape (6, 1), type "<u2">
PID = ''.join(chr(c) for c in PID) #Convert to string https://stackoverflow.com/questions/12036304/loading-hdf5-matlab-strings-into-python

tumorBorder = np.array(cjdata.get('tumorBorder'))[0] #Use [0] indexing - convert from 2D array to 1D array.

tumorMask = np.array(cjdata.get('tumorMask'))

f.close()
####################################################################

#Convert image to uint8 (before saving as jpeg - jpeg doesn't support int16 format).
#Use simple linear conversion: subtract minimum, and divide by range.
#Note: the conversion is not optimal - you should find a better way.
#Multiply by 255 to set values in uint8 range [0, 255], and covert to type uint8.
hi = np.max(image)
lo = np.min(image)
image = (((image - lo)/(hi-lo))*255).astype(np.uint8)

#Save as jpeg
#https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image
im = Image.fromarray(image)
im.save("1.jpg")

#Display image for testing
imgplot = plt.imshow(image)
plt.show()

笔记:
每个 mat文件包含一个名为 cjdata 的结构体.
cjdata 结构的字段:
cjdata = 

struct with fields:

label: 1
PID: '100360'
image: [512×512 int16]
tumorBorder: [38×1 double]
tumorMask: [512×512 logical]

将图像转换为 jpeg 时,您正在丢失信息...

关于python-3.x - 通过python将.mat文件扩展名图像转换为.jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59208896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com