Example #1
0
def tangentCoordinates(N_32F):
    N = N_32F.reshape(-1, 3)

    v_z = np.array([0.0, 0.0, 1.0])
    A = np.cross(N, v_z)

    sin_theta = normVectors(A)
    cos_theta = np.dot(N, v_z)

    v_x = np.array([1.0, 0.0, 0.0])
    v_y = np.array([0.0, 1.0, 0.0])

    T = np.zeros_like(N)
    T[:, :] = np.array([1.0, 0.0, 0.0])
    B = np.zeros_like(N)
    B[:, :] = np.array([0.0, 1.0, 0.0])

    sin_valid = sin_theta > 1e-4

    T[sin_valid, :] = rotateVectors(A, T[sin_valid, :], cos_theta[sin_valid],
                                    sin_theta[sin_valid])
    B[sin_valid, :] = rotateVectors(A, B[sin_valid, :], cos_theta[sin_valid],
                                    sin_theta[sin_valid])

    return T, B
Example #2
0
 def shadingError(self):
     C_diff = self._C_32F - self._C0_32F
     h, w = C_diff.shape[:2]
     C_diff = C_diff.reshape(-1, 3)
     C_diff = normVectors(C_diff)
     C_diff = C_diff.reshape(h, w)
     return C_diff
Example #3
0
    def _illuminationFromColorDifference(self, Cs):
        I0s = np.average(Cs, axis=1)

        I0_ids = self._I_ids(I0s)

        C_map = np.zeros((self._map_size, Cs.shape[1]))
        hist = np.zeros((self._map_size))
        C_map[I0_ids, :] += Cs[:, :]
        hist[I0_ids] += 1.0

        hist_positive = hist > 0

        for ci in xrange(3):
            C_map[hist_positive, ci] *= 1.0 / hist[hist_positive]

        I_map = np.zeros((self._map_size))

        I_min, I_max = self._Iminmax

        Ims = np.linspace(I_min, I_max, num=self._map_size)

        for ci in xrange(3):
            C_map[:, ci] = Rbf(Ims[hist_positive],
                               C_map[hist_positive, ci],
                               smooth=0.0005)(Ims)

        sigma = 0.02
        for mi in xrange(self._map_size):
            c = C_map[mi]
            dc = normVectors(self._map - c)

            dc_i = np.argmin(dc)
            wc = np.exp(-(dc**2) / (sigma**2))

            I = np.dot(wc, Ims) / np.sum(wc)

            #I_map[mi] = I_min + (I_max - I_min) * dc_i / float(self._map_size - 1)
            I_map[mi] = I

        Im_max = 0.0
        for mi in xrange(self._map_size):
            Im_max = max(I_map[mi], Im_max)
            I_map[mi] = Im_max

        I_map = Rbf(Ims, I_map, smooth=0.0005)(Ims)

        #         I_map[np.max(I0_ids):] = I_max

        return I_map[I0_ids]
Example #4
0
    def _selectConstraint(self, p):
        if self._normal_constraints.empty():
            return False

        ps = self._normal_constraints.positions()

        dP = normVectors(ps - p)

        p_min = np.argmin(dP)

        if dP[p_min] < 5:
            self._selected_constraint = self._normal_constraints.constraint(p_min)
            self._n_old = self._selected_constraint.normal()
            return True

        return False
