def _runImp(self):
        image = self._scene.image()
        A_8U = alpha(image)

        N_32F = self._scene.normal()

        if N_32F is None:
            return

        h_high, w_high = image.shape[:2]
        w_low = min(256, w_high)
        h_low = w_low * h_high / w_high

        N_32F = cv2.resize(N_32F, (w_low, h_low))
        if A_8U is not None:
            A_8U = cv2.resize(A_8U, (w_low, h_low))

        D_32F = depthFromNormal(N_32F, A_8U)

        d_scale = w_high / float(w_low)
        D_32F = d_scale * D_32F

        D_32F = cv2.resize(D_32F, (w_high, h_high))

        self._scene.setDepth(D_32F)
        self._scene.setDisplayMode(Scene.DisplayDepth)

        self._output_info = "(%s, %s)" %(np.min(D_32F), np.max(D_32F))

        print self._output_info
Пример #2
0
    def _compute(self):
        image = self._image

        img_32F = to32F(self._image)
        self._A_32F = alpha(img_32F)

        sigma_xy = 100.0
        xy = positionFeatures(img_32F) / sigma_xy
        Lab = LabFeatures(img_32F)

        sigma_L = 1.0
        Lab[:, 0] /= sigma_L

        foreground = foreGroundFeatures(img_32F)

        Labxy = np.concatenate((Lab, xy), axis=1)

        Labxy_samples = shuffle(Labxy[foreground, :], random_state=0)[:1000]

        kmeans = sklearn.cluster.KMeans(n_clusters=self._num_colors,
                                        random_state=0).fit(Labxy_samples)

        self._centers = kmeans.cluster_centers_
        self._centers[:, 0] *= sigma_L

        labels = kmeans.predict(Labxy)
        self._labels = labels.reshape(image.shape[:2])
Пример #3
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        C0_32F = to32F(rgb(C0_8U))

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))
        sfs_method = Wu08SFS(L, C0_32F, A_8U)
        sfs_method.run()
        N_32F = sfs_method.normal()

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 1
        num_cols = 2
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(C0_8U, r'Shading: $C$')
        plot_grid.showImage(normalToColor(N_32F, A_8U), r'Estimated Normal: $N$')

        showMaximize()
def bilateralSmoothing(image):
    sigma_xy = 0.1
    xy = positionFeatures(image) / sigma_xy
    Lab = LabFeatures(image)
    foreground = foreGroundFeatures(image)

    Labxy = np.concatenate((Lab, xy), axis=1)[foreground, :]
    sigma_L = 1.0
    Labxy[:, 0] = Labxy[:, 0] / sigma_L
    Labxy_sparse = shuffle(Labxy, random_state=0)[:1000]

    Lab_smooth = np.array(Lab)

    smooth = 0.0

    L_rbf = Rbf(Labxy_sparse[:, 0], Labxy_sparse[:, 1], Labxy_sparse[:, 2],
                Labxy_sparse[:, 3], Labxy_sparse[:, 4], sigma_L * Labxy_sparse[:, 0], function='linear', smooth=smooth)

    a_rbf = Rbf(Labxy_sparse[:, 0], Labxy_sparse[:, 1], Labxy_sparse[:, 2],
                Labxy_sparse[:, 3], Labxy_sparse[:, 4], Labxy_sparse[:, 1], function='linear', smooth=smooth)
    b_rbf = Rbf(Labxy_sparse[:, 0], Labxy_sparse[:, 1], Labxy_sparse[:, 2],
                Labxy_sparse[:, 3], Labxy_sparse[:, 4], Labxy_sparse[:, 2], function='linear', smooth=smooth)

    #Lab_smooth[:, 0] = L_rbf(Labxy[:, 0], Labxy[:, 1], Labxy[:, 2], Labxy[:, 3], Labxy[:, 4])
    Lab_smooth[foreground, 0] = L_rbf(*(Labxy.T))
    Lab_smooth[foreground, 1] = a_rbf(*(Labxy.T))
    Lab_smooth[foreground, 2] = b_rbf(*(Labxy.T))

    h, w = image.shape[:2]
    Lab_smooth = Lab_smooth.reshape((h, w, 3))

    rgb_smooth = Lab2rgb(np.float32(Lab_smooth))
    rgb_smooth_8U = to8U(rgb_smooth)
    rgb_smooth_8U = setAlpha(rgb_smooth_8U, alpha(image))
    return rgb_smooth_8U
