Пример #1
0
def zl_predict(ls, dist_max, u0, v0, width, height, params):
    hist = []
    ang = []
    for i in range(ls.shape[0]):
        ang.append(line_angle2(ls[i, :]))
        if ang[i] < -np.pi / 4:
            ang[i] = ang[i] + np.pi
        if np.abs(ang[i] - np.pi / 2) < params.theta_v:
            l = line_hmg_from_two_points(ls[i, 0:2], ls[i, 2:4])
            dist = np.abs(np.dot(l, np.array([width / 2, height / 2, 1])))
            if dist < dist_max:
                hist.append(ang[i])

    [N, edges0] = np.histogram(
        hist,
        np.arange(np.pi / 2 - np.pi / 8, np.pi / 2 + np.pi / 8 + np.pi / 180,
                  np.pi / 180))
    N[np.where(N <= 5)[0]] = 0
    if np.sum(N) == 0:
        N[int(np.round(len(N) / 2 + 0.1)) - 1] = 1
    edges = (edges0[:-1] + edges0[1:]) / 2

    max_modes = np.zeros([2 * len(N)])
    H = np.zeros([len(N)])
    Nout = mnf_modes.mnf(np.double(N), len(N), 1, max_modes, H)
    max_modes = max_modes[:Nout * 2]
    max_modes = max_modes.reshape(2, Nout).T
    H = H[:Nout]
    H = H.reshape(Nout)

    if len(max_modes) == 0 or max_modes.shape[1] == 0:
        max_modes = np.array([[1, N.shape[0]]])
        H = 0
    else:
        I = np.argsort(-H)
        H.sort()
        H = H[::-1]
        max_modes = max_modes[I, :]
    zl = []
    for i in range(max_modes.shape[0]):
        Ni = np.zeros(N.shape[0])
        a = max_modes[i, 0]
        b = max_modes[i, 1]
        Ni[int(a - 1):int(b)] = N[int(a - 1):int(b)]
        m = np.max(Ni)
        I = np.where(Ni == m)[0]
        j = I[0]
        a = edges[j]
        l = np.abs(v0 / np.sin(a))
        zl.append(u0 + l * np.cos(np.pi - a))

    return zl
