Пример #1
0
class EdgeSnapWindow(Window):
    def __init__(self):
        super(EdgeSnapWindow, self).__init__('Color Segmentation Example')
        self.image = Image("shapes.png", sample=True).edges()
        self.points = []

        self.show(self.image)

    def on_mouse(self, event, x, y, mouse_key, data=None):
        """ Callback for mouse events

            event - int - see cv2.EVENT_* constants
            x, y - int, int - position of the cursor
            mouse_key - int - mouse key
        """
        if event == cv2.EVENT_LBUTTONDOWN:
            self.points.append((x, y))

            self.image.clear_layers()
            for p in self.points:
                self.image.dl().circle(p, 5, color=Color.BLUE)

            features = self.image.edge_snap(self.points)

            for f in features:
                f.draw(color=Color.RED, width=2)

            self.show(self.image.apply_layers())

    def on_key(self, key):
        if key == 32:  # Space bar to clear points
            self.points = []
            self.image.clear_layers()
            self.show(self.image)
            print "Points cleared"
Пример #2
0
class ColorSegmentationWindow(Window):

    def __init__(self):
        super(ColorSegmentationWindow, self).__init__('Color Segmentation Example')
        self.img = Image('simplecv')
        self.segmentation = ColorSegmentation()
        self.normal = True  # mode toggle for segment detection
        self.point1 = (0, 0)
        self.point2 = (0, 0)
        self.mosue_down = False

    def on_mouse(self, event, x, y, mouse_key, data=None):
        """ Callback for mouse events

            event - int - see cv2.EVENT_* constants
            x, y - int, int - position of the cursor
            mouse_key - int - mouse key
        """
        if event == cv2.EVENT_LBUTTONDOWN:
            self.point1 = (x, y)
            self.point2 = (x, y)
            self.mosue_down = True
        elif event == cv2.EVENT_MOUSEMOVE:
            if self.mosue_down == True:
                self.point2 = (x, y)
        elif event == cv2.EVENT_LBUTTONUP:
            self.mosue_down = False
            self.point2 = (x, y)

            x1, y1, w, h = points_to_roi(self.point1, self.point2)
            if w > 0 and h > 0:
                crop = self.img.crop(x1, y1, w, h)
                self.segmentation = ColorSegmentation()
                self.segmentation.add_to_model(crop)

    def on_key(self, key):
        if key == 32:  # Space bar to switch between modes
            self.normal = not self.normal
            print "Display Mode:", "Normal" if self.normal else "Segmented"

    def on_update(self):
        """ Callback for periodic update.
        """
        if self.normal:
            self.img.clear_layers()
            self.img.dl().rectangle_to_pts(self.point1, self.point2, color=Color.RED)
            self.show(self.img.apply_layers())
        else:
            self.segmentation.add_image(self.img)
            if self.segmentation.is_ready():
                img = self.segmentation.get_segmented_image()
                img = img.erode(iterations = 2).dilate().invert()
                self.show(img)
Пример #3
0
class EdgeSnapWindow(Window):

    def __init__(self):
        super(EdgeSnapWindow, self).__init__('Color Segmentation Example')
        self.image = Image("shapes.png", sample=True).edges()
        self.points = []

        self.show(self.image)

    def on_mouse(self, event, x, y, mouse_key, data=None):
        """ Callback for mouse events

            event - int - see cv2.EVENT_* constants
            x, y - int, int - position of the cursor
            mouse_key - int - mouse key
        """
        if event == cv2.EVENT_LBUTTONDOWN:
            self.points.append((x, y))

            self.image.clear_layers()
            for p in self.points:
                self.image.dl().circle(p, 5, color=Color.BLUE)

            features = self.image.edge_snap(self.points)

            for f in features:
                f.draw(color=Color.RED, width=2)

            self.show(self.image.apply_layers())

    def on_key(self, key):
        if key == 32:  # Space bar to clear points
            self.points = []
            self.image.clear_layers()
            self.show(self.image)
            print "Points cleared"
img = Image("color.jpg", sample=True)
line_l = DrawingLayer()
a = (20, 20)
b = (20, 100)
c = (100, 100)
d = (100, 20)
line_l.line(a, b, alpha=128, width=5)
line_l.line(b, c, alpha=128)
line_l.line(c, d, antialias=True)
line_l.line(d, a, color=Color.PUCE)
line_l.line(b, d, width=5)
img.add_drawing_layer(line_l)
temp = img.apply_layers()
print "line: %s" % temp.save(temp=True)
img.clear_layers()

lines_l = DrawingLayer()
a = (20, 20)
b = (20, 100)
c = (100, 100)
d = (100, 20)
pts = (a, b, c, d, a)
lines_l.lines(pts, alpha=128)
# translate over and down 10
pts = map(lambda x: ((x[0] + 10), (x[1] + 10)), pts)
lines_l.lines(pts, color=Color.BEIGE, width=10)
#translate over and down 10
pts = map(lambda x: ((x[0] + 10), (x[1] + 10)), pts)
lines_l.lines(pts, antialias=True)
img.add_drawing_layer(lines_l)