def computeErrors(L, C0_32F, C_32F, Ng_32F, N_32F, A_8U):
    h, w = A_8U.shape
    N_error = angleErros(N_32F.reshape(-1, 3), Ng_32F.reshape(-1, 3)).reshape(h, w)
    N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
    N_error = trim(N_error, A_8U)

    C_error = normVectors(C0_32F.reshape(-1, 3) - C_32F.reshape(-1, 3)).reshape(h, w)
    C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
    C_error = trim(C_error, A_8U)

    I_32F = np.clip(LdotN(L, N_32F), 0.0, 1.0)
    Ig_32F = np.clip(LdotN(L, Ng_32F), 0.0, 1.0)
    I_error = np.abs(I_32F - Ig_32F)
    I_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
    I_error = trim(I_error, A_8U)
    return C_error, N_error, I_error
    def _illuminationFromColorDifference(self, Cs):
        I0s = np.average(Cs, axis=1)

        I0_ids = self._I_ids(I0s)

        C_map = np.zeros((self._map_size, Cs.shape[1]))
        hist = np.zeros((self._map_size))
        C_map[I0_ids, :] += Cs[:, :]
        hist[I0_ids] += 1.0

        hist_positive = hist > 0

        for ci in xrange(3):
            C_map[hist_positive, ci] *= 1.0 / hist[hist_positive]

        I_map = np.zeros((self._map_size))

        I_min, I_max = self._Iminmax

        Ims = np.linspace(I_min, I_max, num=self._map_size)

        for ci in xrange(3):
            C_map[:, ci] = Rbf(Ims[hist_positive], C_map[hist_positive, ci], smooth=0.0005)(Ims)

        sigma = 0.02
        for mi in xrange(self._map_size):
            c = C_map[mi]
            dc = normVectors(self._map - c)

            dc_i = np.argmin(dc)
            wc = np.exp(-(dc ** 2) / (sigma ** 2))

            I = np.dot(wc, Ims) / np.sum(wc)

            # I_map[mi] = I_min + (I_max - I_min) * dc_i / float(self._map_size - 1)
            I_map[mi] = I

        Im_max = 0.0
        for mi in xrange(self._map_size):
            Im_max = max(I_map[mi], Im_max)
            I_map[mi] = Im_max

        I_map = Rbf(Ims, I_map, smooth=0.0005)(Ims)

        #         I_map[np.max(I0_ids):] = I_max

        return I_map[I0_ids]
def computeErrors(L, C0_32F, C_32F, Ng_32F, N_32F, A_8U):
    h, w = A_8U.shape
    N_error = angleErros(N_32F.reshape(-1, 3),
                         Ng_32F.reshape(-1, 3)).reshape(h, w)
    N_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
    N_error = trim(N_error, A_8U)

    C_error = normVectors(C0_32F.reshape(-1, 3) -
                          C_32F.reshape(-1, 3)).reshape(h, w)
    C_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
    C_error = trim(C_error, A_8U)

    I_32F = np.clip(LdotN(L, N_32F), 0.0, 1.0)
    Ig_32F = np.clip(LdotN(L, Ng_32F), 0.0, 1.0)
    I_error = np.abs(I_32F - Ig_32F)
    I_error[A_8U < 0.5 * np.max(A_8U)] = 0.0
    I_error = trim(I_error, A_8U)
    return C_error, N_error, I_error
def tangentCoordinates(N_32F):
    N = N_32F.reshape(-1, 3)

    v_z = np.array([0.0, 0.0, 1.0])
    A = np.cross(N, v_z)

    sin_theta = normVectors(A)
    cos_theta = np.dot(N, v_z)

    v_x = np.array([1.0, 0.0, 0.0])
    v_y = np.array([0.0, 1.0, 0.0])

    T = np.zeros_like(N)
    T[:, :] = np.array([1.0, 0.0, 0.0])
    B = np.zeros_like(N)
    B[:, :] = np.array([0.0, 1.0, 0.0])

    sin_valid = sin_theta > 1e-4

    T[sin_valid, :] = rotateVectors(A, T[sin_valid, :], cos_theta[sin_valid], sin_theta[sin_valid])
    B[sin_valid, :] = rotateVectors(A, B[sin_valid, :], cos_theta[sin_valid], sin_theta[sin_valid])

    return T, B
