File size: 5,416 Bytes
1c40643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import glob
import random
import os
import cv2
import numpy as np
import tqdm
from matplotlib import pyplot as plt

import albumentations as A
# patient009_frame13_03.png
# patient030_frame01_07.png
# patient049_frame11_02.png
# patient069_frame01_00.png
# patient085_frame01_09.png

def visualize(image, mask, original_image=None, original_mask=None):
    fontsize = 18

    if original_image is None and original_mask is None:
        f, ax = plt.subplots(2, 1, figsize=(8, 8))

        ax[0].imshow(image)
        ax[1].imshow(mask)
    else:
        f, ax = plt.subplots(2, 2, figsize=(8, 8))

        ax[0, 0].imshow(original_image)
        ax[0, 0].set_title('Original image', fontsize=fontsize)

        ax[1, 0].imshow(original_mask)
        ax[1, 0].set_title('Original mask', fontsize=fontsize)

        ax[0, 1].imshow(image)
        ax[0, 1].set_title('Transformed image', fontsize=fontsize)

        ax[1, 1].imshow(mask)
        ax[1, 1].set_title('Transformed mask', fontsize=fontsize)
    plt.show()
# image_path=r'C:\Users\zhang\PycharmProjects\mmsegmentation\data\mr-cardiac\train\image'
# label_path=r'C:\Users\zhang\PycharmProjects\mmsegmentation\data\mr-cardiac\train\label'

# image_paths=glob.glob(os.path.join(image_path,'*.png'))
# test=cv2.imread(r'C:\Users\zhang\PycharmProjects\mmsegmentation\data\mr-cardiac\train\label\mr_train_1001_image_100.png')
# print(np.unique(test))
# for path in tqdm.tqdm(image_paths):

# filename=path.split('\\')[-1]
# print(path,filename)

image = cv2.imread('./mri_train_2d/image/mr_train_1011_image_100.png',0)
mask = cv2.imread('./mri_train_2d/label/mr_train_1011_image_100.png',0)
# print(image.dtype,mask.dtype,np.unique(mask))
# print(np.unique(mask))
# print(image.shape, mask.shape)
original_height, original_width = image.shape[:2]

f, ax = plt.subplots(2, 7, figsize=(32,8),squeeze=True)
plt.axis('off')

image=A.resize(image,256,256)
mask=A.resize(mask,256,256)
ax[0,0].imshow(image,cmap='gray',)
ax[0,0].axis('off')

ax[1,0].imshow(mask,cmap='CMRmap')
ax[1,0].axis('off')
plt.gcf().tight_layout()
aug=A.VerticalFlip()
augmented = aug(image=image, mask=mask)
image_heavy = augmented['image']
mask_heavy = augmented['mask']
ax[0,1].imshow(image_heavy,cmap='gray')
ax[0,1].axis('off')
ax[1,1].imshow(mask_heavy,cmap='CMRmap')
ax[1,1].axis('off')

aug=A.RandomRotate90()
augmented = aug(image=image, mask=mask)
image_heavy = augmented['image']
mask_heavy = augmented['mask']
ax[0,2].imshow(image_heavy,cmap='gray')
ax[0,2].axis('off')
ax[1,2].imshow(mask_heavy,cmap='CMRmap')
ax[1,2].axis('off')


aug=A.RandomSizedCrop(min_max_height=(100,200), height=256,
                      width=256)
augmented = aug(image=image, mask=mask)
image_heavy = augmented['image']
mask_heavy = augmented['mask']
ax[0,3].imshow(image_heavy,cmap='gray')
ax[0,3].axis('off')
ax[1,3].imshow(mask_heavy,cmap='CMRmap')
ax[1,3].axis('off')

aug=A.ElasticTransform(alpha=120, sigma=120 * 0.05,
                               alpha_affine=120 * 0.03)
augmented = aug(image=image, mask=mask)
image_heavy = augmented['image']
mask_heavy = augmented['mask']
ax[0,4].imshow(image_heavy,cmap='gray')
ax[0,4].axis('off')
ax[1,4].imshow(mask_heavy,cmap='CMRmap')
ax[1,4].axis('off')


aug=A.GridDistortion()
augmented = aug(image=image, mask=mask)
image_heavy = augmented['image']
mask_heavy = augmented['mask']
ax[0,5].imshow(image_heavy,cmap='gray')
ax[0,5].axis('off')
ax[1,5].imshow(mask_heavy,cmap='CMRmap')
ax[1,5].axis('off')

aug=A.OpticalDistortion(distort_limit=2, shift_limit=0.5)
augmented = aug(image=image, mask=mask)
image_heavy = augmented['image']
mask_heavy = augmented['mask']
ax[0,6].imshow(image_heavy,cmap='gray')
ax[0,6].axis('off')


ax[1,6].imshow(mask_heavy,cmap='CMRmap')
ax[1,6].axis('off')
plt.tight_layout()
plt.subplots_adjust(wspace=0,hspace=0)
plt.show()
# f.savefig('a.png',bbox_inches='tight')


# aug = A.Compose([
#     # A.PadIfNeeded(min_height=128,min_width=128,value=0,p=1),
#     A.RandomSizedCrop(min_max_height=(128,256), height=original_height,
#                       width=original_width, p=0.5),
#     A.VerticalFlip(p=0.5),
#     A.RandomRotate90(p=0.5),
#     A.OneOf([
#         A.ElasticTransform(alpha=120, sigma=120 * 0.05,
#                            alpha_affine=120 * 0.03, p=0.5),
#         A.GridDistortion(p=0.5),
#         A.OpticalDistortion(distort_limit=2, shift_limit=0.5, p=1)
#     ], p=0.8),
#     A.CLAHE(p=0.8),
#     A.RandomBrightnessContrast(p=0.8),
#     A.RandomGamma(p=0.8)
#     ]
# )



# random.seed(11)
# augmented = aug(image=image, mask=mask)
#
# image_heavy = augmented['image']
# mask_heavy = augmented['mask']
# # print(mask_heavy.shape,np.unique(mask_heavy))
# label_num=len(np.unique(mask_heavy))
# # print(image_heavy.dtype,mask_heavy.dtype)
# # if label_num>=2:
#     # print(filename,np.unique(mask_heavy))
#     # cv2.imwrite(os.path.join(image_path.replace('train','aug_train'),'aug_v2'+filename),image_heavy)
#     # cv2.imwrite(os.path.join(label_path.replace('train', 'aug_train'),
#     #                          'aug_v2' + filename), mask_heavy)
# visualize(image_heavy, mask_heavy, original_image=image,
#           original_mask=mask)
    # visualize(image, mask)
# print(type(image),image_heavy.shape)

# break