Пример #5
0
    def _runImp(self):
        image = self._scene.image()
        A_8U = alpha(image)

        N_32F = self._scene.normal()

        if N_32F is None:
            return

        h_high, w_high = image.shape[:2]
        w_low = min(256, w_high)
        h_low = w_low * h_high / w_high

        N_32F = cv2.resize(N_32F, (w_low, h_low))
        if A_8U is not None:
            A_8U = cv2.resize(A_8U, (w_low, h_low))

        D_32F = depthFromNormal(N_32F, A_8U)

        d_scale = w_high / float(w_low)
        D_32F = d_scale * D_32F

        D_32F = cv2.resize(D_32F, (w_high, h_high))

        self._scene.setDepth(D_32F)
        self._scene.setDisplayMode(Scene.DisplayDepth)

        self._output_info = "(%s, %s)" % (np.min(D_32F), np.max(D_32F))

        print self._output_info
Пример #6
0
    def _compute(self):
        image = self._image

        img_32F = to32F(self._image)
        self._A_32F = alpha(img_32F)

        sigma_xy = 100.0
        xy = positionFeatures(img_32F) / sigma_xy
        Lab = LabFeatures(img_32F)

        sigma_L = 1.0
        Lab[:, 0] /= sigma_L

        foreground = foreGroundFeatures(img_32F)

        Labxy = np.concatenate((Lab, xy), axis=1)

        Labxy_samples = shuffle(Labxy[foreground, :], random_state=0)[:1000]

        kmeans = sklearn.cluster.KMeans(n_clusters=self._num_colors, random_state=0).fit(Labxy_samples)

        self._centers = kmeans.cluster_centers_
        self._centers[:, 0] *= sigma_L

        labels = kmeans.predict(Labxy)
        self._labels = labels.reshape(image.shape[:2])
Пример #7
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        C0_32F = to32F(rgb(C0_8U))

        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))
        sfs_method = Wu08SFS(L, C0_32F, A_8U)
        sfs_method.run()
        N_32F = sfs_method.normal()

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 1
        num_cols = 2
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(C0_8U, r"Shading: $C$")
        plot_grid.showImage(normalToColor(N_32F, A_8U), r"Estimated Normal: $N$")

        showMaximize()
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

#         if A_8U is None:
#             return

        C0_32F = to32F(rgb(C0_8U))
        I_32F = luminance(C0_32F)

        Lab_32F = rgb2Lab(C0_32F)

        th_specular = 0.2
        th_contour = 0.02
        th_material = 0.1
        E_32F = DoG(I_32F, sigma=2.0)

        contour = th_contour * np.min(E_32F) - E_32F
        contour *= 1.0 / np.max(contour)
        contour = np.clip(contour, 0.0, 1.0)
        specular = E_32F - th_specular * np.max(E_32F)
        specular *= 1.0 / np.max(specular)
        specular = np.clip(specular, 0.0, 1.0)

        material = rgb(C0_8U)
#         edge_mask = np.zeros(I_32F.shape, dtype=np.uint8)
#         edge_mask[contour > 0.0] = 1.0
#         material = cv2.inpaint(material, edge_mask, 3, cv2.INPAINT_TELEA)

        for i in xrange(1):
            material = cv2.medianBlur(material, ksize=7)
#         material = th_material * np.max(np.abs(E_32F)) - np.abs(E_32F)
#         material *= 1.0 / np.max(material)
#         material = np.clip(material, 0.0, 1.0)
#         material[material > 0.0] = 1.0
        E_32F[E_32F < 0.0] = 0.0

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 1
        num_cols = 4
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(C0_8U, r'$C$')
        #plot_grid.showImage(setAlpha(C0_32F, material), r'$Material$')
        plot_grid.showImage(setAlpha(material, A_8U), r'$Material$')
        plot_grid.showImage(setAlpha(C0_32F, contour), r'$Contour$')
        plot_grid.showImage(setAlpha(C0_32F, specular), r'$Specular$')

        showMaximize()
Пример #9
0
 def depthImage(self):
     D_min = np.min(self._depth)
     D_max = np.max(self._depth)
     D_32F = (self._depth - D_min) / (D_max - D_min)
     D_8U = to8U(D_32F)
     D_8U = gray2rgb(D_8U)
     A_8U = alpha(self._image)
     D_8U = setAlpha(D_8U, A_8U)
     return D_8U
Пример #10
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        C0_32F = to32F(rgb(C0_8U))
        I_32F = luminance(C0_32F)

        N0_32F, A_8U = loadNormal(
            self.characterResultFile("N0_d.png",
                                     data_name="BaseDetailSepration"))
        Nd_32F, A_8U = loadNormal(
            self.characterResultFile("N_d_smooth.png",
                                     data_name="BaseDetailSepration"))
        Nb_32F, A_8U = loadNormal(
            self.characterResultFile("N_b_smooth.png",
                                     data_name="BaseDetailSepration"))

        W_32F = np.array(Nb_32F[:, :, 2])
        W_32F = W_32F
        W_32F[W_32F < 0.95] = 0.0

        L = lightEstimation(I_32F, N0_32F, A_8U)
        # L = lightEstimationByVoting(I_32F, N0_32F, A_8U)

        L_txt = 0.01 * np.int32(100 * L)

        L_img = lightSphere(L)

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05,
                            right=0.95,
                            top=0.9,
                            hspace=0.12,
                            wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 1
        num_cols = 4
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(C0_8U, r'$C$')
        plot_grid.showImage(normalToColor(N0_32F, A_8U), r'$N$')
        plot_grid.showImage(setAlpha(C0_32F, W_32F), r'$Nd_z$')
        plot_grid.showImage(
            L_img, r'$L: [%s, %s, %s]$' % (L_txt[0], L_txt[1], L_txt[2]))

        showMaximize()
Пример #11
0
def bilateralSmoothing(image):
    sigma_xy = 0.1
    xy = positionFeatures(image) / sigma_xy
    Lab = LabFeatures(image)
    foreground = foreGroundFeatures(image)

    Labxy = np.concatenate((Lab, xy), axis=1)[foreground, :]
    sigma_L = 1.0
    Labxy[:, 0] = Labxy[:, 0] / sigma_L
    Labxy_sparse = shuffle(Labxy, random_state=0)[:1000]

    Lab_smooth = np.array(Lab)

    smooth = 0.0

    L_rbf = Rbf(Labxy_sparse[:, 0],
                Labxy_sparse[:, 1],
                Labxy_sparse[:, 2],
                Labxy_sparse[:, 3],
                Labxy_sparse[:, 4],
                sigma_L * Labxy_sparse[:, 0],
                function='linear',
                smooth=smooth)

    a_rbf = Rbf(Labxy_sparse[:, 0],
                Labxy_sparse[:, 1],
                Labxy_sparse[:, 2],
                Labxy_sparse[:, 3],
                Labxy_sparse[:, 4],
                Labxy_sparse[:, 1],
                function='linear',
                smooth=smooth)
    b_rbf = Rbf(Labxy_sparse[:, 0],
                Labxy_sparse[:, 1],
                Labxy_sparse[:, 2],
                Labxy_sparse[:, 3],
                Labxy_sparse[:, 4],
                Labxy_sparse[:, 2],
                function='linear',
                smooth=smooth)

    #Lab_smooth[:, 0] = L_rbf(Labxy[:, 0], Labxy[:, 1], Labxy[:, 2], Labxy[:, 3], Labxy[:, 4])
    Lab_smooth[foreground, 0] = L_rbf(*(Labxy.T))
    Lab_smooth[foreground, 1] = a_rbf(*(Labxy.T))
    Lab_smooth[foreground, 2] = b_rbf(*(Labxy.T))

    h, w = image.shape[:2]
    Lab_smooth = Lab_smooth.reshape((h, w, 3))

    rgb_smooth = Lab2rgb(np.float32(Lab_smooth))
    rgb_smooth_8U = to8U(rgb_smooth)
    rgb_smooth_8U = setAlpha(rgb_smooth_8U, alpha(image))
    return rgb_smooth_8U
