Simshang
12/27/2016 - 9:05 AM

mat.py

# -*- coding: utf-8 -*-

import scipy.io as sio
import os
import numpy as np

def create_mat(mat_sample,mat_path,savepath,dpi,filenum):
    count = (256/dpi)*(256/dpi)
    # matfils = os.listdir(mat_path)
    # mat_sample = random.sample(matfils,count)
    mat = mat_path + str(mat_sample[0][:-4]+".mat")
    mat = sio.loadmat(mat)
    gt_out = mat['S']

    for index in range(1,len(mat_sample)):
        mat = mat_path + str(mat_sample[index][:-4]+".mat")
        mat = sio.loadmat(mat)
        gt = mat['S']
        gt_out = np.append(gt_out,gt,axis=0)

    split_num = gt_out.shape[0]/256
    gt_result = gt_out[0:256,:]

    for split in range(1,split_num):
        low = split*256
        high = (split+1)*256
        gt_result = np.append(gt_result,gt_out[low:high,:],axis=1)

    # 保存图片
    if os.path.exists(savepath):
        savepath = savepath + str(filenum) +".mat"
        sio.savemat(savepath, {'S': gt_result})
        print "Create mat finished!\n"
    else:
        os.makedirs(savepath)
        savepath = savepath + str(filenum) +".mat"
        sio.savemat(savepath, {'S': gt_result})
        print "Create mat finished!\n"