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()
Example #2
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()
Example #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()
Example #4
0
 def _runImp(self):
     image = self._scene.image()
     I_32F = to32F(rgb2gray(rgb(image)))
     N_32F, D_32F = estimateNormal(I_32F)
     self._scene.setNormal(N_32F)
     self._scene.setDepth(D_32F)
     self._scene.setDisplayMode(Scene.DisplayNormal)
    def _strokeEdited(self, stroke_sets):

        image = self._scene.image()

        C_32F = to32F(rgb(image))

        for stroke_set in stroke_sets.strokeSets():
            for stroke in stroke_set.strokes():
                if stroke.empty():
                    continue

                mask = np.zeros(image.shape[:2], dtype=np.uint8)
                points = stroke.points()
                points = np.int32(points)

                brush_size = int(stroke.brushSize())
                cv2.polylines(mask, [points], 0, 255, brush_size)

                Cs = C_32F[mask > 0, :]
                Is = np.arange(len(Cs), dtype=np.float32)

                Is = np.array(Is)
                Cs = np.array(Cs)
                M = ColorMapEstimation(Cs, Is, num_samples=1000)

                M_img = M.mapImage(image_size=(256, 32))

                def plotFunc():
                    plt.imshow(M_img)

                self._matplot_view.drawPlots(plotFunc)

                saveImage(self._currentColorMapFile(), M_img)
                self._newColorMapID()
    def _strokeEdited(self, stroke_sets):

        image = self._scene.image()

        C_32F = to32F(rgb(image))

        for stroke_set in stroke_sets.strokeSets():
            for stroke in stroke_set.strokes():
                if stroke.empty():
                    continue

                mask = np.zeros(image.shape[:2], dtype=np.uint8)
                points = stroke.points()
                points = np.int32(points)

                brush_size = int(stroke.brushSize())
                cv2.polylines(mask, [points], 0, 255, brush_size)

                Cs = C_32F[mask > 0, :]
                Is = np.arange(len(Cs), dtype=np.float32)

                Is = np.array(Is)
                Cs = np.array(Cs)
                M = ColorMapEstimation(Cs, Is, num_samples=1000)

                M_img = M.mapImage(image_size=(256, 32))

                def plotFunc():
                    plt.imshow(M_img)

                self._matplot_view.drawPlots(plotFunc)

                saveImage(self._currentColorMapFile(), M_img)
                self._newColorMapID()
Example #7
0
 def _runImp(self):
     image = self._scene.image()
     I_32F = to32F(rgb2gray(rgb(image)))
     N_32F, D_32F = estimateNormal(I_32F)
     self._scene.setNormal(N_32F)
     self._scene.setDepth(D_32F)
     self._scene.setDisplayMode(Scene.DisplayNormal)
Example #8
0
def slicSegmentation(image, num_segments=600, sigma=5):
    C_8U = rgb(image)
    C_32F = to32F(C_8U)
    segments = slic(C_32F, n_segments=num_segments, sigma=sigma)

    fig = plt.figure("Superpixels -- %d segments" % (num_segments))
    ax = fig.add_subplot(1, 1, 1)
    ax.imshow(mark_boundaries(C_32F, segments))
    plt.axis("off")
    plt.show()
Example #9
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()
Example #10
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
Example #11
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)
Example #12
0
def saveVideo(file_path, images, fps=30, size=None):
    if size is None:
        h, w = images[0].shape[:2]
        size = (w, h)

        print size

    fourcc = cv2.cv.CV_FOURCC('W', 'M', 'V', '2')
    writer = cv2.VideoWriter(file_path, fourcc, fps, size, True)

    for image in images:
        bgr = rgb2bgr(to8U(rgb(image)))
        writer.write(bgr)

    writer.release()
Example #13
0
def saveVideo(file_path, images, fps=30, size=None):
    if size is None:
        h, w = images[0].shape[:2]
        size = (w, h)

        print size

    fourcc = cv2.cv.CV_FOURCC('W', 'M', 'V', '2')
    writer = cv2.VideoWriter(file_path, fourcc, fps, size, True)

    for image in images:
        bgr = rgb2bgr(to8U(rgb(image)))
        writer.write(bgr)

    writer.release()
Example #14
0
    def _strokeEdited(self, stroke_sets):
        print "Graph Cut Command"
        if len(stroke_sets.strokeSets()) < 2:
            return

        fg_stroke_set, bg_stroke_set = stroke_sets.strokeSets()[:2]

        image = self._scene.image()

        image_rgb = to8U(rgb(image))

        mask = np.zeros(image.shape[:2], dtype=np.uint8)
        mask.fill(cv2.GC_PR_BGD)

        for stroke_set, color in zip(
            [fg_stroke_set, bg_stroke_set],
            [int(cv2.GC_FGD), int(cv2.GC_BGD)]):
            for stroke in stroke_set.strokes():
                if stroke.empty():
                    continue
                points = stroke.points()
                points = np.int32(points)

                brush_size = int(stroke.brushSize())

                print color
                cv2.polylines(mask, [points], 0, color, brush_size)

        bgdModel = np.zeros((1, 65), np.float64)
        fgdModel = np.zeros((1, 65), np.float64)

        cv2.grabCut(image_rgb, mask, None, bgdModel, fgdModel, 5,
                    cv2.GC_INIT_WITH_MASK)

        fg_mask = np.zeros(image.shape[:2], dtype=np.uint8)
        fg_mask[(mask == int(cv2.GC_FGD)) | (mask == int(cv2.GC_PR_FGD))] = 255

        bg_mask = np.zeros(image.shape[:2], dtype=np.uint8)
        bg_mask[(mask == int(cv2.GC_BGD)) | (mask == int(cv2.GC_PR_BGD))] = 255

        layer_set = self._scene.layerSet()
        layer_set.clear()
        layer_set.addLayer(
            Layer(name="BG", color=(0.0, 0.0, 1.0, 0.4), mask=bg_mask))
        layer_set.addLayer(
            Layer(name="FG", color=(1.0, 0.0, 0.0, 0.4), mask=fg_mask))
    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 _compute(self):
        C0_8U = self._image
        C0_32F = to32F(rgb(C0_8U))
        Lab_32F = rgb2Lab(C0_32F)
        I_32F = luminance(C0_32F)

        h, w = I_32F.shape[:2]
        edge_mask = np.zeros((h, w), dtype=np.uint8)

        # E_32F = DoG(Lab_32F, sigma=7.0)

        #         E_32F[E_32F > 0.0] = 0.0
        #         E_32F = - E_32F
        #         E_32F /= np.max(E_32F)
        #         th_contour = 0.05
        #         for ci in xrange(3):
        #             edge_area = E_32F[:, :, ci] > th_contour
        #             edge_mask[edge_area] = 255

        E_32F = DoG(I_32F, sigma=3.0)
        h, w = E_32F.shape[:2]
        E_norm = -np.array(E_32F)
        E_norm[E_norm < 0.0] = 0.0

        th_contour = 0.1
        edge_area = E_norm > th_contour * np.max(E_norm)
        edge_mask[edge_area] = 255
        self._edge_mask = edge_mask

        labels = np.array(edge_mask)

        mask = np.ones((h + 2, w + 2), dtype=np.uint8)
        mask[1:-1, 1:-1] = self._edge_mask

        for label in xrange(1, 3):
            regionSeeds = np.where(self._edge_mask == 0)
            if len(regionSeeds[0]) == 0:
                break
            p = (regionSeeds[1][0], regionSeeds[0][0])
            cv2.floodFill(labels, mask, p, label)
            self._edge_mask[labels == label] = label
        self._labels = labels
Example #18
0
    def _strokeEdited(self, stroke_sets):
        print "Graph Cut Command"
        if len(stroke_sets.strokeSets()) < 2:
            return

        fg_stroke_set, bg_stroke_set = stroke_sets.strokeSets()[:2]

        image = self._scene.image()

        image_rgb = to8U(rgb(image))

        mask = np.zeros(image.shape[:2], dtype=np.uint8)
        mask.fill(cv2.GC_PR_BGD)

        for stroke_set, color in zip([fg_stroke_set, bg_stroke_set],
                                     [int(cv2.GC_FGD), int(cv2.GC_BGD)]):
            for stroke in stroke_set.strokes():
                if stroke.empty():
                    continue
                points = stroke.points()
                points = np.int32(points)

                brush_size = int(stroke.brushSize())

                print color
                cv2.polylines(mask, [points], 0, color, brush_size)

        bgdModel = np.zeros((1,65),np.float64)
        fgdModel = np.zeros((1,65),np.float64)

        cv2.grabCut(image_rgb, mask, None, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK)

        fg_mask = np.zeros(image.shape[:2], dtype=np.uint8)
        fg_mask[(mask==int(cv2.GC_FGD)) | (mask==int(cv2.GC_PR_FGD))] = 255

        bg_mask = np.zeros(image.shape[:2], dtype=np.uint8)
        bg_mask[(mask==int(cv2.GC_BGD)) | (mask==int(cv2.GC_PR_BGD))] = 255

        layer_set = self._scene.layerSet()
        layer_set.clear()
        layer_set.addLayer(Layer(name="BG", color=(0.0, 0.0, 1.0, 0.4), mask=bg_mask))
        layer_set.addLayer(Layer(name="FG", color=(1.0, 0.0, 0.0, 0.4), mask=fg_mask))
    def _compute(self):
        C0_8U = self._image
        C0_32F = to32F(rgb(C0_8U))
        Lab_32F = rgb2Lab(C0_32F)
        I_32F = luminance(C0_32F)

        h, w = I_32F.shape[:2]
        edge_mask = np.zeros((h, w), dtype=np.uint8)

        #E_32F = DoG(Lab_32F, sigma=7.0)

        #         E_32F[E_32F > 0.0] = 0.0
        #         E_32F = - E_32F
        #         E_32F /= np.max(E_32F)
        #         th_contour = 0.05
        #         for ci in xrange(3):
        #             edge_area = E_32F[:, :, ci] > th_contour
        #             edge_mask[edge_area] = 255

        E_32F = DoG(I_32F, sigma=3.0)
        h, w = E_32F.shape[:2]
        E_norm = -np.array(E_32F)
        E_norm[E_norm < 0.0] = 0.0

        th_contour = 0.1
        edge_area = E_norm > th_contour * np.max(E_norm)
        edge_mask[edge_area] = 255
        self._edge_mask = edge_mask

        labels = np.array(edge_mask)

        mask = np.ones((h + 2, w + 2), dtype=np.uint8)
        mask[1:-1, 1:-1] = self._edge_mask

        for label in xrange(1, 3):
            regionSeeds = np.where(self._edge_mask == 0)
            if len(regionSeeds[0]) == 0:
                break
            p = (regionSeeds[1][0], regionSeeds[0][0])
            cv2.floodFill(labels, mask, p, label)
            self._edge_mask[labels == label] = label
        self._labels = labels
Example #20
0
    def _interpolateNormalImage(self, N0_32F, W_32F, A_8U):
        constraints = []
        constraints.append(image_constraints.laplacianConstraints(w_c=0.1))
        constraints.append(image_constraints.normalConstraints(W_32F, N0_32F, w_c=3.0))
        L = normalizeVector(np.array([-0.2, 0.3, 0.7]))
        I_32F = luminance(to32F(rgb(self._image)))
        I_min, I_max = np.min(I_32F), np.max(I_32F)

        I_32F = (I_32F - I_min) / (I_max - I_min)

        # constraints.append(image_constraints.brightnessConstraints(L, I_32F, w_c=0.5))
        constraints.append(image_constraints.silhouetteConstraints(A_8U, w_c=0.8))

        solver_iter = image_solver.solveIterator(constraints,
                                                 [postNormalize(th=0.0)])

        N_32F = np.array(N0_32F)
        N_32F = image_solver.solveMG(N_32F, solver_iter, iterations=10)
        N_32F = image_constraints.NxyToNz(N_32F)

        return N_32F
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
    def _computeSegmentaiton(self, C0_8U):
        C_32F = to32F(rgb(C0_8U))

        label0 = np.zeros(C_32F.shape[:3], dtype=np.uint8)

        colors = []

        for stroke_set in self._stroke_sets.strokeSets():
            print stroke_set.color()
            color = np.array(stroke_set.color())[:3]
            colors.append(color)
            color = np.int32(255 * color)

            print type(color[0])

            for stroke in stroke_set.strokes():
                if stroke.empty():
                    continue
                points = stroke.points()
                points = np.int32(points)

                brush_size = int(stroke.brushSize())

                print color
                cv2.polylines(label0, [points], 0, (color[0], color[1], color[2]), brush_size)

        colors = np.array(colors)

#         h, w = label0.shape[:2]
#
#         w_low = 512
#         h_low = w_low * h / w
#
#         gauide_filter = GuidedFilter(cv2.resize(C_32F, (w_low, h_low)), radius=11, epsilon=0.05)
#
#         label0 = cv2.resize(label0, (w_low, h_low))
#         h, w = label0.shape[:2]
#         label = np.array(label0)
#
#         dc = np.zeros((len(colors), h * w), dtype=np.float32)
#
#         for i in xrange(5):
#             label = gauide_filter.filter(label)
#             label[label0 > 0] = label0[label0 > 0]
#
#         label_flat = label.reshape(-1, 3)
#
#         for ci, color in enumerate(colors):
#             dc[ci, :] = normVectors(label_flat - color)
#
#         centers = np.argmin(dc, axis=0)
#
#         label_flat = colors[centers]
#         label = label_flat.reshape(h, w, 3)

#         kmeans = KMeans(C0_8U, num_colors=20)
#         centerImage = kmeans.centerImage()
#         self._view.render(centerImage)
#
#         histImage = kmeans.histImage()
#
#         plt.imshow(histImage)
#         plt.show()

        edgeSeg = EdgeBasedSegmentaiton(C0_8U)
        labels = edgeSeg.labels()
        plt.imshow(labels)
        plt.show()
Example #24
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)
Example #25
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)