Skip to content

Instantly share code, notes, and snippets.

@pmarkun
Created November 23, 2013 04:05
Show Gist options
  • Select an option

  • Save pmarkun/7610660 to your computer and use it in GitHub Desktop.

Select an option

Save pmarkun/7610660 to your computer and use it in GitHub Desktop.
import numpy as np
import scipy
import pylab
from scipy import ndimage
import mahotas
import math
from SimpleCV import Camera, Image
def calcula_percento(objeto, valor_referencia):
if objeto['h'] >= valor_referencia*0.5:
return 10
elif objeto['w'] <= valor_referencia*0.5:
return 0.1 #calcular varios juntos
else:
return 1
# Initialize the camera
cam = Camera(0)
# Loop to continuously get images
while True:
# Get Image from camera
img = cam.getImage()
#img = img.crop(50,50,img.width-50,img.height-50, smart=True)
# Make image black and white
img = img.blur().invert()
blured = mahotas.thresholding.otsu(img.getNumpy())
img = img.threshold(blured)
img.show()
labeled,nr_objects = ndimage.label(img.getNumpy() > blured)
print nr_objects
objetos = ndimage.find_objects(labeled)
md = []
for objeto in objetos:
o = {}
o['h'] = objeto[0].stop - objeto[0].start
o['w'] = objeto[1].stop - objeto[1].start
o['area'] = o['h']*o['w']
if (o['h'] > o['w']):
w = o['h']
h = o['w']
o['h'] = h
o['w'] = w
md.append(o)
print md
total = 0
for objeto in md:
total += calcula_percento(objeto, 117)
print total-10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment