網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
處理matlab的mat數(shù)據(jù)
python 和matlab是2個(gè)常用的實(shí)驗(yàn)室平臺(tái)工具,在一些應(yīng)用下,這2個(gè)不同平臺(tái)下的數(shù)據(jù)會(huì)打交道,因此如何讀取和保存顯得尤為重要,這里需要用到python的第三方平臺(tái)下的scipy模塊。
先用下面這個(gè)命令檢查是否下載好scipy
import scipy
如果報(bào)錯(cuò),用python install scipy 或者 conda install scipy 下載安裝
需要用到scipy中的輸入輸出類中的loadmat 和savemat方法:?
import scipy.io as sio
?
sio.loadmat(file_name, mdict=None, appendmat=True, **kwargs)
sio.savemat(file_name, mdict, appendmat=True, format='5', long_field_names=False, do_compression=False, oned_as='row'
下面介紹一個(gè)簡(jiǎn)單的錯(cuò)誤例子:(需要傳字典格式的參數(shù))
import scipy.io as sio
import numpy as np
?
x = np.ones((3,3))
?
x
Out[86]:?
array([[1., 1., 1.],
? ? ? ?[1., 1., 1.],
? ? ? ?[1., 1., 1.]])
?
sio.savemat('f.mat',x)
Traceback (most recent call last):
?
? File "<ipython-input-87-d739bc03c885>", line 1, in <module>
? ? sio.savemat('f.mat',x)
下面介紹一個(gè)簡(jiǎn)單的保存 導(dǎo)入例子:
import scipy.io as sio
import numpy as np
?
x = np.ones((3,3))
?
x
Out[86]:?
array([[1., 1., 1.],
? ? ? ?[1., 1., 1.],
? ? ? ?[1., 1., 1.]])
?
sio.savemat('f.mat',{"x":x})
?
?
?
myMat =sio.loadmat('f.mat')
?
print(myMat) #輸出為字典
{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Fri Aug 21 16:29:37 2020', '__version__': '1.0', '__globals__': [], 'x': array([[1., 1., 1.],
? ? ? ?[1., 1., 1.],
? ? ? ?[1., 1., 1.]])}
?
#以保存名為key,輸出list value
print(myMat['x'])
[[1. 1. 1.]
?[1. 1. 1.]
?[1. 1. 1.]]
如果想把python數(shù)據(jù)保存為mat數(shù)據(jù),則需要cell格式數(shù)據(jù),而python沒(méi)有實(shí)現(xiàn)cell,因此需要用到numpy模塊,可以看這篇博客。
處理matlab的*.mat格式數(shù)據(jù)及常見(jiàn)錯(cuò)誤匯總
由于matlab和python兩種語(yǔ)言的編程方式不同,有時(shí)候在進(jìn)行程序混編時(shí),需要利用python調(diào)用matlab下的格式數(shù)據(jù),下面介紹如何調(diào)用mat格式數(shù)據(jù)及常見(jiàn)錯(cuò)誤解決方法,僅供參考!
一、數(shù)據(jù)讀取錯(cuò)誤
# 最初用loadmat讀取數(shù)據(jù)
import numpy as np
from scipy.io mport loadmat
img = loadmat('im.mat')['im'] #im.mat為mat數(shù)據(jù)的名稱,['im'] 中的im表示該文件下im的數(shù)據(jù)
使用如上代碼讀取數(shù)據(jù)時(shí),會(huì)出現(xiàn)如下錯(cuò)誤:
如果出現(xiàn)以上錯(cuò)誤,改用下面方式讀取,
import h5py
img = h5py.File('im.mat')['im']
img = h5py.File('im.mat','r')['im'] # 無(wú)警告
二、數(shù)據(jù)類型錯(cuò)誤
(用Python處理圖像時(shí),若涉及加減運(yùn)算,溢出差值被重新賦值255-0)
# python代碼
import h5py
import numpy as np
img = h5py.File('im.mat')['im']
# python中的M,N剛剛好與matlab中的M,N取值相反,此處進(jìn)行轉(zhuǎn)置與matlab相同矩陣格式進(jìn)行處理
x = np.array(img).T
[M, N] = x.shape
if M < 16 and N < 16:
score = -2
# Feature Extraction:
# 1. horizontal features
d_h = x[:, 1:N] - x[:, 0:N - 1] # 該步操作圖像產(chǎn)生滿溢,溢出后差值可能都被賦為255,依次遞減
此種情況下,d_h數(shù)據(jù)會(huì)出現(xiàn)滿溢情況,下面就是相同數(shù)據(jù)在python和matlab下面進(jìn)行運(yùn)算的差異性。
% Matlab 代碼
img = laod('im.mat')
[M, N] = size(x)
if M < 16 | N < 16
score = -2;
end
x = double(img); % 將無(wú)符號(hào)類型uint8數(shù)據(jù)類型轉(zhuǎn)換為double類型
% Feature Extraction:
% 1. horizontal features
d_h = x(:, 2:N) - x(:, 1:(N-1));
原因: 導(dǎo)入數(shù)據(jù)類型為 uint8 數(shù)據(jù)格式,該種格式下是沒(méi)有負(fù)數(shù)的,在matlab中進(jìn)行運(yùn)算時(shí),先將uint8數(shù)據(jù)類型轉(zhuǎn)化為了double類型,然后進(jìn)行了減法運(yùn)算,所以會(huì)出現(xiàn)如上結(jié)果,但是在python中,由于沒(méi)有double類型,所以需要自己手動(dòng)設(shè)置數(shù)據(jù)格式類型,只需要改成不是uint8格式即可(具體格式需要根據(jù)需求,此處改成了int8格式類型)。解決方法非常簡(jiǎn)單,只需在上面的一行代碼中加入數(shù)據(jù)類型即可:
x = np.array(img,dtype = 'int8').T # 對(duì)讀取的uint8格式數(shù)據(jù)進(jìn)行重新定義一下格式即可
x = np.array(img,dtype = 'float').T # 下面這種格式雖然是浮點(diǎn)型,但是計(jì)算過(guò)程不容易出錯(cuò),如果是上面的int8會(huì)出現(xiàn)部分錯(cuò)誤,需要注意
現(xiàn)在看一下結(jié)果,就跟matlab處理結(jié)果一樣了。
雖然下面是浮點(diǎn)型,但是能夠保證數(shù)據(jù)轉(zhuǎn)化的精度和準(zhǔn)確性,img的影像數(shù)據(jù)轉(zhuǎn)化成數(shù)值時(shí)不出錯(cuò)誤,非必要情況下,不要使用int8數(shù)據(jù)格式,因?yàn)槭褂胕nt8格式數(shù)據(jù)類型,會(huì)在某些部分出錯(cuò),這一定要注意。(改組數(shù)據(jù)中(0,80)數(shù)值在int8格式轉(zhuǎn)化時(shí)出錯(cuò),原始數(shù)值為129,轉(zhuǎn)化之后變成127,而使用float格式則不會(huì)出現(xiàn)錯(cuò)誤)
原始數(shù)據(jù)unit8數(shù)據(jù)格式類型的數(shù)值為129,在python中不同格式類型的值就不一樣。
所以u(píng)int8格式,在python運(yùn)算中還是轉(zhuǎn)換成float格式靠譜,轉(zhuǎn)換成int8真的不行呀!
原文鏈接:https://blog.csdn.net/qq_39463175/article/details/108149649
相關(guān)推薦
- 2022-03-26 Postman配置多環(huán)境請(qǐng)求地址的實(shí)現(xiàn)_相關(guān)技巧
- 2022-05-10 一次性解決:IDEA的 maven 配置問(wèn)題,在新項(xiàng)目中不再擔(dān)心 maven 的配置問(wèn)題
- 2023-01-31 Python函數(shù)用法和底層原理分析_python
- 2022-12-07 C++中的數(shù)字轉(zhuǎn)字符串to_string_C 語(yǔ)言
- 2022-05-17 Servlet快速入門
- 2022-12-22 利用C++求解八數(shù)碼問(wèn)題實(shí)例代碼_C 語(yǔ)言
- 2022-06-27 Android中的TimePickerView(時(shí)間選擇器)的用法詳解_Android
- 2023-07-03 Redis 中 List(列表)類型的命令及詳解
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支