https://blog.csdn.net/Guo_Python/article/details/105989946
# target_dict = {}
# target_dict.setdefault('id', []).append(22)
# print(target_dict)
#
#
# from PIL import Image, ImageDraw
#
# im01 = Image.open("C:\\Users\\wei\\Desktop\\lqq.jpg")
# print("im01======",type(im01))
# draw = ImageDraw.Draw(im01)
#
# draw.text((0,0),"lqq", fill = (255,0,0))
#
# im01.show()
#
import random
import math
import cv2
import numpy as np
class RandomErasing:
"""Random erasing the an rectangle region in Image.
Class that performs Random Erasing in Random Erasing Data Augmentation by Zhong et al.
Args:
sl: min erasing area region
sh: max erasing area region
r1: min aspect ratio range of earsing region
p: probability of performing random erasing
"""
def __init__(self, p=0.5, sl=0.02, sh=0.4, r1=0.3):
self.p = p
self.s = (sl, sh)
self.r = (r1, 1 / r1)
def __call__(self, img):
"""
perform random erasing
Args:
img: opencv numpy array in form of [w, h, c] range
from [0, 255]
Returns:
erased img
"""
assert len(img.shape) == 3, 'image should be a 3 dimension numpy array'
if random.random() > self.p:
return img
else:
while True:
Se = random.uniform(*self.s) * img.shape[0] * img.shape[1]
re = random.uniform(*self.r)
He = int(round(math.sqrt(Se * re)))
We = int(round(math.sqrt(Se / re)))
xe = random.randint(0, img.shape[1])
ye = random.randint(0, img.shape[0])
if xe + We <= img.shape[1] and ye + He <= img.shape[0]:
img[ye: ye + He, xe: xe + We, :] = np.random.randint(low=0, high=255, size=(He, We, img.shape[2]))
return img
if __name__ == "__main__":
img = cv2.imread("C:\\Users\\wei\\Desktop\\lqq.jpg")
RE = RandomErasing(p=1)
for i in range(20):
img1 = RE(img.copy())
cv2.imshow("test", img1)
cv2.waitKey(1000)