Exemplo n.º 1
0
    trans_opts = GetTransOpts()
    trans_opts['xz_theta'] = func.RandomAngle(-np.pi/36, np.pi/36)
    trans_opts['py'] = func.RandomDis((-40, -40), (40, 40))
    trans3_1.QuickTrans(['py', 'xz'], trans_opts)

    trans3_2 = Trans(trans3_1.obj_imB)
    trans_opts['xz_theta'] += func.NormalAngle(0, 1, 'd')
    trans_opts['py'] += func.NormalDis(0, 2)
    trans3_2.QuickTrans(['py', 'xz'], trans_opts)

####################################
    mainboard1 = Board([640, 480])
    mainboard1.addTrans(trans_back)
    mainboard1.addTrans(trans1_1)
    mainboard1.addTrans(trans2_1)
    mainboard1.addTrans(trans3_1)
    mainboard1.Gen()
    mainboard1.Save(dict(zip(['imA', 'imB', 'flowAB_viz'],
                             name[0:3])))

    mainboard2 = Board([640, 480])
    mainboard2.addTrans(trans_back)
    mainboard2.addTrans(trans1_2)
    mainboard2.addTrans(trans2_2)
    mainboard2.addTrans(trans3_2)
    mainboard2.Gen()
    mainboard2.Save(dict(zip(['imB', 'flowAB_viz'],
                             name[3:])))
    print(f'完成:{i}/{num}')
print('全部完成!')
Exemplo n.º 2
0
size2 = [50, 50]

obj1_rect = Rect(pos, size)
obj1_data = RectArray(obj1_rect, 3)
obj1_data.SetColor([255, 0, 255])


obj1_datamask = RectArray(obj1_rect, 1, dtype=np.bool)
obj1_datamask.SetValue(True)

obj1_mask_rect = Rect(Point(75, 75), size2)
obj1_true_datamask = RectArray(obj1_mask_rect, 1, dtype=np.bool)
obj1_true_datamask.SetValue(False)

obj1_datamask.AddRectArray(obj1_true_datamask, CSYS='local')


