Peiman Mohseni
Peiman Mohseni

Reputation: 23

Grayscale image array rotation for data augmentation

I am trying to rotate some images for data augmentation to train a network for image segmentation task. After searching a lot, the best candidate for rotating each image and its corresponding mask was to use the scipy.ndimage.rotate function, but the problem with this is that after rotating the mask image numpy array ( which includes only 0 and 255 values for pixel values) the rotated mask has got all the values from 0 to 255 while I expect the mask array to have only 0 and 255 as its pixel values.

Here is the code:

from scipy.ndimage import rotate
import numpy as np

ample = dataset[1]
print(np.unique(sample['image']))
print(np.unique(sample['mask']))
print(sample['image'].shape)
print(sample['mask'].shape)
rot_image = rotate(sample['image'], 60, reshape = False)
rot_mask = rotate(sample['mask'], 60, reshape = False)
print(np.unique(rot_image))
print(np.unique(rot_mask))
print(rot_image.shape)
print(rot_mask.shape)

Here are the results:

[  0   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 118 119 120 121 125 139]
[  0 255]
(512, 512, 1)
(512, 512, 1)
[  0   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 115 117 118 124 125 132 135]
[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  17  18  20
  24  25  26  28  31  34  35  38  39  41  42  43  45  46  48  49  50  51
  52  58  59  62  66  67  68  73  75  76  79  80  82  85  86  88  90  96
  98 101 108 109 111 114 116 118 119 123 124 125 127 128 130 138 140 142
 146 148 151 156 157 158 161 164 165 166 168 169 176 180 184 185 188 189
 194 196 197 198 199 201 203 204 205 207 208 210 211 213 216 217 218 219
 220 221 222 225 228 229 230 231 233 234 235 237 239 240 241 242 243 244
 245 246 247 248 249 250 251 252 253 254 255]
(512, 512, 1)
(512, 512, 1)

It seems to be a simple problem to rotate image array, but I'm searching for days and I didn't find any solution to this problem. I am really confused how to prevent mask array values( 0 and 255) to take all values from 0 to 255 after rotation. I mean something like this:

x = np.unique(sample['mask'])
rot_mask = rotate(sample['mask'], 30, reshape = False)
x_rot = np.unique(rot_mask)
print(np.unique(x - x_rot))
[ 0]

Upvotes: 1

Views: 893

Answers (1)

Baptiste Merliot
Baptiste Merliot

Reputation: 861

Since you are using numpy arrays to represent images, why not using numpy functions? This library has all sorts of array manipulations. Try the rot90 function

Upvotes: 0

Related Questions