Пример #2
0
def vp_predict(lines_homo, initialIds, horizon_homo, params):
    # compute intersections between the line segments and the HL candidate

    inter_homo = np.cross(lines_homo[:, initialIds].T, horizon_homo).T
    inter_homo = inter_homo / np.sqrt(np.sum(np.power(inter_homo, 2), axis=0))
    inter_homo = inter_homo * np.sign(inter_homo[2, :] + np.finfo(float).eps)
    inter_homo[2, :] = np.where(inter_homo[2, :] != 0, inter_homo[2, :],
                                np.finfo(float).eps)
    inter_pts = inter_homo[:2, :] / inter_homo[2, :]

    # compute the MMMs of the coordinate histogram
    max_modes = []
    a = horizon_homo[0]
    b = horizon_homo[1]
    c = horizon_homo[2]

    A_hmg = np.cross(horizon_homo, np.array([b, -a, 0]))
    A = np.array([A_hmg[0] / A_hmg[2], A_hmg[1] / A_hmg[2]])
    rho = np.abs(c) / np.sqrt(np.power(a, 2) + np.power(b, 2))
    rho2 = np.sqrt(inter_pts[0, :] * inter_pts[0, :] +
                   inter_pts[1, :] * inter_pts[1, :])

    if rho > 1:
        p = np.arccos(rho / rho2) / np.pi
    else:
        p = np.zeros(rho2.shape)
        d = np.sqrt(np.abs(rho2 * rho2 - np.power(rho, 2)))
        I = np.where(rho2 <= 1)[0]
        if len(I) != 0:
            p[I] = d[I] / np.pi
        I = np.where(rho2 > 1)[0]
        if len(I) != 0:
            d2 = np.sqrt(rho2 * rho2 - 1)
            beta = np.arctan(d2)
            p[I] = (beta[I] + d[I] - d2[I]) / np.pi

    tmp = inter_pts - np.array([A]).T.dot(np.ones([1, inter_pts.shape[1]]))
    dt = np.array([b, -a]).dot(tmp)
    I = np.where(dt < 0)[0]
    p[I] = -p[I]
    [N, edges] = np.histogram(p, params.L_vp)

    # import scipy.io as sio
    # tmp_mat = sio.loadmat('vp_Nedges.mat')
    # [N, edges] = [tmp_mat['N'][0], tmp_mat['edges'][0]]

    max_modes = np.zeros([2 * len(N)])
    H = np.zeros([len(N)])
    Nout = mnf_modes.mnf(np.double(N), len(N), 400, max_modes, H)
    max_modes = max_modes[:Nout * 2]
    max_modes = max_modes.reshape(2, Nout).T
    H = H[:Nout]
    H = H.reshape(Nout)

    if len(max_modes) == 0:
        max_modes = np.array([])
        H = 0
    else:
        I = np.argsort(-H)
        H.sort()
        H = H[::-1]
        max_modes = max_modes[I, :]

    horgroups = []
    scores = []
    horvps_homo = []

    for i in range(max_modes.shape[0]):
        Ni = np.zeros([N.shape[0]])
        a = max_modes[i, 0]
        b = max_modes[i, 1]
        Ni[int(a) - 1:int(b)] = N[int(a) - 1:int(b)]
        m = np.max(Ni)
        j = np.argmax(Ni)
        p_i = (edges[j] + edges[j + 1]) / 2
        vpId = np.argmin(np.abs(p - p_i))
        horvps_homo.append(inter_homo[:, vpId])
        scores.append(m)
        edgesId = np.intersect1d(
            np.where(p >= edges[int(a) - 1])[0],
            np.where(p <= edges[int(b)])[0])
        horgroups.append(edgesId)

    if len(max_modes) == 0:
        scores = [0]
        sc = 0
        horvps_homo = np.array([])

    else:
        #refine the VPs according to [Zhai et al.] and/or compute the scores
        if params.hvp_refinement:
            horvps_homo = vp_refinement(lines_homo, horvps_homo, horizon_homo,
                                        params)

        [scores, horgroups] = vp_score(horvps_homo, lines_homo,
                                       params.score_function)

        #sorted by score
        sortIds = np.argsort(-scores)
        scores.sort()
        scores = scores[::-1]
        horvps_homo = np.array(horvps_homo)[sortIds]
        horgroups = horgroups[sortIds]
        nvps = np.min([len(horgroups), 2])
        sc = np.sum(scores[:nvps])

    return [sc, horvps_homo, horgroups]
Пример #3
0
import mnf_modes
import numpy as np

histo = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,15,20,19,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], dtype=np.double)
N = 45
epsilon = 1
outArray1 = np.zeros([2*N])
outArray2 = np.zeros([N])

print(len(outArray1))

