灰度图像阵列旋转以进行数据增强

时间:2018-08-04 15:07:36

标签: python scipy image-segmentation grayscale image-rotation

我正在尝试旋转一些图像以进行数据增强,以训练网络进行图像分割任务。经过大量搜索之后,旋转每个图像及其对应的蒙版的最佳候选方法是使用scipy.ndimage.rotate函数,但是这样做的问题是旋转蒙版图像numpy数组(其中仅包含0和255的值像素值),旋转的蒙版具有从0到255的所有值,而我希望蒙版数组的像素值只有0和255。

代码如下:

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)

以下是结果:

[  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)

旋转图像阵列似乎是一个简单的问题,但是我花了几天的时间,但没有找到解决此问题的任何方法。我真的很困惑如何防止旋转后的掩码数组值(0和255)采用从0到255的所有值。我的意思是这样的:

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]

1 个答案:

答案 0 :(得分:0)

由于您使用numpy数组表示图像,为什么不使用numpy函数?该库具有各种数组操作。尝试the rot90 function

相关问题