Пример #12
0
def colorToNormal(C_8U, fill_background=False):
    rgb_8U = rgb(C_8U)
    A_8U = alpha(C_8U)

    C_32F = to32F(rgb_8U)

    N_32F = 2.0 * C_32F - 1.0

    if fill_background:
        N_32F[A_8U < 10, :] = np.array([0.0, 0.0, 0.0])

    N_32F_normalized = normalizeImage(N_32F)

    return N_32F_normalized
Пример #13
0
    def _runLayer(self, layer_file):
        if layer_file is None:
            return
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            C0_8U = loadRGB(layer_file)

        if C0_8U is None:
            return

        h, w = C0_8U.shape[:2]
        w_low = 1024
        h_low = w_low * h / w

        #  C0_8U = cv2.resize(C0_8U, (w_low, h_low))

        A_8U = alpha(C0_8U)
        self._A_8U = A_8U

        C0_32F = to32F(rgb(C0_8U))

        if A_8U is not None:
            C0_32F[A_8U < 0.9 * np.max(A_8U), :] = np.array([0, 0, 0])

        self._C0_32F = C0_32F

        self._loadImage()
        self._computeBaseDetalSeparation()
        self._computeInitialDetailNormal()
        self.computeDetailNormal()
        self._computeLumoNormal()
        self.computeInitialNormal()

        # plt.savefig(self.characterResultFile("BumpNormal.png"))

        if self._N_b_smooth is not None:
            self.cleanCharacterResultDir()
            saveNormal(self.characterResultFile("N_b.png"), self._N_b, A_8U)
            saveNormal(self.characterResultFile("N_b_smooth.png"),
                       self._N_b_smooth, A_8U)
            saveNormal(self.characterResultFile("N_d.png"), self._N_d, A_8U)
            saveNormal(self.characterResultFile("N_d_smooth.png"),
                       self._N_d_smooth, A_8U)
            saveNormal(self.characterResultFile("N_lumo.png"), self._N_lumo,
                       A_8U)
            saveNormal(self.characterResultFile("N0_b.png"), self._N0_b_32F,
                       A_8U)
            saveNormal(self.characterResultFile("N0_d.png"), self._N0_d_32F,
                       A_8U)
Пример #14
0
    def _runImp(self):
        image = self._scene.image()
        A_8U = alpha(image)

        if A_8U is None:
            return

        A_8U = cv2.resize(A_8U, None, fx=0.25, fy=0.25)
        N0_32F, N_32F = estimateNormal(A_8U)

        h, w = image.shape[:2]
        N_32F = cv2.resize(N_32F, (w, h))
        self._scene.setNormal(N_32F)
        self._scene.setDisplayMode(Scene.DisplayNormal)
Пример #15
0
    def keyPressEvent(self, e):
        if e.key() == Qt.Key_0:
            self._view.render(self._image)

        if e.key() == Qt.Key_1:
            if self._N_32F is None:
                self._interpolateNormal()
            A_8U = None
            if self._image.shape[2] == 4:
                A_8U = to8U(alpha(self._image))
            self._view.render(normalToColor(self._N_32F, A_8U))

        if e.key() == Qt.Key_2:
            self._interpolateNormal()
            if self._N_32F is None:
                self._interpolateNormal()
            A_8U = None
            if self._image.shape[2] == 4:
                A_8U = to8U(alpha(self._image))
            self._view.render(normalToColor(self._N_32F, A_8U))

        if e.key() == Qt.Key_Delete:
            self._normal_constraints.clear()
            self._view.update()
Пример #16
0
    def showImage(self, image, title, alpha_clip=True, font_size=15):
        plt.subplot(self._num_rows, self._num_cols, self._plot_id)
        plt.title(title, fontsize=font_size)
        if len(image.shape) == 2:
            plt.gray()

        show_image = image
        if alpha_clip:
            if len(image.shape) == 3:
                if image.shape[2] == 4:
                    show_image = trim(image, alpha(image))

        plt.imshow(show_image)
        plt.axis('off')
        self._plot_id += 1
