Diff of /Dataset/sliceimages.py [000000] .. [6d4adb]

Switch to unified view

a b/Dataset/sliceimages.py
1
import os, glob
2
import nibabel as nib
3
import numpy as np
4
import cv2
5
from bayesnoiseremoval import BayesPreprocessor as bp
6
7
# STEP 1 - Load and visualize data
8
imgPath = '../../mr_train/'
9
maskPath = '../../train_labels/'
10
11
imagePathInput = os.path.join(imgPath)
12
maskPathInput = os.path.join(maskPath)
13
14
imgOutput = './zdim/imagesbayes/'
15
maskOutput = './zdim/masks/'
16
imageSliceOutput = os.path.join(imgOutput)
17
maskSliceOutput = os.path.join(maskOutput)
18
19
# STEP 2 - Image normalization
20
HOUNSFIELD_MIN = -1000
21
HOUNSFIELD_MAX = 2000
22
HOUNSFIELD_RANGE = HOUNSFIELD_MAX - HOUNSFIELD_MIN
23
24
# STEP 3 - Slicing and saving
25
SLICE_X = False
26
SLICE_Y = False
27
SLICE_Z = True
28
29
SLICE_DECIMATE_IDENTIFIER = 3
30
31
32
# Normalize image
33
def normalizeImageIntensityRange(img):
34
    img[img < HOUNSFIELD_MIN] = HOUNSFIELD_MIN
35
    img[img > HOUNSFIELD_MAX] = HOUNSFIELD_MAX
36
    return (img - HOUNSFIELD_MIN) / HOUNSFIELD_RANGE
37
38
39
def readImageVolume(imgPath, normalize=False):
40
    img = nib.load(imgPath).get_fdata()
41
    if normalize:
42
        return normalizeImageIntensityRange(img)
43
    else:
44
        return img
45
46
47
# Save volume slice to file
48
49
def saveSlice(img, fname, path):
50
    # img = np.uint8(img * 255)
51
    fout = os.path.join(path, f'{fname}.png')
52
    cv2.imwrite(fout, img)
53
    print(f'[+] Slice saved: {fout}', end='\r')
54
55
56
def sliceAndSaveVolumeImage(vol, fname, path):
57
    (dimx, dimy, dimz) = vol.shape
58
    print(dimx, dimy, dimz)
59
    cnt = 0
60
    if SLICE_X:
61
        cnt += dimx
62
        print('Slicing X: ')
63
        for i in range(dimx):
64
            saveSlice(bp.bayes_noise_removal(image=vol[i, :, :], o=140),
65
                      fname + f'-slice{str(i).zfill(SLICE_DECIMATE_IDENTIFIER)}_x', path)
66
67
    if SLICE_Y:
68
        cnt += dimy
69
        print('Slicing Y: ')
70
        for i in range(dimy):
71
            saveSlice(bp.bayes_noise_removal(image=vol[:, i, :], o=140),
72
                      fname + f'-slice{str(i).zfill(SLICE_DECIMATE_IDENTIFIER)}_y', path)
73
74
    if SLICE_Z:
75
        cnt += dimz
76
        print('Slicing Z: ')
77
        for i in range(dimz):
78
            saveSlice(bp.bayes_noise_removal(image=vol[:, :, i], o=140),
79
                      fname + f'-slice{str(i).zfill(SLICE_DECIMATE_IDENTIFIER)}_z', path)
80
    return cnt
81
82
83
for index, filename in enumerate(sorted(glob.iglob(imagePathInput + '*.nii.gz'))):
84
    img = readImageVolume(filename)
85
    print(filename, img.shape, np.sum(img.shape), np.min(img), np.max(img))
86
    numOfSlices = sliceAndSaveVolumeImage(img, 'heart' + str(index), imageSliceOutput)
87
    print(f'\n{filename}, {numOfSlices} slices created \n')
88
89
# # Read and process image mask volumes
90
# for index, filename in enumerate(sorted(glob.iglob(maskPathInput + '*.nii'))):
91
#     img = readImageVolume(filename, False)
92
#     print(filename, img.shape, np.sum(img.shape), np.min(img), np.max(img))
93
#     numOfSlices = sliceAndSaveVolumeImage(img, 'tooth' + str(index), maskSliceOutput)
94
#     print(f'\n{filename}, {numOfSlices} slices created \n')