def corrupt_image(img, MAR_prob=0, min_rects=0, max_rects=0, min_width=0, max_width=0):
new_img = img.copy()
mask = np.zeros(img.shape[0:2], dtype=np.bool)
if MAR_prob > 0:
mask[(random_sample(mask.shape) < MAR_prob)] = True
if max_rects > 0 and max_width > 0:
h, w = mask.shape
num_rects = random_integers(min_rects, max_rects)
for i in range(num_rects):
px1 = random_integers(0, w - min(max(min_width, 1), w))
py1 = random_integers(0, h - min(max(min_width, 1), h))
px2 = px1 + (min_width - 1) + random_integers(0, max(min(w - px1 - min_width, max_width - min_width), 0));
py2 = py1 + (min_width - 1) + random_integers(0, max(min(h - py1 - min_width, max_width - min_width), 0));
if px1 <= px2 and py1 <= py2:
mask[py1:py2, px1:px2] = True
else:
# One of the sides has length 0, so we should remove any pixels4
pass
if len(new_img.shape) == 2:
new_img[mask] = 0
else:
new_img[mask,:] = 0
return (new_img, 1.0 * mask)
# Process command line inputs
generate_missing_data.py 文件源码
python
阅读 32
收藏 0
点赞 0
评论 0
评论列表
文章目录