예제 #1
0
class GraphCutCommand(BaseCommand):
    def __init__(self, scene, view, parent=None):
        super(GraphCutCommand, self).__init__(scene, "GraphCut", parent)
        self._view = view
        self._tool = StrokeTool()
        self._tool.setStrokeEditedCallBack(self._strokeEdited)

    def _runImp(self):
        stroke_sets = self._scene.strokeSets()
        self._tool.setStrokeSets(stroke_sets)
        self._view.setTool(self._tool)

    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))
예제 #2
0
class GraphCutCommand(BaseCommand):
    def __init__(self, scene, view, parent=None):
        super(GraphCutCommand, self).__init__(scene, "GraphCut", parent)
        self._view = view
        self._tool = StrokeTool()
        self._tool.setStrokeEditedCallBack(self._strokeEdited)

    def _runImp(self):
        stroke_sets = self._scene.strokeSets()
        self._tool.setStrokeSets(stroke_sets)
        self._view.setTool(self._tool)

    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))
예제 #3
0
    def __init__(self, scene, view, parent=None):
        super(ColorMapCommand, self).__init__(scene, "ColrMap", parent)
        self._view = view
        self._tool = StrokeTool()
        self._tool.setStrokeEditedCallBack(self._strokeEdited)

        self._matplot_view = MatplotFrame()
        self._matplot_view.show()

        self._cmap_id = 0
        self._newColorMapID()
        print self._cmap_id
        print self._currentColorMapFile()
예제 #4
0
    def __init__(self, scene, view, parent=None):
        super(ColorMapCommand, self).__init__(scene, "ColrMap", parent)
        self._view = view
        self._tool = StrokeTool()
        self._tool.setStrokeEditedCallBack(self._strokeEdited)

        self._matplot_view = MatplotFrame()
        self._matplot_view.show()

        self._cmap_id = 0
        self._newColorMapID()
        print self._cmap_id
        print self._currentColorMapFile()
예제 #5
0
 def __init__(self, scene, view, parent=None):
     super(StrokeToolCommand, self).__init__(scene, "&Stroke Tool", parent)
     self._view = view
     self._tool = StrokeTool()
     action = self.action()
     action.setShortcut("S")
예제 #6
0
class ColorMapCommand(BaseCommand):
    def __init__(self, scene, view, parent=None):
        super(ColorMapCommand, self).__init__(scene, "ColrMap", parent)
        self._view = view
        self._tool = StrokeTool()
        self._tool.setStrokeEditedCallBack(self._strokeEdited)

        self._matplot_view = MatplotFrame()
        self._matplot_view.show()

        self._cmap_id = 0
        self._newColorMapID()
        print self._cmap_id
        print self._currentColorMapFile()

    def _runImp(self):
        stroke_sets = self._scene.strokeSets()
        self._tool.setStrokeSets(stroke_sets)
        self._view.setTool(self._tool)

    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 _newColorMapID(self):
        while os.path.exists(colorMapFile(self._cmap_id)):
            self._cmap_id += 1

    def _currentColorMapFile(self):
        return colorMapFile(self._cmap_id)
예제 #7
0
class ColorMapCommand(BaseCommand):
    def __init__(self, scene, view, parent=None):
        super(ColorMapCommand, self).__init__(scene, "ColrMap", parent)
        self._view = view
        self._tool = StrokeTool()
        self._tool.setStrokeEditedCallBack(self._strokeEdited)

        self._matplot_view = MatplotFrame()
        self._matplot_view.show()

        self._cmap_id = 0
        self._newColorMapID()
        print self._cmap_id
        print self._currentColorMapFile()

    def _runImp(self):
        stroke_sets = self._scene.strokeSets()
        self._tool.setStrokeSets(stroke_sets)
        self._view.setTool(self._tool)

    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 _newColorMapID(self):
        while os.path.exists(colorMapFile(self._cmap_id)):
            self._cmap_id += 1

    def _currentColorMapFile(self):
        return colorMapFile(self._cmap_id)
예제 #8
0
 def __init__(self, scene, view, parent=None):
     super(GraphCutCommand, self).__init__(scene, "GraphCut", parent)
     self._view = view
     self._tool = StrokeTool()
     self._tool.setStrokeEditedCallBack(self._strokeEdited)
예제 #9
0
 def __init__(self, scene, view, parent=None):
     super(GraphCutCommand, self).__init__(scene, "GraphCut", parent)
     self._view = view
     self._tool = StrokeTool()
     self._tool.setStrokeEditedCallBack(self._strokeEdited)
예제 #10
0
#         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()

    def finishCharacter(self):
        if self._character_name != "":
            self._stroke_sets.save(self._segmentationFile())
        self.runCharacter()

    def _segmentationFile(self):
        return self.characterResultFile("segmentation.json")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = ImageView()
    view.showMaximized()
    tool = StrokeTool()
    view.setTool(tool)
    batch = SegmentationBatch(view, tool)
    view.setReturnCallback(batch.finishCharacter)
    sys.exit(app.exec_())