def make_fmo(path, gt_path, video_path):
    n_im = 5
    background_images = os.listdir(g_background_image_path)
    seq_name = random.choice(background_images)
    seq_images = glob.glob(os.path.join(g_background_image_path, seq_name, '*.jpg'))
    if (len(seq_images) <= n_im):
        seq_images = glob.glob(os.path.join(g_background_image_path, seq_name, '*.png'))
    seq_images.sort()
    bgri = random.randint(n_im, (len(seq_images) - 1))
    bgr_path = seq_images[bgri]
    B0 = (cv2.imread(bgr_path) / 255)
    B = cv2.resize(B0, dsize=(int(((g_resolution_x * g_resolution_percentage) / 100)), int(((g_resolution_y * g_resolution_percentage) / 100))), interpolation=cv2.INTER_CUBIC)
    B[(B > 1)] = 1
    B[(B < 0)] = 0
    FH = np.zeros(B.shape)
    MH = np.zeros(B.shape[:2])
    pars = np.array([[(((B.shape[0] - 1) / 2) - 1), (((B.shape[1] - 1) / 2) - 1)], [1.0, 1.0]]).T
    FM = np.zeros((B.shape[:2] + (4, g_fmo_steps)))
    centroids = np.zeros((2, g_fmo_steps))
    for ki in range(g_fmo_steps):
        FM[:, :, :, ki] = (cv2.imread(os.path.join(gt_path, 'image-{:06d}.png'.format((ki + 1))), cv2.IMREAD_UNCHANGED) / g_rgb_color_max)
        props = regionprops((FM[:, :, (- 1), ki] > 0).astype(int))
        if (len(props) != 1):
            return False
        centroids[:, ki] = props[0].centroid
    for ki in range(g_fmo_steps):
        F = (FM[:, :, :(- 1), ki] * FM[:, :, (- 1):, ki])
        M = FM[:, :, (- 1), ki]
        if (ki < (g_fmo_steps - 1)):
            pars[:, 1] = (centroids[:, (ki + 1)] - centroids[:, ki])
        H = renderTraj(pars, np.zeros(B.shape[:2]))
        H /= (H.sum() * g_fmo_steps)
        for kk in range(3):
            FH[:, :, kk] += signal.fftconvolve(H, F[:, :, kk], mode='same')
        MH += signal.fftconvolve(H, M, mode='same')
    Im = (FH + ((1 - MH)[:, :, np.newaxis] * B))
    Im[(Im > 1)] = 1
    Im[(Im < 0)] = 0
    if g_skip_low_contrast:
        Diff = np.sum(np.abs((Im - B)), 2)
        meanval = np.mean(Diff[(MH > 0.05)])
        print('Contrast {}'.format(meanval))
        if (meanval < 0.2):
            return False
    if g_skip_small:
        sizeper = (np.sum((MH > 0.01)) / (MH.shape[0] * MH.shape[1]))
        print('Size percentage {}'.format(sizeper))
        if (sizeper < 0.05):
            return False
    Im = Im[:, :, [2, 1, 0]]
    Ims = Image.fromarray((Im * 255).astype(np.uint8))
    Ims.save(path)
    Ball = np.zeros((B.shape + (n_im,)))
    Ball[:, :, :, 0] = B
    for ki in range(1, n_im):
        bgrki_path = seq_images[(bgri - ki)]
        Ball[:, :, :, ki] = cv2.resize((cv2.imread(bgrki_path) / 255), dsize=(int(((g_resolution_x * g_resolution_percentage) / 100)), int(((g_resolution_y * g_resolution_percentage) / 100))), interpolation=cv2.INTER_CUBIC)
    Ball[(Ball > 1)] = 1
    Ball[(Ball < 0)] = 0
    Bmed = np.median(Ball, 3)
    Image.fromarray((B[:, :, [2, 1, 0]] * 255).astype(np.uint8)).save(os.path.join(gt_path, 'bgr.png'))
    Image.fromarray((Bmed[:, :, [2, 1, 0]] * 255).astype(np.uint8)).save(os.path.join(gt_path, 'bgr_med.png'))
    if False:
        Fwr = ((FM[:, :, :(- 1), :] * FM[:, :, (- 1):, :]) + (1 * (1 - FM[:, :, (- 1):, :])))
        Fwr = (Fwr * 255).astype(np.uint8)
        out = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*'MJPG'), 6, (F.shape[1], F.shape[0]), True)
        for ki in range(g_fmo_steps):
            out.write(Fwr[:, :, :, ki])
        out.release()
    return True