Пример #17
0
    def showImage(self, image, title, alpha_clip=True, font_size=15):
        plt.subplot(self._num_rows, self._num_cols, self._plot_id)
        plt.title(title, fontsize=font_size)
        if len(image.shape) == 2:
            plt.gray()

        show_image = image
        if alpha_clip:
            if len(image.shape) == 3:
                if image.shape[2] == 4:
                    show_image = trim(image, alpha(image))

        plt.imshow(show_image)
        plt.axis('off')
        self._plot_id += 1
Пример #18
0
    def _interpolateNormalAMG(self, N0_32F, W_32F, A_8U):
        h, w = N0_32F.shape[:2]
        A_c, b_c = normalConstraints(W_32F, N0_32F)
        A_8U = None
        if self._image.shape[2] == 4:
            A_8U = to8U(alpha(self._image))
        A_sil, b_sil = silhouetteConstraints(A_8U)

        A_L = laplacianMatrix((h, w))
        A = 10.0 * A_c + A_L + A_sil
        b = 10.0 * b_c + b_sil

        N_32F = amg_solver.solve(A, b).reshape(h, w, 3)
        N_32F = normalizeImage(N_32F)

        return N_32F
def sparseHistogramSampling(image, num_color_bin=24, num_xy_bins=64):
    h, w = image.shape[:2]

    Lab = LabFeatures(image)
    xy = positionFeatures(image)

    Labxy = np.concatenate((Lab, xy), axis=1)

    num_bins = [
        num_color_bin, num_color_bin, num_color_bin, num_xy_bins, num_xy_bins
    ]
    num_color_bins = num_bins[:]
    num_color_bins.append(3)

    hist_bins = np.zeros(num_bins, dtype=np.float32)
    color_bins = np.zeros(num_color_bins, dtype=np.float32)

    f_min = np.min(Labxy, axis=0)
    f_max = np.max(Labxy, axis=0)

    num_bins = np.array(num_bins)
    f_ids = (num_bins - 1) * (Labxy - f_min) / (f_max - f_min)
    f_ids = np.int32(f_ids)

    hist_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3],
              f_ids[:, 4]] += 1
    color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3],
               f_ids[:, 4]] += Lab[:, :]

    hist_positive = hist_bins > 0.0

    print np.count_nonzero(hist_positive)

    for ci in xrange(3):
        color_bins[hist_positive, ci] /= hist_bins[hist_positive]

    map_Lab = color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3],
                         f_ids[:, 4]].reshape(h, w, -1)
    map_rgb = to8U(Lab2rgb(map_Lab))

    print image.shape
    print map_rgb.shape

    map_image = setAlpha(map_rgb, alpha(image))

    return map_image
    def _runLayer(self, layer_file):
        if layer_file is None:
            return
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            C0_8U = loadRGB(layer_file)

        if C0_8U is None:
            return

        h, w = C0_8U.shape[:2]
        w_low = 1024
        h_low = w_low * h / w

        #  C0_8U = cv2.resize(C0_8U, (w_low, h_low))

        A_8U = alpha(C0_8U)
        self._A_8U = A_8U

        C0_32F = to32F(rgb(C0_8U))

        if A_8U is not None:
            C0_32F[A_8U < 0.9 * np.max(A_8U), :] = np.array([0, 0, 0])

        self._C0_32F = C0_32F

        self._loadImage()
        self._computeBaseDetalSeparation()
        self._computeInitialDetailNormal()
        self.computeDetailNormal()
        self._computeLumoNormal()
        self.computeInitialNormal()

        # plt.savefig(self.characterResultFile("BumpNormal.png"))

        if self._N_b_smooth is not None:
            self.cleanCharacterResultDir()
            saveNormal(self.characterResultFile("N_b.png"), self._N_b, A_8U)
            saveNormal(self.characterResultFile("N_b_smooth.png"), self._N_b_smooth, A_8U)
            saveNormal(self.characterResultFile("N_d.png"), self._N_d, A_8U)
            saveNormal(self.characterResultFile("N_d_smooth.png"), self._N_d_smooth, A_8U)
            saveNormal(self.characterResultFile("N_lumo.png"), self._N_lumo, A_8U)
            saveNormal(self.characterResultFile("N0_b.png"), self._N0_b_32F, A_8U)
            saveNormal(self.characterResultFile("N0_d.png"), self._N0_d_32F, A_8U)
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        C0_32F = to32F(rgb(C0_8U))
        I_32F = luminance(C0_32F)

        N0_32F, A_8U = loadNormal(self.characterResultFile("N0_d.png", data_name="BaseDetailSepration"))
        Nd_32F, A_8U = loadNormal(self.characterResultFile("N_d_smooth.png", data_name="BaseDetailSepration"))
        Nb_32F, A_8U = loadNormal(self.characterResultFile("N_b_smooth.png", data_name="BaseDetailSepration"))

        W_32F = np.array(Nb_32F[:, :, 2])
        W_32F = W_32F
        W_32F[W_32F < 0.95] = 0.0

        L = lightEstimation(I_32F, N0_32F, A_8U)
        # L = lightEstimationByVoting(I_32F, N0_32F, A_8U)

        L_txt = 0.01 * np.int32(100 * L)

        L_img = lightSphere(L)

        fig, axes = plt.subplots(figsize=(11, 5))
        font_size = 15
        fig.subplots_adjust(left=0.05, right=0.95, top=0.9, hspace=0.12, wspace=0.05)
        fig.suptitle(self.name(), fontsize=font_size)

        num_rows = 1
        num_cols = 4
        plot_grid = SubplotGrid(num_rows, num_cols)

        plot_grid.showImage(C0_8U, r'$C$')
        plot_grid.showImage(normalToColor(N0_32F, A_8U), r'$N$')
        plot_grid.showImage(setAlpha(C0_32F, W_32F), r'$Nd_z$')
        plot_grid.showImage(L_img, r'$L: [%s, %s, %s]$' %(L_txt[0], L_txt[1], L_txt[2]))

        showMaximize()
