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"
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)
to think of this is if you played the card matching game, the cards would pretty much have to be identical. The template method doesn't allow much for the scale to change, nor for rotation. This is the most basic pattern matching SimpleCV offers. If you are looking for something more complex you will probably want to look into img.find() """ print __doc__ import time from simplecv.api import Image, Color, TemplateMatch source = Image("templatetest.png", sample=True) # the image to search template = Image("template.png", sample=True) # the template to search the image for t = 5 methods = [ "SQR_DIFF", "SQR_DIFF_NORM", "CCOEFF", "CCOEFF_NORM", "CCORR", "CCORR_NORM" ] # the various types of template matching available for m in methods: img = Image("templatetest.png", sample=True) img.dl().text("current method: {}".format(m), (10, 20), color=Color.RED) fs = source.find(TemplateMatch, template, threshold=t, method=m) for match in fs: img.dl().rectangle((match.x, match.y), (match.width, match.height), color=Color.RED) img.apply_layers().show() time.sleep(3)
from simplecv.api import Color, Image from simplecv.core.drawing.layer import DrawingLayer 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)
# write the text layer.text("Just some innocent looking dots", (50, 25), Color.BLACK) layer.text("Use w/s keys to change intensity", (50, 50), Color.BLACK) layer.text("a/d keys to change angle", (50, 75), Color.BLACK) #draw 6 innocent looking dots layer.circle((125, 200), 25, Color.RED, 1, True) layer.circle((250, 200), 25, Color.BLUE, 1, True) layer.circle((375, 200), 25, Color.GREEN, 1, True) layer.circle((125, 300), 25, Color.YELLOW, 1, True) layer.circle((250, 300), 25, Color.ORANGE, 1, True) layer.circle((375, 300), 25, Color.CYAN, 1, True) # apply layer img.add_drawing_layer(layer) img = img.apply_layers() img.show() power = 1 angle = 0 while True: key = cv2.waitKey(1) if key == -1: continue print chr(key) # detect w,a,s,d key presses and modify power, angle if key == ord('w'): power += 10 blur = img.motion_blur2(power, angle) blur.show()
""" This example uses the built in template matching. The easiest way to think of this is if you played the card matching game, the cards would pretty much have to be identical. The template method doesn't allow much for the scale to change, nor for rotation. This is the most basic pattern matching SimpleCV offers. If you are looking for something more complex you will probably want to look into img.find() """ print __doc__ import time from simplecv.api import Image, Color, TemplateMatch source = Image("templatetest.png", sample=True) # the image to search template = Image("template.png", sample=True) # the template to search the image for t = 5 methods = ["SQR_DIFF", "SQR_DIFF_NORM", "CCOEFF", "CCOEFF_NORM", "CCORR", "CCORR_NORM"] # the various types of template matching available for m in methods: img = Image("templatetest.png", sample=True) img.dl().text("current method: {}".format(m), (10, 20), color=Color.RED) fs = source.find(TemplateMatch, template, threshold=t, method=m) for match in fs: img.dl().rectangle((match.x, match.y), (match.width, match.height), color=Color.RED) img.apply_layers().show() time.sleep(3)