MOSAIC数据增强

yolo系列中的MOSAIC数据增强

  • MOSAIC代码
  • 图片

MOSAIC代码

def mosaicDataAugment(img):
    images = []
    images.append(img)
    images.append(img)
    images.append(img)
    images.append(img)
    
    xc, yc = np.random.randint(320, 960, 2)
    print(xc, yc)
    s = 640
    for i, img in enumerate(images):
        h, w, c = img.shape
        
        if i == 0:
            img4 = np.full((s*2, s*2, c), 114, dtype=np.uint8)
            x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc
            x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h
        elif i == 1:
            x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
            x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), x2a - x1a, h
        elif i == 2:
            x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(yc + h, s * 2)
            x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, y2a - y1a
        elif i == 3:
            x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(yc + h, s * 2)
            x1b, y1b, x2b, y2b = 0, 0, x2a - x1a, y2a - y1a
    
        img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]
    
    cv2.imshow("aa", img4)
    cv2.waitKey()
   
if __name__ == "__main__":
    img = cv2.imread("arg.jpg")
    mosaicDataAugment(img)

图片

MOSAIC数据增强_第1张图片

你可能感兴趣的:(opencv,python,计算机视觉)