def sparseHistogramSampling(image, num_color_bin=24, num_xy_bins=64):
    h, w = image.shape[:2]

    Lab = LabFeatures(image)
    xy = positionFeatures(image)

    Labxy = np.concatenate((Lab, xy), axis=1)

    num_bins = [num_color_bin, num_color_bin, num_color_bin, num_xy_bins, num_xy_bins]
    num_color_bins = num_bins[:]
    num_color_bins.append(3)

    hist_bins = np.zeros(num_bins, dtype=np.float32)
    color_bins = np.zeros(num_color_bins, dtype=np.float32)

    f_min = np.min(Labxy, axis=0)
    f_max = np.max(Labxy, axis=0)

    num_bins = np.array(num_bins)
    f_ids = (num_bins - 1) * (Labxy - f_min) / (f_max - f_min)
    f_ids = np.int32(f_ids)

    hist_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]] += 1
    color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]] += Lab[:, :]

    hist_positive = hist_bins > 0.0

    print np.count_nonzero(hist_positive)

    for ci in xrange(3):
        color_bins[hist_positive, ci] /= hist_bins[hist_positive]

    map_Lab = color_bins[f_ids[:, 0], f_ids[:, 1], f_ids[:, 2], f_ids[:, 3], f_ids[:, 4]].reshape(h, w, -1)
    map_rgb = to8U(Lab2rgb(map_Lab))

    print image.shape
    print map_rgb.shape

    map_image = setAlpha(map_rgb, alpha(image))

    return map_image
Пример #23
0
    def _runImp(self):
        D_32F = self._scene.depth()

        if D_32F is None:
            return

        image = self._scene.image()
        A_8U = alpha(image)

        if A_8U is None:
            return

        self._view = GLView()
        self._view.setRGBAD(image, D_32F)
        self._view.show()