def errorTable():
    colormap_files = colorMapFiles()
    num_colormap_files = len(colormap_files)
    M_errors = np.zeros((num_colormap_files))

    for mi, colormap_file in enumerate(colormap_files):
        M_32F = loadColorMap(colormap_file)
        C_32F = M_32F.reshape(1, len(M_32F), 3)

        I_32F = np.linspace(0.0, 1.0, len(M_32F))
        I_32F = I_32F.reshape(C_32F.shape[:2])
        reflectance = LambertReflectanceEstimation(C_32F, I_32F)
        Ml = reflectance.shading(I_32F)[0, :, :]

        I0_32F = luminance(C_32F)
        IL_32F = luminance(Ml.reshape(1, -1, 3))

        I_min, I_max = np.min(I0_32F), np.max(I0_32F)

        M_error = normVectors(M_32F - Ml)

        #M_errors[mi] = np.mean(M_error) / (I_max - I_min)

        # M_errors[mi] = computeGradientDistance(M_32F, Ml) / (I_max - I_min)

        #M_errors[mi] = np.linalg.norm(I0_32F - IL_32F) / (I_max - I_min)

        M_errors[mi] = np.mean(M_error) / (I_max - I_min)

        # M_errors[mi] = np.linalg.norm(M_32F - Ml) / (I_max - I_min)
        # M_errors[mi] = compareHist(M_32F, Ml)

    file_path = shapeResultFile("ReflectanceEstimation", "ReflectanceError", file_ext=".npy")
    np.save(file_path, M_errors)

    plt.plot(M_errors)
    plt.show()
def reflectanceEstimationFigure():
    errorTable()
    M_errors = loadReflectanceErrorTable()
    M_error_orders = np.argsort(M_errors)

    print M_errors[M_error_orders]

    colormap_files = colorMapFiles()
    colormap_files = [colormap_files[M_error_order] for M_error_order in M_error_orders]

    colormap_files = colormap_files[0:-1:3]

    Ms = []
    MLs = []
    for colormap_file in colormap_files:
        M_32F = loadColorMap(colormap_file)
        Ms.append(M_32F)

        C_32F = M_32F.reshape(1, len(M_32F), 3)
        I_32F = np.linspace(0.0, 1.0, len(M_32F))
        I_32F = I_32F.reshape(C_32F.shape[:2])
        reflectance = LambertReflectanceEstimation(C_32F, I_32F)
        Ml = reflectance.shading(I_32F)

        MLs.append(Ml[0, :, :])

    num_rows = 3
    num_cols = len(colormap_files)

    w = 10
    h = w * num_rows / num_cols

    fig, axes = plt.subplots(figsize=(w, h))
    font_size = 15
    fig.subplots_adjust(left=0.02, right=0.98, top=0.98, bottom=0.02, hspace=0.05, wspace=0.05)
    fig.suptitle("", fontsize=font_size)

    N_sphere, A_32F = normalSphere(h=512, w=512)
    Lg = normalizeVector(np.array([-0.2, 0.3, 0.6]))

    plot_grid = SubplotGrid(num_rows, num_cols)

    mi = 1

    for M, Ml in zip(Ms, MLs):
        CM_32F = ColorMapShader(M).diffuseShading(Lg, N_sphere)
        CL_32F = ColorMapShader(Ml).diffuseShading(Lg, N_sphere)
        C_error = normVectors((CM_32F - CL_32F).reshape(-1, 3)).reshape(CL_32F.shape[:2])
        C_error[A_32F < 0.5 * np.max(A_32F)] = 0.0

        plot_grid.setPlot(1, mi)
        plot_grid.showImage(setAlpha(CM_32F, A_32F), "")
        plot_grid.setPlot(2, mi)
        plot_grid.showImage(setAlpha(CL_32F, A_32F), "")
        plot_grid.setPlot(3, mi)
        plot_grid.showColorMap(C_error, "", v_min=0.0, v_max=0.3, with_colorbar=False)

        mi += 1

    file_path = shapeResultFile("ReflectanceEstimation", "ReflectanceEstimationError")
    fig.savefig(file_path, transparent=True)