Nout = mnf_modes.mnf(histo, N, epsilon,outArray1, outArray2)
max_modes = outArray1[:Nout*2]
max_modes = max_modes.reshape(2, Nout).T
H = outArray2[:Nout]
H = H.reshape(1, Nout).T
print(max_modes)
print(H)
Пример #4
0
def hl_predict(seglines, zenith_homo, u0, v0, width, height, focal, params):
    L = params.L_h
    x2 = zenith_homo[0] / zenith_homo[2]
    y2 = zenith_homo[1] / zenith_homo[2]
    tilt = np.arctan(x2 / y2)
    if np.isnan(tilt):
        tilt = 0
    offsets = []
    for i in range(seglines.shape[0]):
        v = np.array([seglines[i, 2] - seglines[i, 0], seglines[i, 3] - seglines[i, 1]])
        scal = np.dot(v/linalg.norm(v), np.array([np.cos(-tilt), np.sin(-tilt)]))
        ang = np.arccos(scal)
        if np.abs(ang) < params.theta_h * np.pi / 180:
            tmp = np.dot(np.array([(seglines[i, 2] + seglines[i, 0])/2 - u0, (seglines[i, 3] + seglines[i, 1])/2 - v0]),
                         np.array([np.cos(-tilt + np.pi / 2), np.sin(-tilt + np.pi/2)]))
            offsets.append(tmp)
    offsets = np.asarray(offsets)

    if len(offsets) == 0:
        offsets = np.arange(-height/2, (height+1)/2)
    [N, edges] = np.histogram(offsets, L)

    # import scipy.io as sio
    # tmp_mat = sio.loadmat('N_edges.mat')
    # [N, edges] = [tmp_mat['N'][0], tmp_mat['edges'][0]]


    max_modes = np.zeros([2 * len(N)])
    H = np.zeros([len(N)])
    Nout = mnf_modes.mnf(np.double(N), len(N), 1, max_modes, H)
    max_modes = max_modes[:Nout * 2]
    max_modes = max_modes.reshape(2, Nout).T
    H = H[:Nout]
    H = H.reshape(Nout)

    # Modification 1
    if Nout == 0:
        max_modes = np.array([[1, N.shape[0]]])
        H = np.array([-1])
    else:
        I = np.argsort(-H)
        H.sort()
        H = H[::-1]
        max_modes = max_modes[I, :]
    nmodes = max_modes.shape[0]
    modes_offset = []
    modes_left = []
    modes_right = []
    modes_homo = []
    for i in range(nmodes):
        Ni = np.zeros(N.shape[0])
        a = max_modes[i, 0]
        b = max_modes[i, 1]
        Ni[int(a)-1 : int(b)] = N[int(a)-1 : int(b)]
        bin = np.argmax(Ni)


        #modes_offset.append(edges[0] + (bin + 1) / L * (edges[L-1] - edges[0]))
        modes_offset.append(edges[0] + (bin + 1) / L * (edges[L] - edges[0]))
        mnf_center = np.array([u0 + modes_offset[-1] * np.cos(-tilt + np.pi/2), v0 + modes_offset[-1] * np.sin(-tilt + np.pi/2)])
        hmnf = line_hmg_from_two_points(mnf_center, mnf_center + np.array([np.cos(-tilt), np.sin(-tilt)]))
        modes_left.append(-hmnf[2] / hmnf[1])
        modes_right.append((-hmnf[0]*width -hmnf[2])/ hmnf[1])
        mode_seg = np.array([[0, modes_left[0], width, modes_right[0]]])
        modes_homo.append(np.squeeze(normalize.normalize(mode_seg, width, height, focal)))

    modes_offset = np.array(modes_offset)
    modes_left = np.array(modes_left)
    modes_right = np.array(modes_right)
    modes_homo = np.array(modes_homo).T

    return [modes_homo, modes_offset, modes_left, modes_right, H]