obj1 = Obj(obj1_data, obj1_datamask)
trans = Trans(obj1)
pts = trans.GenTrans('py')
trans.ImposeTrans(pts)
#DisplayObject(obj1)
mainboard = Board([640, 480])
mainboard.addTrans(trans)
mainboard.Gen()
mainboard.Display('imA')
mainboard.Display('imB')
#print(obj1)
#look = obj1.data_
#look2 = obj1.dataMask_
Exemplo n.º 3
0
class MainBoard(object):
    '''
    Board类的管理类
    辅助储存board的opts、obj、trans等属性,便于连续生成多幅图像
    '''
    def __init__(self, obj_num, mainboard_opts=DEFAULT_MAINBOARD_OPTS):
        self.obj_num = obj_num
        self.opts = mainboard_opts
        self.objDict = {}
        self.transOptsDict = {}
        self.transOptsDict_customize = {}
        # ======= func ========
        self.initBoard()
        self.initRandomImgGenerator()
        self.initBackground()
        for numid in range(obj_num):
            self.initForeground(numid + 1)  # 0号编号属于背景,从1开始

    # ================  init  =========================
    def initBoard(self):
        board_size = self.opts['board_size']
        self.board = Board(board_size)

    def initRandomImgGenerator(self):
        back_name = self.opts['background_name']
        fore_name = self.opts['foreground_name']
        back_opts = self.opts['background_RandomImg_opts']
        fore_opts = self.opts['foreground_RandomImg_opts']
        # back
        if isinstance(back_name, str):
            if back_opts is None:
                back_opts = GetRandomImgOpts(back_name)
        elif isinstance(back_name, list):
            assert back_opts is None or len(back_opts) == len(back_name)
            if back_opts is None:
                back_name = np.random.choice(back_name)
                back_opts = GetRandomImgOpts(back_name)
            else:
                ids = np.random.randint(0, len(back_name))
                back_name = back_name[ids]
                if back_opts[ids] is None:
                    back_opts = GetRandomImgOpts(back_name)
                else:
                    back_opts = back_opts[ids]
        # fore
        if isinstance(fore_name, str):
            if fore_opts is None:
                fore_opts = GetRandomImgOpts(fore_name)
        elif isinstance(fore_name, list):
            assert fore_opts is None or len(fore_opts) == len(fore_name)
            if fore_opts is None:
                fore_name = np.random.choice(fore_name)
                fore_opts = GetRandomImgOpts(fore_name)
            else:
                ids = np.random.randint(0, len(fore_name))
                fore_name = fore_name[ids]
                if fore_opts[ids] is None:
                    fore_opts = GetRandomImgOpts(fore_name)
                else:
                    fore_opts = fore_opts[ids]
        # 赋值
        self.backgroundGenerator = RandomImg(back_name, back_opts)
        self.foregroundGenerator = RandomImg(fore_name, fore_opts)

    def initBackground(self, numid=0):
        # 用于初始化背景对象
        board_size = self.opts['board_size']
        background_iniMethod = self.opts['background_iniMethod']
        assert background_iniMethod in [
            'slice_central', 'slice_random', 'zoom', 'define'
        ]
        # 设置背景的图片 及 图片的位置和大小
        while (True):
            background = self.backgroundGenerator.RandomGet()  # 读取背景图
            assert background is not None
            if background_iniMethod == 'define':
                # <define 通过直接缩放后确定左上角位置,不进行切片,有待改进>
                back_minpos = self.opts['background_minIniPos']
                back_maxpos = self.opts['background_maxIniPos']
                back_size = self.opts['background_size']
                pos0 = func2.RandomPoint(back_minpos, back_maxpos)
                background = background.resize(back_size)
                break
            if background_iniMethod == 'slice_central':
                # 检查尺寸,进行缩放
                qx, raw_size, tag_size = self.initBackground_check(background)
                if qx == 'f':
                    continue
                elif qx == 's':
                    background = Zoom(background, raw_size, tag_size)
                new_size = np.array(background.size)
                if not (new_size >= tag_size).all():
                    continue  # 以防万一
                cen_pos = new_size - tag_size
                box = (cen_pos[0], cen_pos[1], cen_pos[0] + tag_size[0],
                       cen_pos[1] + tag_size[1])
                background = background.crop(box)
                back_size = tag_size
                pos0 = Point((np.array(board_size) - tag_size) / 2)
                break
            elif background_iniMethod == 'slice_random':
                # 几乎和slice_central一模一样
                qx, raw_size, tag_size = self.initBackground_check(background)
                if qx == 'f':
                    continue
                elif qx == 's':
                    background = Zoom(background, raw_size, tag_size)
                new_size = np.array(background.size)
                if not (new_size >= tag_size).all():
                    continue  # 以防万一
                ran_pos = func.RandomSize((0, 0), new_size - tag_size)
                box = (ran_pos[0], ran_pos[1], ran_pos[0] + tag_size[0],
                       ran_pos[1] + tag_size[1])
                background = background.crop(box)
                back_size = tag_size
                pos0 = Point((np.array(board_size) - tag_size) / 2)
                break
            elif background_iniMethod == 'zoom':
                qx, raw_size, tag_size = \
                    self.initBackground_check(background, 2)
                if qx == 'f':
                    continue
                # 几乎和slice_central一模一样,区别在于必定缩放
                background = Zoom(background, raw_size, tag_size)
                new_size = np.array(background.size)
                if not (new_size >= tag_size).all():
                    continue  # 以防万一
                box = (ran_pos[0], ran_pos[1], ran_pos[0] + tag_size[0],
                       ran_pos[1] + tag_size[1])
                box = (ran_pos[0], ran_pos[1], tag_size[0], tag_size[1])
                background = background.crop(box)
                back_size = tag_size
                pos0 = Point((np.array(board_size) - tag_size) / 2)
                break
        # 创建背景 Rect 和 RectArray
        back_rect = Rect(pos0, back_size)
        back_data = RectArray(back_rect, 3)
        # 创建背景obj
        back_data.SetRectData(np.array(background))
        back_datamask = RectArray(back_rect, 1, dtype=np.bool)
        back_datamask.SetValue(True)
        obj_back = Obj(back_data, back_datamask)
        self.objDict[numid] = obj_back
        self.transOptsDict[numid] = TransOptsManager()  # 私人变换数据
        self.transOptsDict_customize[numid] = False

    def initBackground_check(self, backgroud, check=None):
        check = self.opts['background_check'] if check is None else check
        board_size = np.array(self.opts['board_size'])
        safety_factor = self.opts['background_safety_factor']
        raw_back_size = np.array(backgroud.size, dtype=np.int)
        target_back_size = np.array(board_size * safety_factor, dtype=np.int)
        if (raw_back_size < board_size).all():
            qx = 's' if check == 0 else 'f'
        elif (raw_back_size < target_back_size).all():
            qx = 's' if check == 0 or check == 1 else 'f'
        else:
            qx = 'q'
        return (qx, raw_back_size, target_back_size)

    def initForeground(self, numid):
        # 用于初始化前景对象
        board_size = self.opts['board_size']
        froe_minpos = self.opts['foreground_minIniPos']
        froe_maxpos = self.opts['foreground_maxIniPos']
        foreground_iniPos = self.opts['foreground_iniPosMethod']
        assert foreground_iniPos in ['auto', 'define']
        foreground_iniPosStandard = self.opts['foreground_iniPosStandard']
        assert foreground_iniPosStandard in ['central', 'topleft']
        foreground_size = self.opts['foreground_size']
        # 读入前景
        img, imgmask = self.foregroundGenerator.RandomGet()
        froesize = np.array(img.size)  # (x,y)
        if (froesize > foreground_size).any():
            b = np.max(froesize / foreground_size)
            b = 1 if b < 1 else b  # 以防万一
            froesize = np.array(froesize / (1 +
                                            (b - 1) * np.random.uniform(0, 1)),
                                dtype=np.int)
            img = img.resize(froesize)
            imgmask = imgmask.resize(froesize)
        im = np.array(img)
        immask = np.array(imgmask) > 0
        if foreground_iniPos == 'auto':
            pos = func2.RandomPoint((0, 0), board_size)
        elif foreground_iniPos == 'define':
            pos = func2.RandomPoint(froe_minpos, froe_maxpos)
        if foreground_iniPosStandard == 'central':
            # print(pos)
            pos = pos - froesize / 2
        elif foreground_iniPosStandard == 'topleft':
            pass
        froe_rect = Rect(pos, froesize)
        # print(froe_rect)
        froe_data = RectArray(froe_rect, 3)
        froe_data.SetRectData(im)
        froe_datamask = RectArray(froe_rect, 1, dtype=np.bool)
        froe_datamask.SetValue(immask)
        obj_froe = Obj(froe_data, froe_datamask)
        self.objDict[numid] = obj_froe
        self.transOptsDict[numid] = TransOptsManager()  # 私人变换数据
        self.transOptsDict_customize[numid] = False

    # ==========================================

    def SetTransOptsDict(self, id_, transOM):
        assert id_ in ['back', 'fore'] or id_ in self.objDict
        if id_ in self.objDict:
            self.transOptsDict[id_] = transOM
            self.transOptsDict_customize[id_] = True
        else:
            self.transOptsDict[id_] = transOM

    def GetTransOpts(self, id_):
        assert id_ in ['back', 'fore'] or id_ in self.objDict
        if id_ in self.objDict:  # 个人设置优先级高
            return self.transOptsDict[id_]
        elif id_ in ['back', 'fore']:
            if id_ in self.transOptsDict:
                return self.transOptsDict[id_]
            else:
                self.transOptsDict[id_] = TransOptsManager()
                return self.transOptsDict[id_]

    # ==========================
    def TransAllOnce(self, nameDict):
        # 产生一次图
        # 对于背景
        # <多背景需要对这里进行修改>
        back_transOM = self.GetTransOpts('back')
        obj_back = self.objDict[0]
        self.objDict[0] = TransSingalOnce(obj_back, self.board, back_transOM)
        # </>
        for i in range(self.obj_num):
            # 对于所有的前景
            obj_transOM = self.GetTransOpts(i + 1)
            if self.transOptsDict_customize[i + 1] is False:
                # 从前景拷贝副本,但是保留自身的变换矩阵
                fore_transOM = self.GetTransOpts('fore')
                obj_transOM.Copyfrom(fore_transOM)
            obj_fore = self.objDict[i + 1]  # 0是背景
            obj_fore_ = TransSingalOnce(obj_fore, self.board, obj_transOM)
            self.objDict[i + 1] = obj_fore_
        self.board.Gen()
        self.board.Save(nameDict)
