a b/mat_to_numpy.py
1
"""
2
Read all the Matlab files in the 'data' directory and export 3 numpy arrays:
3
- labels.npy
4
- images.npy
5
- masks.npy
6
Usage: python matlab_to_numpy.py ~/brain_tumor_dataset
7
"""
8
9
import os
10
import argparse
11
import sys
12
import numpy as np
13
import hdf5storage
14
import cv2
15
16
17
class NoDataFound(Exception):
18
    pass
19
20
21
def dir_path(path):
22
    """Check the path and the existence of a data directory"""
23
    # replace '\' in path for Windows users
24
    path = path.replace('\\', '/')
25
    data_path = os.path.join(path, 'data').replace('\\', '/')
26
27
    if os.path.isdir(data_path):
28
        return path
29
    elif os.path.isdir(path):
30
        raise NoDataFound('Could not find a "data" folder inside directory. {} does not exist.'
31
                          .format(data_path))
32
    else:
33
        raise NotADirectoryError(path)
34
35
36
parser = argparse.ArgumentParser()
37
parser.add_argument('path', help='path to the brain_tumor_dataset directory', type=dir_path)
38
parser.add_argument('--image-dimension', '-d', default=512, help='dimension of the image', type=int)
39
args = parser.parse_args()
40
41
labels = []
42
images = []
43
masks = []
44
45
data_dir = os.path.join(args.path, 'data').replace('\\', '/')
46
files = os.listdir(data_dir)
47
for i, file in enumerate(files, start=1):
48
    if i % 10 == 0:
49
        # print the percentage of images loaded
50
        sys.stdout.write('\r[{}/{}] images loaded: {:.1f} %'
51
                         .format(i, len(files), i / float(len(files)) * 100))
52
        sys.stdout.flush()
53
54
    # load matlab file with hdf5storage as scipy.io.loadmat does not support v7.3 files
55
    mat_file = hdf5storage.loadmat(os.path.join(data_dir, file))['cjdata'][0]
56
57
    # resize image and mask to a unique size
58
    image = cv2.resize(mat_file[2], dsize=(args.image_dimension, args.image_dimension),
59
                       interpolation=cv2.INTER_CUBIC)
60
    mask = cv2.resize(mat_file[4].astype('uint8'), dsize=(args.image_dimension, args.image_dimension),
61
                      interpolation=cv2.INTER_CUBIC)
62
63
    labels.append(int(mat_file[0]))
64
    images.append(image)
65
    masks.append(mask.astype(bool))
66
67
sys.stdout.write('\r[{}/{}] images loaded: {:.1f} %'
68
                 .format(i, len(files), i / float(len(files)) * 100))
69
sys.stdout.flush()
70
71
labels = np.array(labels)
72
images = np.array(images)
73
masks = np.array(masks)
74
75
print('\nlabels:', labels.shape)
76
print('images:', images.shape)
77
print('masks:', masks.shape)
78
79
np.save(os.path.join(args.path, 'labels.npy'), labels)
80
np.save(os.path.join(args.path, 'images.npy'), images)
81
np.save(os.path.join(args.path, 'masks.npy'), masks)
82
83
print('labels.npy, images.npy, masks.npy saved in', args.path)