Пример #1
0
    def __init__(self):
        super(StrokeTool, self).__init__()
        self._stroke_sets = StrokeSets()
        self._stroke_sets.addStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

        self._stroke_edited_func = None
        self._stroke_updated_func = None
        self._adjusting_brush = False
        self._p_old = None
        self._brush_size = 20
Пример #2
0
    def __init__(self):
        super(Scene, self).__init__()

        self._image = None
        self._image_file = ""
        self._normal = None
        self._depth = None
        self._layer_set = LayerSet()
        self._stroke_sets = StrokeSets()
        self._selection = None
        self._display_mode = self.DisplayImage
Пример #3
0
    def __init__(self):
        super(StrokeTool, self).__init__()
        self._stroke_sets = StrokeSets()
        self._stroke_sets.addStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

        self._stroke_edited_func = None
        self._stroke_updated_func = None
        self._adjusting_brush = False
        self._p_old = None
        self._brush_size = 20
Пример #4
0
class StrokeTool(BaseTool):
    ## Constructor
    def __init__(self):
        super(StrokeTool, self).__init__()
        self._stroke_sets = StrokeSets()
        self._stroke_sets.addStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

        self._stroke_edited_func = None
        self._stroke_updated_func = None
        self._adjusting_brush = False
        self._p_old = None
        self._brush_size = 20

    def clearStrokeSets(self):
        self._stroke_sets.clear()
        self._selectStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

    def setStrokeSets(self, stroke_sets):
        self._stroke_sets = stroke_sets
        self._selectStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

    def setStrokeEditedCallBack(self, stroke_edited_func):
        self._stroke_edited_func = stroke_edited_func

    def setStrokeUpdatedCallBack(self, stroke_updated_func):
        self._stroke_updated_func = stroke_updated_func

    def setView(self, view):
        super(StrokeTool, self).setView(view)

    def mousePressEvent(self, e):
        self._p_old = self._mousePosition(e)
        if self._adjusting_brush:
            return

        if e.buttons() & Qt.LeftButton:
            if (e.modifiers() & Qt.ShiftModifier):
                self._stroke_sets.selectedStrokeSet().addEmptyStroke()

            else:
                self._stroke_sets.selectedStrokeSet().clear(with_empty=True)

            self.addStrokePoint(e)

    def mouseReleaseEvent(self, e):
        pass

    def mouseMoveEvent(self, e):
        if e.buttons() & Qt.LeftButton:
            if self._adjusting_brush:
                self.adjustBrush(e)
                return

            if self._addingStrokePoint(e):
                self.addStrokePoint(e)

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_B:
            self._adjusting_brush = True

        if e.key() == Qt.Key_0:
            self._selectStrokeSet("0", (0.0, 0.0, 1.0, 0.4))

        if e.key() == Qt.Key_1:
            self._selectStrokeSet("1", (1.0, 0.0, 0.0, 0.4))

        if e.key() == Qt.Key_2:
            self._selectStrokeSet("2", (0.0, 1.0, 0.0, 0.4))

        if e.key() == Qt.Key_3:
            self._selectStrokeSet("3", (1.0, 1.0, 0.0, 0.4))

        if e.key() == Qt.Key_4:
            self._selectStrokeSet("4", (1.0, 0.0, 1.0, 0.4))

        if e.key() == Qt.Key_5:
            self._selectStrokeSet("5", (0.0, 1.0, 1.0, 0.4))

        if e.key() == Qt.Key_Enter or e.key() == Qt.Key_Return:
            print "Press Enter"
            if self._stroke_edited_func:
                self._stroke_edited_func(self._stroke_sets)

    def keyReleaseEvent(self, e):
        self._adjusting_brush = False

    def adjustBrush(self, e):
        p = self._mousePosition(e)
        dx = p[0] - self._p_old[0]

        self._brush_size += dx
        self._brush_size = max(3, self._brush_size)

        self._p_old = p

        self._view.update()

    def addStrokePoint(self, e):
        p = self._mousePosition(e)
        self._stroke_sets.selectedStrokeSet().lastStroke().addStrokePoint(
            p, self._brush_size)

        self._view.update()

        if self._stroke_updated_func:
            self._stroke_updated_func(self._stroke_sets)

    def _addingStrokePoint(self, e):
        p = self._mousePosition(e)
        if self._stroke_sets.selectedStrokeSet().lastStroke().empty() == 0:
            return True

        p_old = self._stroke_sets.selectedStrokeSet().lastStroke().points()[-1]

        if np.linalg.norm(p - p_old) > 3:
            return True

        return False

    def _overlayFunc(self, painter):
        pen = QPen(QColor(255, 0, 0, 100))
        pen.setWidth(self._brush_size)
        pen.setCapStyle(Qt.RoundCap)
        painter.setPen(pen)

        p = self._p_old

        if p is not None:
            painter.drawPoint(QPoint(p[0], p[1]))

        for stroke_set in self._stroke_sets.strokeSets():
            color = stroke_set.color()
            for stroke in stroke_set.strokes():
                brush_size = stroke.brushSize()

                pen = QPen(
                    QColor.fromRgbF(color[0], color[1], color[2], color[3]))
                pen.setWidth(brush_size)

                painter.setPen(pen)
                polyline = QPolygon(
                    [QPoint(p[0], p[1]) for p in stroke.points()])
                painter.drawPolyline(polyline)

    def _selectStrokeSet(self, name, color):
        if self._stroke_sets.selectStrokeSet(name):
            return
        self._stroke_sets.addStrokeSet(name, color)
