def process(self, filepath): img = Image(filepath) # load the image into SimpleCV # ~ img = img.edges() # Get the edges img.save(filepath) # save the temporary image self.imageset.append(img.get_pil()) writeGif(self.giffile, self.imageset, 0.2, 9999) return
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
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)
def process_image(self): #Start SimpleCV Code img = Image('lenna').rotate90().edges(self.edge_threshold).to_rgb() #End SimpleCV Code return img
import time from simplecv.api import Image import ImageTk # This has to be installed from the system repos import Tkinter Tkinter.Tk() image = Image('http://i.imgur.com/FTKqh.jpg') # load the simplecv logo from the web photo = ImageTk.PhotoImage(image.get_pil()) label = Tkinter.Label(image=photo) label.image = photo # keep a reference! label.pack() # show the image time.sleep(5)
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 process(self, filepath): img = Image(filepath) # load the image into SimpleCV img = img.edges() # Get the edges img.save(filepath) # save the temporary image
This example rotates through a few of the image morphology examples for more information see: http://en.wikipedia.org/wiki/Mathematical_morphology """ print __doc__ from simplecv.api import Color, Image max_threshold = 1 # this is used for the edge detection threshold_step = 1 # this is the amount to adjust the threshold threshold = max_threshold example = 1 while True: image = Image('lenna') # This just automatically cycles through threshold levels if threshold >= 20: threshold = max_threshold if example >= 5: example = 1 else: example += 1 else: threshold += threshold_step if example == 1: image = image.erode(threshold) text = "Erode Morphology Example: img.erode(" + str(threshold) + ")"
This example shows how to perform various rotations and warps on images and put back into a display. """ print __doc__ import time from simplecv.api import Color, Image sleep_for = 3 # seconds to sleep for text_point = (20, 20) font_size = 24 draw_color = Color.YELLOW while True: image = Image("orson_welles.jpg", sample=True) image.dl().set_font_size(font_size) image.dl().text("Original Size", text_point, color=draw_color) image.show() time.sleep(sleep_for) rot = image.rotate(45) rot.dl().set_font_size(font_size) rot.dl().text("Rotated 45 degrees", text_point, color=draw_color) rot.show() time.sleep(sleep_for) rot = image.rotate(45, scale=0.5) rot.dl().set_font_size(font_size) rot.dl().text("Rotated 45 degreesand scaled", text_point, color=draw_color) rot.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()
#!/usr/bin/python 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
""" This program super imposes the camera onto the television in the picture """ print __doc__ from simplecv.api import Camera, Image tv_original = Image("family_watching_television_1958.jpg", sample=True) tv_coordinates = [(353, 379), (433, 380), (432, 448), (354, 446)] tv_mask = Image(tv_original.size_tuple).invert().warp(tv_coordinates) tv = tv_original - tv_mask c = Camera() while True: bwimage = c.get_image().to_gray().resize(tv.width, tv.height).to_bgr() on_tv = tv + bwimage.warp(tv_coordinates) on_tv.show()
""" This program basically functions like a greenscreen that typically a weather or news reporter would use. It allows you to super impose anything standing in front of a "green screen" in front of another image this should even work with a camera is the user is standing in front of a green background """ print __doc__ import time from simplecv.api import Image, Color gs = Image("greenscreen.png", sample=True) gs.show() time.sleep(3) background = Image("icecave.png", sample=True) background.show() time.sleep(3) matte = gs.hue_distance(color=Color.GREEN, minvalue=100).binarize(inverted=True).to_bgr() matte.show() time.sleep(3) result = (gs - matte) + (background - matte.invert()) result.show() time.sleep(3)
quarter - 24.26 mm """ import time from simplecv.api import Image, Color, Blob print __doc__ # A quarter is 24.26mm or 0.955in quarter_size = 24.26 # millimeters # This will hold the ratio of the image size to a quarter's actual size size_ratio = 0 img = Image('coins.jpg', sample=True) coins = img.invert().find(Blob, minsize=200) # Here we compute the scale factor if coins: c = coins[-1] diameter = c.radius * 2 size_ratio = quarter_size / diameter # Now we print the measurements back on the picture for coin in coins: # get the physical size of the coin size = (coin.radius * 2) * size_ratio # label the coin accordingly if 18 < size < 20: coin_type = "penny"
""" This demo is used to find missing pills in a blister type of package it would be used in quality control in manufacturing type of application were you are verifying that the correct number of pills are present """ from simplecv.api import Image, Color, Blob pillcolor = ( 153, 198, 252 ) # This is set manually, you could either open the image you want and pick the color, # or use blob detection to find the blob and do .mean_color() to get the RGB value i = Image("pills.png", sample=True) expected_pillcount = 12 saturation_threshold = 40 # This is how much saturation to allow in the image pill_size = 100 # The size of the expected pills in pixels packblobs = i.find( Blob, minsize=10) # find the bright silver on back background, easy # run through the list of pills (blobs) found and check their color and markup the image when they are found for idx in range(len(packblobs)): pack = packblobs[idx].crop() pills_img = pack.hue_distance(pillcolor, minsaturation=saturation_threshold) pills_img = pills_img.binarize(127, inverted=True) pills = pills_img.find(Blob, minsize=pill_size) if not pills: continue
""" This examples demonstrates the motionBlur method. Use Up/Down Arrow keys to change power Use Left/Right Arrow keys to change angle """ print __doc__ import time import cv2 from simplecv.api import Image, Color from simplecv.core.drawing.layer import DrawingLayer img = Image((500, 500)) layer = DrawingLayer() layer.set_font_size(25) layer.rectangle((0, 0), (500, 500), Color.WHITE, 1, True) # 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)