#         A_8U = cv2.resize(A_8U, None, fx=0.25, fy=0.25)
#         N0_32F, N_32F = estimateNormal(A_8U)
#
#         h, w = image.shape[:2]
#         N_32F = cv2.resize(N_32F, (w, h))
#         self._scene.setNormal(N_32F)
#         self._scene.setDisplayMode(Scene.DisplayNormal)
Пример #24
0
    def _interpolateNormal(self):
        if self._normal_constraints.empty():
            return

        if self._image is None:
            return

        ps = np.int32(self._normal_constraints.positions())
        ns = self._normal_constraints.normals()

        h, w = self._image.shape[:2]
        N0_32F = np.zeros((h, w, 3))
        N0_32F[ps[:, 1], ps[:, 0]] = ns
        W_32F = np.zeros((h, w))
        W_32F[ps[:, 1], ps[:, 0]] = 1.0

        A_8U = None
        if self._image.shape[2] == 4:
            A_8U = to8U(alpha(self._image))

        self._N_32F = self._interpolateNormalImage(N0_32F, W_32F, A_8U)
        self._projectConstraints()
def slicSampling(image, num_segments=4000, sigma=5):
    h, w = image.shape[:2]
    C_32F = to32F(rgb(image))
    segments = slic(C_32F, n_segments=num_segments, sigma=sigma)
    print segments.shape
    print np.max(segments)
    num_centers = np.max(segments) + 1
    hist_centers = np.zeros((num_centers), dtype=np.float32)
    centers = np.zeros((num_centers, 3), dtype=np.float32)

    hist_centers[segments[:, :]] += 1.0
    centers[segments[:, :], :] += C_32F[:, :, :3]

    hist_positive = hist_centers > 0.0

    print np.count_nonzero(hist_positive)

    for ci in xrange(3):
        centers[hist_positive, ci] /= hist_centers[hist_positive]

    map_rgb = to8U(centers[segments[:, :], :].reshape(h, w, -1))
    #map_rgb = to8U(mark_boundaries(C_32F, segments))
    map_image = setAlpha(map_rgb, alpha(image))
    return map_image
def slicSampling(image, num_segments=4000, sigma=5):
    h, w = image.shape[:2]
    C_32F = to32F(rgb(image))
    segments = slic(C_32F, n_segments=num_segments, sigma=sigma)
    print segments.shape
    print np.max(segments)
    num_centers = np.max(segments) + 1
    hist_centers = np.zeros((num_centers), dtype=np.float32)
    centers = np.zeros((num_centers, 3), dtype=np.float32)

    hist_centers[segments[:, :]] += 1.0
    centers[segments[:, :], :] += C_32F[:, :, :3]

    hist_positive = hist_centers > 0.0

    print np.count_nonzero(hist_positive)

    for ci in xrange(3):
        centers[hist_positive, ci] /= hist_centers[hist_positive]

    map_rgb = to8U(centers[segments[:, :], :].reshape(h, w, -1))
    # map_rgb = to8U(mark_boundaries(C_32F, segments))
    map_image = setAlpha(map_rgb, alpha(image))
    return map_image
Пример #27
0
def alphaFeatures(image):
    h, w = image.shape[:2]
    if alpha(image) == None:
        return 255 * np.ones((h * w))
    features = alpha(image).reshape(h * w)
    return features
Пример #28
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        A_32F = to32F(A_8U)

        C0_32F = to32F(rgb(C0_8U))
        I0_32F = luminance(C0_32F)

        initial_normals = ["N_lumo.png", "N0_d.png"]

        layer_name = os.path.splitext(os.path.basename(layer_file))[0]

        for initial_normal in initial_normals:
            N0_32F, AN_8U = loadNormal(
                self.characterResultFile(initial_normal,
                                         data_name="BaseDetailSepration"))
            N_32F, L, C_32F, M = self._runSFS(C0_32F, A_8U, N0_32F, AN_8U)

            L_img = lightSphere(L)

            M_img = M.mapImage()

            fig, axes = plt.subplots(figsize=(11, 5))
            font_size = 15
            fig.subplots_adjust(left=0.02,
                                right=0.98,
                                top=0.9,
                                hspace=0.12,
                                wspace=0.02)
            fig.suptitle(self.name(), fontsize=font_size)

            num_rows = 1
            num_cols = 4
            plot_grid = SubplotGrid(num_rows, num_cols)

            plot_grid.showImage(normalToColor(N0_32F, A_8U),
                                r'Initial Normal: $N_0$')
            plot_grid.showImage(normalToColor(N_32F, A_8U),
                                r'Estimated Normal: $N$')
            plot_grid.showImage(C0_8U, r'Shading: $C_0$')
            plot_grid.showImage(setAlpha(C_32F, A_32F),
                                r'Recovered Shading: $C$')

            out_file_path = self.characterResultFile("ToonSFS" +
                                                     initial_normal,
                                                     layer_name=layer_name)
            plt.savefig(out_file_path)

            N_trim = trim(N_32F, A_8U)
            N0_trim = trim(N0_32F, A_8U)
            C0_trim = trim(C0_32F, A_8U)
            A_trim = trim(A_8U, A_8U)

            out_file_path = self.characterResultFile(initial_normal,
                                                     layer_name=layer_name)
            saveNormal(out_file_path, N_trim, A_trim)

            images = self._relightingImages(N_trim, A_trim, M)

            initial_normal_name = os.path.splitext(initial_normal)[0]
            video_name = "Relighting" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name,
                                                     layer_name=layer_name)
            saveVideo(out_file_path, images)

            images = self._relightingOffsetImages(L, C0_trim, N0_trim, A_trim,
                                                  M)
            video_name = "RelightingOffset" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name,
                                                     layer_name=layer_name)
            saveVideo(out_file_path, images)