Пример #5
0
class Scene(QObject, Data):
    updatedImage = pyqtSignal(object)
    updatedDepth = pyqtSignal(object, object)
    updatedMessage = pyqtSignal(str)

    DisplayImage = 0
    DisplayNormal = 1
    DisplayDepth = 2

    ## Constructor
    def __init__(self):
        super(Scene, self).__init__()

        self._image = None
        self._image_file = ""
        self._normal = None
        self._depth = None
        self._layer_set = LayerSet()
        self._stroke_sets = StrokeSets()
        self._selection = None
        self._display_mode = self.DisplayImage

    def setDisplayMode(self, mode):
        if self._image is None:
            return

        self._display_mode = mode

        if self._display_mode == self.DisplayImage:
            self.updatedImage.emit(self._image)

        if self._display_mode == self.DisplayNormal:
            if self._normal is None:
                return
            normal_image = self.normalColor()
            self.updatedImage.emit(normal_image)

        if self._display_mode == self.DisplayDepth:
            if self._depth is None:
                return
            depth_image = self.depthImage()
            self.updatedImage.emit(depth_image)

    def displayMode(self):
        return self._display_mode

    def setImageFile(self, image_file):
        self._image_file = image_file
        image = loadRGBA(self._image_file)
        self.setImage(image)
        self._normal = None
        self._depth = None
        self.updatedDepth.emit(self._image, self._depth)

    def setImage(self, image):
        self._image = image
        self.updatedImage.emit(image)

    def image(self):
        return self._image

    def setNormal(self, normal):
        self._normal = normal

    def normal(self):
        return self._normal

    def normalColor(self):
        A_8U = alpha(self._image)
        return normalToColor(self._normal, A_8U)

    def setDepth(self, depth):
        self._depth = depth
        self.updatedDepth.emit(self._image, self._depth)

    def depth(self):
        return self._depth

    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

    def strokeSets(self):
        return self._stroke_sets

    def layerSet(self):
        return self._layer_set

    def setSelection(self, selection):
        self._selection = selection

    def Selection(self):
        return self._selection

    def setMessage(self, message):
        self.updatedMessage.emit(message)

    ## dictionary data for writeJson method.
    def _dataDict(self):
        data = {"image_file": self._image_file}
        data["stroke"] = self._stroke_sets._dataDict()
        return data

    ## set dictionary data for loadJson method.
    def _setDataDict(self, data):
        self.setImageFile(data["image_file"])
        self._stroke_sets._setDataDict(data["stroke"])
Пример #6
0
 def __init__(self, view, tool, name="Segmentation", dataset_name="3dmodel"):
     super(SegmentationBatch, self).__init__(name, dataset_name)
     self._view = view
     self._tool = tool
     self._stroke_sets = StrokeSets()
     self._tool.setStrokeSets(self._stroke_sets)
Пример #7
0
class SegmentationBatch(CharacterBatch):
    def __init__(self, view, tool, name="Segmentation", dataset_name="3dmodel"):
        super(SegmentationBatch, self).__init__(name, dataset_name)
        self._view = view
        self._tool = tool
        self._stroke_sets = StrokeSets()
        self._tool.setStrokeSets(self._stroke_sets)

    def _runCharacterImp(self):
        self._runLayer(self.fullLayerFile())

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

        if C0_8U is None:
            return

        if os.path.exists(self._segmentationFile()):
            self._stroke_sets.load(self._segmentationFile())
            self._tool.setStrokeSets(self._stroke_sets)
            self._view.update()
        else:
            self._tool.clearStrokeSets()

        self._computeSegmentaiton(C0_8U)
        #self._runUI(C0_8U)

    def _runUI(self, C0_8U):
        self._view.render(C0_8U)

    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()

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

    def _segmentationFile(self):
        return self.characterResultFile("segmentation.json")
