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

2023-12-14

我目前正在尝试将图像从.mat文件至.jpg从该网站下载的文件-脑肿瘤数据集。 该目录中包含的所有文件都是.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()

Note:
Each mat文件包含一个名为的结构cjdata.
cjdata 结构体的字段:

cjdata = 

struct with fields:

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过python将.mat文件扩展名图像转换为.jpg 的相关文章

随机推荐

  • 加载所有图像(包括缓存图像)后的 jquery 事件?

    我有以下功能 用于在页面中使用ajax 并且仅在加载所有图像后才显示它 get target page php function data var live preview temp holder html data var imgCoun
  • ApplicationServices 解析网络核心中不同范围的实例?

    我使用 net core 3 1 进行以下配置 public interface IFoo public void Work public class Foo IFoo readonly string MyGuid public Foo M
  • 无法开始分布式事务

    我尝试对链接服务器运行 SQL 但出现以下错误 BEGIN DISTRIBUTED TRANSACTION SELECT TOP 1 FROM Sessions OLE DB provider SQLNCLI for linked serv
  • 使用 gzip 压缩 HttpWebRequest

    我正在开发一个 NET 4 0控制台应用程序充当SOAP Web Service客户端将数据发送 POST 到第三方 我无法控制服务器端的网络服务 第三方确实提供了WSDL s使用 并且我能够导入它们并以相当成功的方式使用它们 但是 需要使
  • PDO::PARAM 用于十进制类型?

    我有2个数据库字段 decval decimal 5 2 intval int 3 我有 2 个更新它们的 pdo 查询 更新 int 的那个工作正常 update intval gt bindParam intval intval PDO
  • 有没有办法将数字转换为整数?

    我需要转换one into 1 two into 2等等 有没有办法通过图书馆或课程或其他东西来做到这一点 此代码的大部分内容是设置 numwords 字典 这仅在第一次调用时完成 def text2int textnum numwords
  • Selenium IDE - 记录右键单击

    我使用 Selenium IDE 为我的网络应用程序进行记录测试 Selenium IDE 无法识别我的 div 中的右键单击 我已经自定义了我的 div 上的右键单击 我想测试相关功能 谁能帮我 Thanks Tommaso 使用 con
  • PHP eval(array_as_string) 返回 null

    arr eval array foo gt bar returns null var dump arr 有人可以解释一下为什么我得到的是 null 而不是数组吗 你需要return数组 来自docs eval 回报NULL unless r
  • 编写将一行输入作为字符串读取并打印字符串中所有元音的位置的程序

    我是 Java 初学者 我有一个问题loops 我一直在努力完成这个任务 它说 编写将一行输入读取为string并打印positions of all vowels在字符串中 我已经设法打印出输入中的元音数量 但在打印它们的位置时我遇到了困
  • 如何在Linux中创建隐藏文件?

    在我的程序中 我必须隐藏一个文件以避免删除或修改该文件 PATH etc NAME file C 中有一个函数可以让我做到这一点吗 您只需添加一个 到文件名的前面 话虽如此 如果您的目标是不允许修改文件 请将权限更改为无法修改的内容 就像是
  • JanusGraph .net C#

    嘿 谁能帮我弄清楚如何使用 C JanusGraph net 连接到托管多个图形的远程 JanusGraph 服务器并查询特定图形 按图形名称 我可以连接到服务器 但无法查询特定图表 var c JanusGraph Net JanusGr
  • 请求 Windows 中 Python 函数的管理员访问权限

    我想将文件列表复制到 Windows 系统目录 C Windows 使用Python函数 我有一个功能 import shutil def copy list src list dst for file in src list shutil
  • spring-boot 中存在多个 WebSecurityConfigurerAdapter 的问题

    我正在使用 spring boot 1 5 10 和 spring boot starter security 在我的微服务中 我将 API 暴露给外部世界和内部微服务 所以我想要2种安全性 一个用于外部呼叫 另一个用于内部呼叫 我已经提到
  • 如何附加到文件?

    如何附加到文件而不是覆盖它 将模式设置为open to a 附加 而不是 w 写 with open test txt a as myfile myfile write appended text The 文档列出所有可用模式
  • ListView 的 ItemsPanelTemplate 明显错误地抛出异常

    我创建了一个用户控件 其中包含带有自定义 ItemsPanelTemplate 的 ListView
  • 在 for 循环中返回 C

    在下面的代码中 会返回什么吗 include
  • 为什么内联块元素的边距会影响同级内联块元素

    我不明白为什么margin top of the
  • -[UIViewController _keyboard]:无法识别的选择器发送到实例 0x7b731ac0

    我正在编写一个应用程序 其中包含一个 MainViewController 内的 3 个 viewController 其中之一是通过故事板控制并显示广告 另外两个 viewController 正在显示信息 根据按下的菜单按钮和要呈现的视
  • R中的“累积差异”函数

    是否有一个预先存在的函数来计算连续值之间的累积差异 上下文 这是为了估计一个人在旅程中必须在两个方向上经历的高度变化自行车街网 可重现的例子 x lt c 27 24 24 27 28 create the data 方法一 for循环 f
  • 通过python将.mat文件扩展名图像转换为.jpg

    我目前正在尝试将图像从 mat文件至 jpg从该网站下载的文件 脑肿瘤数据集 该目录中包含的所有文件都是 mat文件 现在我想转换中的所有文件 jpg通过 python 格式化 通过 CNN 制作项目 使用深度神经网络进行脑肿瘤分类 我在谷