def make_fmo(path, gt_path, video_path):
    n_im = 5
    background_images = os.listdir(g_background_image_path)
    seq_name = random.choice(background_images)
    seq_images = glob.glob(os.path.join(g_background_image_path, seq_name, '*.jpg'))
    if (len(seq_images) <= n_im):
        seq_images = glob.glob(os.path.join(g_background_image_path, seq_name, '*.png'))
    seq_images.sort()
    bgri = random.randint(n_im, (len(seq_images) - 1))
    bgr_path = seq_images[bgri]
    B0 = (cv2.imread(bgr_path) / 255)
    B = cv2.resize(B0, dsize=(int(((g_resolution_x * g_resolution_percentage) / 100)), int(((g_resolution_y * g_resolution_percentage) / 100))), interpolation=cv2.INTER_CUBIC)
    B[(B > 1)] = 1
    B[(B < 0)] = 0
    FH = np.zeros(B.shape)
    MH = np.zeros(B.shape[:2])
    pars = np.array([[(((B.shape[0] - 1) / 2) - 1), (((B.shape[1] - 1) / 2) - 1)], [1.0, 1.0]]).T
    FM = np.zeros((B.shape[:2] + (4, g_fmo_steps)))
    centroids = np.zeros((2, g_fmo_steps))
    for ki in range(g_fmo_steps):
        FM[:, :, :, ki] = (cv2.imread(os.path.join(gt_path, 'image-{:06d}.png'.format((ki + 1))), cv2.IMREAD_UNCHANGED) / g_rgb_color_max)
        props = regionprops((FM[:, :, (- 1), ki] > 0).astype(int))
        if (len(props) != 1):
            return False
        centroids[:, ki] = props[0].centroid
    for ki in range(g_fmo_steps):
        F = (FM[:, :, :(- 1), ki] * FM[:, :, (- 1):, ki])
        M = FM[:, :, (- 1), ki]
        if (ki < (g_fmo_steps - 1)):
            pars[:, 1] = (centroids[:, (ki + 1)] - centroids[:, ki])
        H = renderTraj(pars, np.zeros(B.shape[:2]))
        H /= (H.sum() * g_fmo_steps)
        for kk in range(3):
            FH[:, :, kk] += signal.fftconvolve(H, F[:, :, kk], mode='same')
        MH += signal.fftconvolve(H, M, mode='same')
    Im = (FH + ((1 - MH)[:, :, np.newaxis] * B))
    Im[(Im > 1)] = 1
    Im[(Im < 0)] = 0
    if g_skip_low_contrast:
        Diff = np.sum(np.abs((Im - B)), 2)
        meanval = np.mean(Diff[(MH > 0.05)])
        print('Contrast {}'.format(meanval))
        if (meanval < 0.2):
            return False
    if g_skip_small:
        sizeper = (np.sum((MH > 0.01)) / (MH.shape[0] * MH.shape[1]))
        print('Size percentage {}'.format(sizeper))
        if (sizeper < 0.05):
            return False
    Im = Im[:, :, [2, 1, 0]]
    Ims = Image.fromarray((Im * 255).astype(np.uint8))
    Ims.save(path)
    Ball = np.zeros((B.shape + (n_im,)))
    Ball[:, :, :, 0] = B
    for ki in range(1, n_im):
        bgrki_path = seq_images[(bgri - ki)]
        Ball[:, :, :, ki] = cv2.resize((cv2.imread(bgrki_path) / 255), dsize=(int(((g_resolution_x * g_resolution_percentage) / 100)), int(((g_resolution_y * g_resolution_percentage) / 100))), interpolation=cv2.INTER_CUBIC)
    Ball[(Ball > 1)] = 1
    Ball[(Ball < 0)] = 0
    Bmed = np.median(Ball, 3)
    Image.fromarray((B[:, :, [2, 1, 0]] * 255).astype(np.uint8)).save(os.path.join(gt_path, 'bgr.png'))
    Image.fromarray((Bmed[:, :, [2, 1, 0]] * 255).astype(np.uint8)).save(os.path.join(gt_path, 'bgr_med.png'))
    if False:
        Fwr = ((FM[:, :, :(- 1), :] * FM[:, :, (- 1):, :]) + (1 * (1 - FM[:, :, (- 1):, :])))
        Fwr = (Fwr * 255).astype(np.uint8)
        out = cv2.VideoWriter(video_path, cv2.VideoWriter_fourcc(*'MJPG'), 6, (F.shape[1], F.shape[0]), True)
        for ki in range(g_fmo_steps):
            out.write(Fwr[:, :, :, ki])
        out.release()
    return True