Пример #8
0
class StrokeTool(BaseTool):
    ## Constructor
    def __init__(self):
        super(StrokeTool, self).__init__()
        self._stroke_sets = StrokeSets()
        self._stroke_sets.addStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

        self._stroke_edited_func = None
        self._stroke_updated_func = None
        self._adjusting_brush = False
        self._p_old = None
        self._brush_size = 20

    def clearStrokeSets(self):
        self._stroke_sets.clear()
        self._selectStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

    def setStrokeSets(self, stroke_sets):
        self._stroke_sets = stroke_sets
        self._selectStrokeSet("1", color=(1.0, 0.0, 0.0, 0.4))

    def setStrokeEditedCallBack(self, stroke_edited_func):
        self._stroke_edited_func = stroke_edited_func

    def setStrokeUpdatedCallBack(self, stroke_updated_func):
        self._stroke_updated_func = stroke_updated_func

    def setView(self, view):
        super(StrokeTool, self).setView(view)

    def mousePressEvent(self, e):
        self._p_old = self._mousePosition(e)
        if self._adjusting_brush:
            return

        if e.buttons() & Qt.LeftButton:
            if (e.modifiers() & Qt.ShiftModifier):
                self._stroke_sets.selectedStrokeSet().addEmptyStroke()

            else:
                self._stroke_sets.selectedStrokeSet().clear(with_empty=True)

            self.addStrokePoint(e)

    def mouseReleaseEvent(self, e):
        pass

    def mouseMoveEvent(self, e):
        if e.buttons() & Qt.LeftButton:
            if self._adjusting_brush:
                self.adjustBrush(e)
                return

            if self._addingStrokePoint(e):
                self.addStrokePoint(e)

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_B:
            self._adjusting_brush = True

        if e.key() == Qt.Key_0:
            self._selectStrokeSet("0", (0.0, 0.0, 1.0, 0.4))

        if e.key() == Qt.Key_1:
            self._selectStrokeSet("1", (1.0, 0.0, 0.0, 0.4))

        if e.key() == Qt.Key_2:
            self._selectStrokeSet("2", (0.0, 1.0, 0.0, 0.4))

        if e.key() == Qt.Key_3:
            self._selectStrokeSet("3", (1.0, 1.0, 0.0, 0.4))

        if e.key() == Qt.Key_4:
            self._selectStrokeSet("4", (1.0, 0.0, 1.0, 0.4))

        if e.key() == Qt.Key_5:
            self._selectStrokeSet("5", (0.0, 1.0, 1.0, 0.4))

        if e.key() == Qt.Key_Enter or e.key() == Qt.Key_Return:
            print "Press Enter"
            if self._stroke_edited_func:
                self._stroke_edited_func(self._stroke_sets)

    def keyReleaseEvent(self, e):
        self._adjusting_brush = False

    def adjustBrush(self, e):
        p = self._mousePosition(e)
        dx = p[0] - self._p_old[0]

        self._brush_size += dx
        self._brush_size = max(3, self._brush_size)

        self._p_old = p

        self._view.update()

    def addStrokePoint(self, e):
        p = self._mousePosition(e)
        self._stroke_sets.selectedStrokeSet().lastStroke().addStrokePoint(p, self._brush_size)

        self._view.update()

        if self._stroke_updated_func:
            self._stroke_updated_func(self._stroke_sets)

    def _addingStrokePoint(self, e):
        p = self._mousePosition(e)
        if self._stroke_sets.selectedStrokeSet().lastStroke().empty() == 0:
            return True

        p_old = self._stroke_sets.selectedStrokeSet().lastStroke().points()[-1]

        if np.linalg.norm(p - p_old) > 3:
            return True

        return False

    def _overlayFunc(self, painter):
        pen = QPen(QColor(255, 0, 0, 100))
        pen.setWidth(self._brush_size)
        pen.setCapStyle(Qt.RoundCap)
        painter.setPen(pen)

        p = self._p_old

        if p is not None:
            painter.drawPoint(QPoint(p[0], p[1]))

        for stroke_set in self._stroke_sets.strokeSets():
            color = stroke_set.color()
            for stroke in stroke_set.strokes():
                brush_size = stroke.brushSize()

                pen = QPen(QColor.fromRgbF(color[0], color[1], color[2], color[3]))
                pen.setWidth(brush_size)

                painter.setPen(pen)
                polyline = QPolygon([QPoint(p[0], p[1]) for p in stroke.points()])
                painter.drawPolyline(polyline)

    def _selectStrokeSet(self, name, color):
        if self._stroke_sets.selectStrokeSet(name):
            return
        self._stroke_sets.addStrokeSet(name, color)