Exemplo n.º 4
0
        obj_data.SetRectData(im)
        obj_datamask = RectArray(obj_rect, 1, dtype=np.bool)
        obj_datamask.SetValue(immask)
        obj = Obj(obj_data, obj_datamask)
        # 通过trans初始化obj的姿态,初始化不保存光流
        trans = Trans(obj)
        trans_opts = GetTransOpts()
        trans_opts['xz_theta'] = func.RandomAngle(-np.pi/2, np.pi/2)
        trans_opts['py'] = func.RandomDis((-100, -100), (100, 100))
        trans.QuickTrans(['py', 'xz'], [trans_opts])
        # 更新board
        initboard.addTrans(trans)
        # 存入列表
        obj_list.append(trans.obj_imB)
    # 生成、保存
    initboard.Gen()
    name_front = 0
    initboard.Save({'imB': name[name_front]})
    name_front += 1

    # =============== 第一次生成区域 ==================
    # 生成背景
    obj_back = obj_list[0]
    trans_back = Trans(obj_back)
    trans_back_opts = GetTransOpts()
    # <这里插入对opts的修改>
    trans_back_opts['xz'] = func.RandomAngle(-1, 1, unit='d')
    trans_back_opts['py'] = func.RandomDis((-5, -5), (5, 5))
    trans_back_opts['sf'] = func.RandomScale((0.98, 0.98), (1.02, 1.02))
    # <>
    trans_back.QuickTrans(['py', 'xz', 'sf'], [trans_back_opts])
