• mosaic实现


    step1. 假设模型输入尺寸为s,生成一幅尺寸为2s * 2s的灰色图
    step2. 从点A(s/2, s/2)和点B(3s/2, 3s/2)限定的矩形内随机选择一点作为拼接点
    step3. 随机选择四张图,取其部分拼入该图,如下图所示,四种颜色代表四张样本图,超出的部分将被舍弃
    在这里插入图片描述

    import numpy as np
    import random
    import cv2
    
    
    def load_image(f, img_size):
        im = cv2.imread(f)  # BGR
        h0, w0 = im.shape[:2]  # orig hw
        r = img_size / max(h0, w0)  # ratio
        if r != 1:  # if sizes are not equal
            interp = cv2.INTER_LINEAR if r > 1 else cv2.INTER_AREA
            im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interp)
        return im, (h0, w0), im.shape[:2]  # im, hw_original, hw_resized
    
    
    s = 640
    yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in [-s // 2, -s // 2])  # mosaic center x, y
    img4 = np.full((s * 2, s * 2, 3), 114, dtype=np.uint8)  # base image with 4 tiles
    
    img, _, (h, w) = load_image("1.jpg",s)  # top left
    x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc  # xmin, ymin, xmax, ymax (large image)
    x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h  # xmin, ymin, xmax, ymax (small image)
    img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]  
    
    img, _, (h, w) = load_image("2.jpg",s)  # top right
    x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
    x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
    img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]  
    
    img, _, (h, w) = load_image("3.jpg",s)  # bottom left
    x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
    x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
    img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]  
    
    img, _, (h, w) = load_image("4.jpg",s)  # bottom right
    x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
    x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
    img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]  
    
    cv2.imwrite("mosaic.jpg",img4)
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    量化交易系列【3】字符串与时间处理
    在Java中操作Redis_Spring Data Redis使用方式——环境搭建
    交换网络----三种生成树协议
    基于python下django框架 实现网上疫苗预约系统详细设计
    [echarts] 两侧堆叠柱状图
    二叉树的遍历方式和代码
    c++day4
    虹科示波器 | 汽车免拆检修 | 2015款奔驰G63AMG车发动机偶尔自动熄火
    flutter简单的本地草稿箱功能
    aspnetcore使用websocket实时更新商品信息
  • 原文地址:https://blog.csdn.net/taifyang/article/details/127820125