欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python如何處理matlab的mat數(shù)據(jù)

 更新時(shí)間:2022年05月11日 10:11:02   作者:amcomputer  
這篇文章主要介紹了python如何處理matlab的mat數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

處理matlab的mat數(shù)據(jù)

python 和matlab是2個(gè)常用的實(shí)驗(yàn)室平臺工具,在一些應(yīng)用下,這2個(gè)不同平臺下的數(shù)據(jù)會打交道,因此如何讀取和保存顯得尤為重要,這里需要用到python的第三方平臺下的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è)簡單的錯(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è)簡單的保存 導(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沒有實(shí)現(xiàn)cell,因此需要用到numpy模塊,可以看這篇博客。

處理matlab的*.mat格式數(shù)據(jù)及常見錯(cuò)誤匯總

由于matlab和python兩種語言的編程方式不同,有時(shí)候在進(jìn)行程序混編時(shí),需要利用python調(diào)用matlab下的格式數(shù)據(jù),下面介紹如何調(diào)用mat格式數(shù)據(jù)及常見錯(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í),會出現(xiàn)如下錯(cuò)誤:

在這里插入圖片描述

如果出現(xiàn)以上錯(cuò)誤,改用下面方式讀取,

import h5py
img = h5py.File('im.mat')['im']
img = h5py.File('im.mat','r')['im']    # 無警告

二、數(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ù)會出現(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);  % 將無符號類型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ù)格式,該種格式下是沒有負(fù)數(shù)的,在matlab中進(jìn)行運(yùn)算時(shí),先將uint8數(shù)據(jù)類型轉(zhuǎn)化為了double類型,然后進(jìn)行了減法運(yùn)算,所以會出現(xiàn)如上結(jié)果,但是在python中,由于沒有double類型,所以需要自己手動設(shè)置數(shù)據(jù)格式類型,只需要改成不是uint8格式即可(具體格式需要根據(jù)需求,此處改成了int8格式類型)。解決方法非常簡單,只需在上面的一行代碼中加入數(shù)據(jù)類型即可:

x = np.array(img,dtype = 'int8').T   # 對讀取的uint8格式數(shù)據(jù)進(jìn)行重新定義一下格式即可
x = np.array(img,dtype = 'float').T   # 下面這種格式雖然是浮點(diǎn)型,但是計(jì)算過程不容易出錯(cuò),如果是上面的int8會出現(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ù)類型,會在某些部分出錯(cuò),這一定要注意。(改組數(shù)據(jù)中(0,80)數(shù)值在int8格式轉(zhuǎn)化時(shí)出錯(cuò),原始數(shù)值為129,轉(zhuǎn)化之后變成127,而使用float格式則不會出現(xiàn)錯(cuò)誤)

在這里插入圖片描述

原始數(shù)據(jù)unit8數(shù)據(jù)格式類型的數(shù)值為129,在python中不同格式類型的值就不一樣。

在這里插入圖片描述

所以uint8格式,在python運(yùn)算中還是轉(zhuǎn)換成float格式靠譜,轉(zhuǎn)換成int8真的不行呀!

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論