Пример #29
0
def alphaFeatures(image):
    h, w = image.shape[:2]
    if alpha(image) == None:
        return 255 * np.ones((h * w))
    features = alpha(image).reshape(h * w)
    return features
Пример #30
0
    def _runLayer(self, layer_file):
        C0_8U = loadRGBA(layer_file)

        if C0_8U is None:
            return

        A_8U = alpha(C0_8U)

        if A_8U is None:
            return

        A_32F = to32F(A_8U)

        C0_32F = to32F(rgb(C0_8U))
        I0_32F = luminance(C0_32F)

        initial_normals = ["N_lumo.png", "N0_d.png"]

        layer_name = os.path.splitext(os.path.basename(layer_file))[0]

        for initial_normal in initial_normals:
            N0_32F, AN_8U = loadNormal(self.characterResultFile(initial_normal, data_name="BaseDetailSepration"))
            N_32F, L, C_32F, M = self._runSFS(C0_32F, A_8U, N0_32F, AN_8U)

            L_img = lightSphere(L)

            M_img = M.mapImage()

            fig, axes = plt.subplots(figsize=(11, 5))
            font_size = 15
            fig.subplots_adjust(left=0.02, right=0.98, top=0.9, hspace=0.12, wspace=0.02)
            fig.suptitle(self.name(), fontsize=font_size)

            num_rows = 1
            num_cols = 4
            plot_grid = SubplotGrid(num_rows, num_cols)

            plot_grid.showImage(normalToColor(N0_32F, A_8U), r'Initial Normal: $N_0$')
            plot_grid.showImage(normalToColor(N_32F, A_8U), r'Estimated Normal: $N$')
            plot_grid.showImage(C0_8U, r'Shading: $C_0$')
            plot_grid.showImage(setAlpha(C_32F, A_32F), r'Recovered Shading: $C$')

            out_file_path = self.characterResultFile("ToonSFS" + initial_normal, layer_name=layer_name)
            plt.savefig(out_file_path)

            N_trim = trim(N_32F, A_8U)
            N0_trim = trim(N0_32F, A_8U)
            C0_trim = trim(C0_32F, A_8U)
            A_trim = trim(A_8U, A_8U)


            out_file_path = self.characterResultFile(initial_normal, layer_name=layer_name)
            saveNormal(out_file_path, N_trim, A_trim)

            images = self._relightingImages(N_trim, A_trim, M)

            initial_normal_name = os.path.splitext(initial_normal)[0]
            video_name = "Relighting" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name, layer_name=layer_name)
            saveVideo(out_file_path, images)

            images = self._relightingOffsetImages(L, C0_trim, N0_trim, A_trim, M)
            video_name = "RelightingOffset" + initial_normal_name + ".wmv"
            out_file_path = self.characterResultFile(video_name, layer_name=layer_name)
            saveVideo(out_file_path, images)
Пример #31
0
 def normalColor(self):
     A_8U = alpha(self._image)
     return normalToColor(self._normal, A_8U)