数据格式:
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()
结果