Пример #5
0
def calculate_histogram(hvps_consensus_rectified, root, plot_redundant):
    hvp_cum = np.vstack(hvps_consensus_rectified)
    hvp_cum_pos = np.array([z_i if z_i[0] >= 0 else -z_i for z_i in hvp_cum])

    hvp_degrees = np.degrees(np.arctan2(hvp_cum_pos[:, 0], hvp_cum_pos[:, 2]))
    # hvp_periodic = np.where(hvp_degrees > 0, hvp_degrees, hvp_degrees+180)
    hvp_periodic = hvp_degrees

    hvp_periodic_2x = np.hstack([hvp_periodic, hvp_periodic + 180])
    hvp_periodic_3x = np.hstack(
        [hvp_periodic - 180, hvp_periodic, hvp_periodic + 180])

    his_edge = 1

    if plot_redundant:
        plt.figure()
        plt.hist(hvp_periodic, np.arange(181))
        plt.title('histogram of angles (0-180)')
        plt.xlabel('degree')
        plt.ylabel('number')
        plt.savefig(os.path.join(root, 'histogram.jpg'))

        plt.figure()
        plt.hist(hvp_periodic_2x, np.arange(361))
        plt.title('histogram of angles (0-360)')
        plt.xlabel('degree')
        plt.ylabel('number')
        plt.savefig(os.path.join(root, 'histogram_2x.jpg'))

        plt.figure()
        plt.hist(hvp_periodic_3x, np.arange(-181, 362))
        plt.title('histogram of angles (-180-360)')
        plt.xlabel('degree')
        plt.ylabel('number')
        plt.savefig(os.path.join(root, 'histogram_3x.jpg'))

    if his_edge == 1:
        hvp_histogram = np.histogram(hvp_periodic_3x, np.arange(-180, 361))
    elif his_edge == 2:
        hvp_histogram = np.histogram(hvp_periodic_3x, np.arange(-180, 361, 2))
    elif his_edge == 3:
        hvp_histogram = np.histogram(hvp_periodic_3x, np.arange(-180, 361, 3))
    elif his_edge == 'auto':
        hvp_histogram = np.histogram(hvp_periodic_3x, 'auto')

    [N, edges] = hvp_histogram

    max_modes = np.zeros([2 * len(N)])
    H = np.zeros([len(N)])

    his_epsilon = 1
    Nout = mnf_modes.mnf(np.double(N), len(N), his_epsilon, max_modes, H)
    max_modes = max_modes[:Nout * 2]
    max_modes = max_modes.reshape(2, Nout).T
    H = H[:Nout]
    H = H.reshape(Nout)

    if len(max_modes) == 0:
        max_modes = np.array([])
        H = 0
    else:
        I = np.argsort(-H)
        H.sort()
        H = H[::-1]
        max_modes = max_modes[I, :]

    horgroups = []
    # scores = []
    # horvps_homo = []

    three_x_list = []
    three_y_list = []

    one_x_list = []
    one_y_list = []

    two_x_list = []
    two_y_list = []

    for i in range(max_modes.shape[0]):
        Ni = np.zeros([N.shape[0]])
        a = max_modes[i, 0]
        b = max_modes[i, 1]
        Ni[int(a) - 1:int(b)] = N[int(a) - 1:int(b)]
        m = np.max(Ni)
        j = np.argmax(Ni)

        three_x_list.append(j - 180)
        three_y_list.append(m)

        # p_i = (edges[j] + edges[j+1]) / 2
        # vpId = np.argmin(np.abs(p - p_i))
        # horvps_homo.append(inter_homo[:,vpId])
        # scores.append(m)

        if 0 <= j - 180 / his_edge < 180 / his_edge:

            #print('range{}-{}'.format(j - 180/his_edge, j - 180/his_edge + 1))
            one_x_list.append(j - 180)
            one_y_list.append(m)

            two_x_list.append(j - 180)
            two_y_list.append(m)
            two_x_list.append(j - 180 + 180)
            two_y_list.append(m)

            ############## Wrong shouldn't be a and b (fixed)

            edgesId = np.intersect1d(np.where(hvp_periodic >= edges[j]),
                                     np.where(hvp_periodic < edges[j + 1]))
            horgroups.append(edgesId)

    final_hvps_rectified = []
    for i in range(len(horgroups)):
        tmp = calculate_consensus_vps(hvp_cum_pos[horgroups[i]], 'svd')
        # at most two vanishing points will be chosen
        if i < 2:
            final_hvps_rectified.append(tmp)

    if plot_redundant:
        # plot peaks on -180-360
        plt.figure()
        plt.hist(hvp_periodic_3x, np.arange(-181, 362))
        plt.title('histogram of angles (-180-360)')
        plt.scatter(three_x_list, three_y_list, s=20, c='r')
        plt.xlabel('degree')
        plt.ylabel('number')
        plt.savefig(os.path.join(root, 'peaks_on_histogram_3x.jpg'))

        # plot peaks on 0-180
        plt.figure()
        plt.hist(hvp_periodic, np.arange(181))
        plt.title('histogram of angles (0-180)')
        plt.scatter(one_x_list, one_y_list, s=20, c='r')
        plt.xlabel('degree')
        plt.ylabel('number')
        plt.savefig(os.path.join(root, 'peaks_on_histogram.jpg'))

        # plot peaks on 0-180
        plt.figure()
        plt.hist(hvp_periodic_2x, np.arange(361))
        plt.title('histogram of angles (0-360)')
        plt.scatter(two_x_list, two_y_list, s=20, c='r')
        plt.xlabel('degree')
        plt.ylabel('number')
        plt.savefig(os.path.join(root, 'peaks_on_histogram_2x.jpg'))

    return final_hvps_rectified