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) 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'],
obj1_datamask.SetValue(immask) # #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_opts = GetTransOpts() trans_opts['xz_theta'] = func.RandomAngle(np.pi / 2) trans_opts['py'] = func.RandomDis((-100, -100), (100, 100)) trans.GenTrans('py', trans_opts) trans.GenTrans('xz', trans_opts) trans.ImposeTrans() #DisplayObject(obj1) mainboard = Board([640, 480]) mainboard.addTrans(trans) mainboard.Gen() mainboard.Save({'imA': '../data/ds_v2/TEST2/1A.jpg'}) mainboard.Save({'imB': '../data/ds_v2/TEST2/1B.jpg'}) mainboard.Save({'flowA_viz': '../data/ds_v2/TEST2/1gt.jpg'}) look = mainboard.Save({'flowA': '../data/ds_v2/TEST2/1gt.flo'}) #print(obj1) #look = obj1.data_
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_
back_data = RectArray(back_rect, 3) # 设置背景对象 backgroud = backgroundGenerator.RandomGet() backgroud = backgroud.resize(background_size) back_data.SetRectData(np.array(backgroud)) back_datamask = RectArray(back_rect, 1, dtype=np.bool) back_datamask.SetValue(True) obj_back = Obj(back_data, back_datamask) # 生成背景变换 trans_back = Trans(obj_back) trans_back_opts = GetTransOpts() trans_back.QuickTrans(['M'], [trans_back_opts]) # 创建列表储存初始化的obj obj_list = [trans_back.obj_imB] # 初始化obj列表 # initboard 用于保存第一帧 initboard = Board(board_size) initboard.addTrans(trans_back) # 初始化循环生成 obj for _ in range(obj_num): # 随机obj的初始位置及大小 img,imgmask = materialGenerator.RandomGet() im = np.array(img) immask = np.array(imgmask) > 0 pos = func2.RandomPoint(obj_minIniPos, obj_maxIniPos) size = (im.shape[1],im.shape[0]) # 初始化obj obj_rect = Rect(pos, size) obj_data = RectArray(obj_rect, 3) obj_data.SetRectData(im) obj_datamask = RectArray(obj_rect, 1, dtype=np.bool) obj_datamask.SetValue(immask)
def initBoard(self): board_size = self.opts['board_size'] self.board = Board(board_size)
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)
obj1_rect = Rect(pos, size) obj1_data = RectArray(obj1_rect, 3) obj1_data.SetRectData(im) obj1_datamask = RectArray(obj1_rect, 1, dtype=np.bool) obj1_datamask.SetValue(immask) obj1 = Obj(obj1_data, obj1_datamask) # 初始化trans trans = Trans(obj1) trans_opts = GetTransOpts() trans_opts['xz_theta'] = func.RandomAngle(np.pi/2) trans_opts['py'] = func.RandomDis((-100, -100), (100, 100)) trans.QuickTrans(['py', 'xz'], trans_opts) # 另一种写法: # trans.GenTrans('py',trans_opts) # trans.GenTrans('xz',trans_opts) # trans.ImposeTrans() trans2 = Trans(trans.obj_imB) trans_opts = GetTransOpts() trans_opts['xz_theta'] = func.RandomAngle(np.pi/2) trans_opts['py'] = func.RandomDis((-100, -100), (100, 100)) trans2.QuickTrans(['py', 'xz'], trans_opts) mainboard = Board([640, 480]) mainboard.addTrans(trans2) mainboard.Gen() mainboard.Save(dict(zip(['imA', 'imB', 'flowAB', 'flowAB_viz'], name))) print(f'完成:{i}/{num}') print('全部完成!')
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) # 另一种写法: # trans.GenTrans('py',trans_opts) # trans.GenTrans('xz',trans_opts) # trans.ImposeTrans() trans2 = Trans(trans.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)) trans2.QuickTrans(['py', 'xz'], trans_opts) trans3 = Trans(trans2.obj_imB) trans_opts['xz_theta'] trans_opts['py'] trans3.QuickTrans(['py', 'xz'], trans_opts) mainboard1 = Board([640, 480]) mainboard1.addTrans(trans2) mainboard1.Gen() mainboard1.Save( dict(zip(['imA', 'imB', 'flowAB', 'flowAB_viz'], name[0:4]))) mainboard2 = Board([640, 480]) mainboard2.addTrans(trans3) mainboard2.Gen() mainboard2.Save(dict(zip(['imB', 'flowAB', 'flowAB_viz'], name[4:]))) print(f'完成:{i}/{num}') print('全部完成!')