def gaussian2D(shape, sigma=1):
"""
compute gaussian
"""
m, n = [(ss - 1.) / 2. for ss in shape]
y, x = np.ogrid[-m:m + 1, -n:n + 1]
h = np.exp(-(x * x + y * y) / (2 * sigma * sigma)) # 离原点越近越大
h[h < np.finfo(h.dtype).eps * h.max()] = 0 # np.finfo(h.dtype).eps是指非负的最小值
h = h * 255
h = np.stack((h,) * 3, axis = -1).astype(int)
return h
def draw_umich_gaussian(heatmap, center):
"""
draw gaussian in heatmap
"""
heatmap = np.int64(heatmap)
height, width = heatmap.shape[0:2]
radius = 100
diameter = 2 * radius + 1 # radius
# compute gaussian value
gaussian = gaussian2D((diameter, diameter), sigma=diameter / 6)
plt.imshow(gaussian)
plt.show()
x, y = int(center[0]), int(center[1]) # 获得整形的中点坐标
#gaussian[:, :, 0] = gaussian[:, :, 0] * (255 - heatmap[x, y, 0])
#gaussian[:, :, 1] = gaussian[:, :, 1] * (255 - heatmap[x, y, 1])
#gaussian[:, :, 2] = gaussian[:, :, 2] * (255 - heatmap[x, y, 2])
#gaussian.astype(int)
# get gaussian map pos
left, right = min(x, radius), min(height - x, radius + 1) # 如果xy落在heatmap的边上,离边的距离小于r,就要限制一下防止越界
top, bottom = min(y, radius), min(width - y, radius + 1)
# get masked heatmap pos
masked_heatmap = heatmap[x - left:x + right, y - top:y + bottom, :] # 得到我们要替换heatmap的位置
masked_gaussian = gaussian[radius - left:radius + right, radius - top:radius + bottom, :] # 得到可用高斯的范围
heatmap[x - left:x + right, y - top:y + bottom, :] = masked_heatmap + masked_gaussian
print(masked_gaussian.dtype, masked_heatmap.dtype, heatmap.dtype)
#heatmap[heatmap > 255] = 255
heatmap = np.clip(heatmap, 0, 255)
heatmap = np.uint8(heatmap)
return heatmap
def over_pixel(img, labels, ovpix_prob):
img_w, img_h, _ = img.shape
label = random.choice(labels)
if random.random() < ovpix_prob:
start_x = int(np.random.uniform(label[1] , label[3]))
start_y = int(np.random.uniform(label[0] , label[2]))
img = draw_umich_gaussian(img, (start_x, start_y))
cv2.imshow("over_image", img)
cv2.waitKey()
return img
对图像施加人工过曝处理
最新推荐文章于 2024-07-06 12:03:33 发布