python 讀取.mat數據

數據格式:

struct groundTruth {
   Segmentation:320x480
   Boundaries:320x480
}  = loadmat(file_list)

在這裏插入圖片描述

# coding=utf-8
import scipy.io as sio
import pandas as pd
import os
import numpy as np


path_dir = "I:\\1裂縫檢測\\CrackForest-dataset\\"
""" 將path_dir 文件夾下的.mat 文件轉成二元數組array
因爲該文件的數據較複雜:
struct groundTruth {
   Segmentation:320x480
   Boundaries:320x480
}  = loadmat(file_list)

下面的結果是:
dat[x][y] 就是 該320x480的某個值

"""

def mat2csv():

    curr_path = os.path.dirname(path_dir)
    mat_data_path = os.path.join(curr_path, "groundTruth")
    csv_data_path = os.path.join(curr_path, "csv")
    if not os.path.exists(csv_data_path):
        os.makedirs(csv_data_path)
    if not os.path.exists(mat_data_path):
        os.makedirs(mat_data_path)
    file_list = os.listdir(mat_data_path)
    mat_list = [file_name for file_name in file_list if file_name.endswith(".mat")]
    print("find mat file : ", mat_list)

    for mat_file in mat_list:
        file_path = os.path.join(mat_data_path, mat_file)
        mat_data = sio.loadmat(file_path)
        for key in mat_data:
            if not str(key).startswith("__"):
                data = mat_data[key][:]
                print (mat_file)
                try:
                    dat = np.array(data[0]['Segmentation'][0])
                    print(dat[1][3])
                    print(dat.shape)
                except ValueError as e:
                    print (e)
                    continue
        break
if __name__ == "__main__":
    mat2csv()

結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章