Exemplo n.º 5
0
    obj3_datamask = RectArray(obj3_rect, 1, dtype=np.bool)
    obj3_datamask.SetValue(immask3)
    obj3 = Obj(obj3_data, obj3_datamask)

    trans3_0 = Trans(obj3)
    trans_opts = GetTransOpts()
    trans_opts['xz_theta'] = func.RandomAngle(-np.pi / 2, np.pi / 2)
    trans_opts['py'] = func.RandomDis((-100, -100), (100, 100))
    trans3_0.QuickTrans(['py', 'xz'], trans_opts)

    trans3_1 = Trans(trans3_0.obj_imB)
    trans_opts = GetTransOpts()
    trans_opts['xz_theta'] = func.RandomAngle(-np.pi / 36, np.pi / 36)
    trans_opts['py'] = func.RandomDis((-40, -40), (40, 40))
    trans3_1.QuickTrans(['py', 'xz'], trans_opts)

    mainboard1 = Board([640, 480])
    mainboard1.addTrans(trans1_1)
    mainboard1.addTrans(trans2_1)
    mainboard1.addTrans(trans3_1)
    mainboard1.Gen()
    mainboard1.Save(dict(zip(['imA', 'imB', 'flowAB_viz'], name[0:3])))

    #    mainboard2 = Board([640, 480])
    #    mainboard2.addTrans(trans1_2)
    #    mainboard2.Gen()
    #    mainboard2.Save(dict(zip(['imB', 'flowAB_viz'],
    #                             name[3:])))
    print(f'完成:{i}/{num}')
print('全部完成!')