예제 #1
0
def gauss_filter(fps, transforms_abs, smooth_percent):
    smoothing = round((int(fps) / 100) * int(smooth_percent))
    mu = smoothing
    s = mu * 2 + 1

    sigma2 = (mu / 2)**2

    kernel = np.exp(-(np.arange(s) - mu)**2 / sigma2)

    transforms_filtered = [0] * len(transforms_abs)
    tlen = len(transforms_abs)
    for i in range(tlen):
        ## make a convolution:
        weightsum, avg = 0.0, datatypes.transform(0, 0, 0)
        for k in range(s):
            idx = i + k - mu
            if idx >= 0 and idx < tlen:
                weightsum += kernel[k]
                avg = add_transforms(
                    avg, mult_transforms(transforms_abs[idx], kernel[k]))

        if weightsum > 0:
            avg = mult_transforms(avg, 1.0 / weightsum)
            ## high frequency must be transformed away
            transforms_filtered[i] = sub_transforms(transforms_abs[i], avg)

    return transforms_filtered
예제 #2
0
def convert_relative_transforms_to_absolute(transforms_rel):
    '''Relative to absolute (integrate transformations)'''
    transforms_abs = [0] * len(transforms_rel)
    transforms_abs[0] = datatypes.transform(0, 0, 0)
    t = transforms_rel[0]
    for i in range(1, len(transforms_rel)):
        transforms_abs[i] = add_transforms(transforms_rel[i], t)
        t = transforms_abs[i]

    return transforms_abs
예제 #3
0
def get_global_motions(f):
    cfg = config.cfg

    motions = []

    lines = f.read().splitlines()
    for line in lines:
        if not line[0] == '#':
            data = line.split()
            motions.append(
                datatypes.transform(float(data[1]), float(data[2]),
                                    float(data[3])))

    return motions
예제 #4
0
def parseTransformsTrf(fpath):
    cfg = config.cfg

    f = open(fpath)
    lines = f.read().splitlines()

    ver_line = lines[0]
    match = re.match(r'VID.STAB (.+)', ver_line)
    version = int(match.group(1).strip())
    if version < 1:
        exit("transforms.trf version error")
    if version > 1:
        exit("Version of VID.STAB file too large: got " + version)

    lines = lines[1:]
    trf_frames = []
    for line in lines:
        if not line.strip()[0] == "#":
            match = re.match(r'.*\[(.*)\]', line)
            lm_strs = match.group(1).split(",")
            lms = []
            if len(match.group(1)):
                for lm_str in lm_strs:
                    match = re.match(r'\(LM(.+)\)', lm_str.strip())
                    if match:
                        nums = match.group(1).split()
                        lms.append(
                            LM(int(nums[0]), int(nums[1]), int(nums[2]),
                               int(nums[3]), int(nums[4]), float(nums[5]),
                               float(nums[6])))

                trf_frames.append(lms)

    print("Compute frame transforms")

    frame_motions = [datatypes.transform(0.0, 0.0, 0.0)]
    for f_cnt, motions in enumerate(trf_frames):

        match_quals = []
        ## calculates means transform to initialise gradient descent
        if motions:
            xsum, ysum = 0.0, 0.0
            for ilm in range(len(motions)):
                match_quals.append(motions[ilm].match)
                xsum += motions[ilm].vx
                ysum += motions[ilm].vy
                t = array("f", [xsum / len(motions), ysum / len(motions), 0])
        else:
            t = array("f", [0.0] * 3)

        mu = stats.fmean(match_quals)
        psd = stats.pstdev(match_quals, mu)  # population standard distribution

        # first we throw away those fields that match badly (during motion detection)
        # filter fields by match quality
        disableFields(motions, mu, psd, 1.5)

        stepsizes = [0.2, 0.2, 0.00005]  # x, y, roll
        residual = [0.0]
        for k in range(3):
            # optimize `t' to minimize transform quality (12 steps per dimension)
            resgd = gradientDescent(t, motions, stepsizes.copy(), residual)

            # now we need to ignore the fields that don't fit well (e.g. moving objects)
            # cut off everything above 1 std. dev. for skewed distributions
            # this will cut off the tail
            # do this only two times (3 gradient optimizations in total)
            if (k == 0 and residual[0] > 0.1) or (k == 1 and residual[0] > 20):
                disableFields(motions, mu, psd, 1.0)
                t = resgd
            else:
                break

        frame_motions.append(datatypes.transform(resgd[0], resgd[1], resgd[2]))
        print_progress(f_cnt, len(trf_frames))

    return frame_motions
예제 #5
0
def mult_transforms(t, s):
    return datatypes.transform(t.x * s, t.y * s, t.roll * s)
예제 #6
0
def add_transforms(m1, m2):
    return datatypes.transform(m1.x + m2.x, m1.y + m2.y, m1.roll + m2.roll)
예제 #7
0
def sub_transforms(m1, m2):
    return datatypes.transform(m1.x - m2.x, m1.y - m2.y, m1.roll - m2.roll)