In [1]:
from __future__ import division
import SimpleCV as cv
import os
from random import randint
import numpy as np
import pandas as pd
disp = cv.Display(displaytype='notebook')
os.chdir('/home/will/SimpleCVExample/')

In [2]:
print 2+2


4

In [22]:
img.findCircle?

In [26]:
from sklearn.metrics import mean_absolute_error
def gen_random_images(ncircles):
    base_img = cv.Image('empty_background.png').resize(100, 100)
    for _ in range(ncircles):
        pos = (randint(10, 40), randint(10, 40))
        base_img.dl().circle(pos, 10, cv.Color.RED, filled=True)
    nimg = base_img.applyLayers()
    mask = nimg.colorDistance(cv.Color.RED).invert().threshold(100)
    true_area = (mask.getNumpy()[:,:,0].flatten()>0).sum()
    return nimg, true_area


def predict_image(img, display=False, threshval = 50,
                  minsize = 10, 
                  maxsize = 0, threshblocksize = 5, 
                  threshconstant = 5, appx_level = 3):
    
    mask = img.colorDistance(cv.Color.RED).invert()
    #blobs = mask.findCircle()
    blobs = mask.findBlobs(threshval=threshval,
                           minsize=minsize,
                           maxsize=maxsize,
                           threshblocksize=threshblocksize,
                           threshconstant=threshconstant,
                           appx_level=appx_level)
    if display:
        if blobs is not None:
            blobs.draw()
        mask.save(disp)
            
    if blobs is None:
        return 0
    return blobs.area().sum()


def predict_batch(nreps, ncircles, **kwargs):
    
    true_areas = []
    guessed_areas = []
    for n in range(nreps):
        img, area = gen_random_images(ncircles)
        guessed_area = predict_image(img, **kwargs)
        true_areas.append(area)
        guessed_areas.append(guessed_area)
        
    return mean_absolute_error(true_areas, guessed_areas)

In [17]:
img.findBlobs?

In [27]:
img, area = gen_random_images(3)
img.save(disp)
print area


884

In [28]:
predict_image(img, display=True,
              threshval=50)


Out[28]:
806.0

In [32]:
list(product(minsizes, threshvals))


Out[32]:
[(5, -1),
 (5, 49),
 (5, 99),
 (5, 149),
 (5, 199),
 (10, -1),
 (10, 49),
 (10, 99),
 (10, 149),
 (10, 199),
 (15, -1),
 (15, 49),
 (15, 99),
 (15, 149),
 (15, 199)]

In [41]:
from itertools import product

minsizes = range(5, 100, 10)
#threshblocksizes = range(5, 20, 2)
#threshconstants = range(5, 20, 2)
#appx_levels = range(1, 11, 2)
threshvals = range(20, 200, 20)
circles = range(2, 5)

reps = 20


results = []

for circle in circles:
    print circle
    iterable = product(minsizes, threshvals)
    for minsize, threshval in iterable:
    
        mean_error = predict_batch(reps, circle, 
                                   threshval=threshval,
                                   minsize=minsize)
        results.append({
                    'circle': circle,
                    'minsize':minsize,
                    'threshval':threshval,
                    'mean_error': mean_error,
                    })


2
3
4

In [42]:
results_df = pd.DataFrame(results)
pos = results_df['mean_error'].idxmin()
results_df.head(n=5)


/usr/local/lib/python2.7/dist-packages/pandas/core/config.py:570: DeprecationWarning: height has been deprecated.

  warnings.warn(d.msg, DeprecationWarning)
/usr/local/lib/python2.7/dist-packages/pandas/core/config.py:570: DeprecationWarning: height has been deprecated.

  warnings.warn(d.msg, DeprecationWarning)
Out[42]:
circle mean_error minsize threshval
0 2 44.4 5 20
1 2 41.2 5 40
2 2 44.9 5 60
3 2 42.5 5 80
4 2 43.3 5 100

In [46]:
plt.figure(figsize = (15,15))
plt.scatter(results_df['mean_error'], results_df['threshval'])


Out[46]:
<matplotlib.collections.PathCollection at 0x7c47750>

In [43]:
results_df.ix[pos]


Out[43]:
circle         2.0
mean_error    39.6
minsize       35.0
threshval     60.0
Name: 29, dtype: float64

In [40]:



/usr/local/lib/python2.7/dist-packages/pandas/core/config.py:570: DeprecationWarning: height has been deprecated.

  warnings.warn(d.msg, DeprecationWarning)
/usr/local/lib/python2.7/dist-packages/pandas/core/config.py:570: DeprecationWarning: height has been deprecated.

  warnings.warn(d.msg, DeprecationWarning)
Out[40]:
circle mean_error minsize
0 2 45.15 5
1 2 43.00 5
2 2 43.80 5
3 2 46.35 5
4 2 45.00 5
5 2 44.85 5
6 2 44.55 5
7 2 46.10 5
8 2 46.55 5
9 2 45.35 15
10 2 43.05 15
11 2 47.95 15
12 2 47.85 15
13 2 40.10 15
14 2 44.10 15
15 2 45.55 15
16 2 41.15 15
17 2 41.70 15
18 2 44.90 25
19 2 46.35 25
20 2 45.25 25
21 2 43.85 25
22 2 43.35 25
23 2 47.05 25
24 2 42.30 25
25 2 45.60 25
26 2 45.60 25
27 2 45.35 35
28 2 45.15 35
29 2 42.50 35
30 2 43.45 35
31 2 46.45 35
32 2 42.45 35
33 2 44.10 35
34 2 47.00 35
35 2 41.00 35
36 2 47.45 45
37 2 42.55 45
38 2 39.45 45
39 2 44.80 45
40 2 43.25 45
41 2 40.80 45
42 2 47.55 45
43 2 44.05 45
44 2 42.35 45
45 2 44.70 55
46 2 44.15 55
47 2 45.90 55
48 2 44.30 55
49 2 45.20 55

In [38]:
results_df.ix[pos]


Out[38]:
circle         2.00
mean_error    39.45
minsize       45.00
Name: 38, dtype: float64

In [36]:
_ = pd.scatter_matrix(results_df, figsize=(15,15))



In [16]:
results_df['circle'].unique()


Out[16]:
array([2])

In [11]:
print 100**2


10000

In [ ]: