In [1]:
import cv2
from numpy import *
import readtraces as rt
import os
import sys
from matplotlib import pyplot as pl
from matplotlib import cm
from PIL import Image
%matplotlib inline

In [2]:
#change these if you're not Corinna!
if os.path.exists('/media/corinna/corinna-data/'): dataroot='/media/corinna/corinna-data/'
if os.path.exists('/media/corinna-data/'): dataroot='/media/corinna-data/'
if os.path.exists('/home/cmaass/goettingen/python/swimmertracking/'): workdir='/home/cmaass/goettingen/python/swimmertracking/'
print dataroot, workdir
if not workdir in sys.path: sys.path.append(workdir)
reload(rt)


def circle_invert(pt, cr, integ=True):
    """Inverts point (inside) at circle cirumference. Used to create mirror clusters for Voronoi construction.
    arguments point: (x,y), circle: (xc,yc,r). returns (x,y) as float"""
    d=sqrt((pt[0]-cr[0])**2+(pt[1]-cr[1])**2) #distance to centre
    scf=2*cr[2]/d-1 #scaling factor 
    newpt=[cr[0]+(pt[0]-cr[0])*scf, cr[1]+(pt[1]-cr[1])*scf]
    if integ: newpt=[int(p) for p in newpt]
    return  newpt


/media/corinna-data/ /home/cmaass/goettingen/python/swimmertracking/
/usr/lib/pymodules/python2.7/matplotlib/__init__.py:1173: UserWarning:  This call to matplotlib.use() has no effect
because the backend has already been chosen;
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)

In [3]:
os.chdir(dataroot+'data/20150107_Many_droplet_system_height_test_6mm_wide_15wtpcTTAB_5uL_50um_Droplets_2x_Olympus_4fps/')
mov=rt.movie('0p5mm_high_2_output.avi')
f=mov.getFrame(0)
circ=cv2.HoughCircles(f[:,:,2],cv2.cv.CV_HOUGH_GRADIENT,3,2000,minRadius=450,maxRadius=600).astype(int).flatten()
print circ
b=f.copy()
cv2.circle(b,(circ[0],circ[1]),circ[2]-10, (0,0,255),5)
pl.imshow(b)
mask=zeros(mov.shape[::-1])
cv2.circle(mask,(circ[0],circ[1]),circ[2]-10,1,-1)


[889 571 517]

In [15]:
f=mov.getFrame(3000)
vorim=f.copy()
contim=f.copy()
f=f[:,:,2]
fcopy=cv2.GaussianBlur(f,(51,51),0)
fcopy=fcopy*mask+255*(1-mask)
print amax(fcopy), amin(fcopy)
thresh=(fcopy<180).astype(uint8)

print amax(thresh), amin(thresh)
blobs,contours=rt.extract_blobs(thresh, -1, (10,3000), -1, diskfit=False,returnCont=True, outpSpac=1, spherthresh=10)
# thresh=dstack((thresh,thresh,thresh))
cv2.drawContours(contim,contours,-1,(255,0,0),2)
cv2.circle(contim,(circ[0],circ[1]),circ[2], (0,0,255),5)
pl.imshow(contim)

newpoints=[]
vor=rt.Voronoi(blobs[:,3:5])
dists=sum((vor.vertices-array(circ[:2]))**2,axis=1)-circ[2]**2
extinds=[-1]+(dists>0).nonzero()[0]
for i in range(blobs.shape[0]):
    r=vor.regions[vor.point_region[i]]
    if any(j in extinds for j in r):
        newpoints+=[circle_invert(blobs[i,3:5],circ, integ=True)]
        cv2.circle(contim,(newpoints[-1][0],newpoints[-1][1]),10, (0,180,0),-5)
pts=vstack((blobs[:,3:5],array(newpoints)))
vor=rt.Voronoi(pts)
Image.fromarray(contim).save(workdir+'contim.png')
for i in range(blobs.shape[0]):
    r=vor.regions[vor.point_region[i]]
    col=tuple([int(255*c) for c in cm.jet(i*255/len(vor.points))])[:3]
#     pl.plot(vor.vertices[r,1],vor.vertices[r,0], c=col[:3])
    cv2.polylines(vorim, [(vor.vertices[r]).astype(int32)], True, col[:3], 2)
Image.fromarray(vorim).save(workdir+'vorim.png')
print vorim.shape
print workdir


3000.0
255.0 27.0
1 0
# blobs:  69
(1080, 1920, 3)
/home/cmaass/goettingen/python/swimmertracking/

In [16]:
r=pl.hist(fcopy.flatten(),bins=255,log=True)



In [17]:
pl.imshow(vorim)


Out[17]:
<matplotlib.image.AxesImage at 0x7d9ead0>

In [39]:
print workdir


/media/corinna-data/data/20150107_Many_droplet_system_height_test_6mm_wide_15wtpcTTAB_5uL_50um_Droplets_2x_Olympus_4fps/

In [33]:
g=array(contours[68]).flatten().reshape(-1,2)
pl.plot(g[:,0],g[:,1])


Out[33]:
[<matplotlib.lines.Line2D at 0x4c88190>]

In [34]:
blobs[:,-1]<10


Out[34]:
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True, False,  True], dtype=bool)

In [35]:
len(contours[blobs[:,-1]>10])


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-35-0ce0b91e8a25> in <module>()
----> 1 len(contours[blobs[:,-1]>10])

TypeError: only integer arrays with one element can be converted to an index

In [20]:
dists=sum((vor.vertices-array(circ[:2]))**2,axis=1)-circ[2]**2
print dists
inds= (dists>0).nonzero()[0]


[   523549.52     498764.169  22517806.619    430414.294    288976.305
    491712.518   1356014.251    317697.872    248558.544      1036.671
      3572.16    -262688.283   -254897.753    229643.724    192020.309
      9640.131     14712.994     73332.767    473053.699    967645.174
    226634.06     164307.816    275063.877   -266774.927     77119.23
    301313.843    453030.506     27238.88      16068.557   -207939.595
    -81694.847   -118253.286   -118693.22     221565.375    107737.376
    199028.238   -104785.069      8040.766    291227.748    205867.989
    188987.768    386583.849    806412.415    366191.887   -181101.261
   -132739.965      7308.694   -111737.806   -122016.554    304826.628
     71939.834    121004.679    183825.87       3532.053    -84095.694
    232629.006   -239214.352    224831.763    265233.648      5395.759
       905.039      4931.699   -183850.869   -137578.155   -143203.238
   -154278.586   -186808.234   -173823.738    122816.881    159685.11
    251006.954      1386.709      6724.838    397741.11     271877.106
    520409.418   1727228.114    904264.224    -87102.105      4715.805
       788.329      5353.322    164881.414     89449.6        7147.522
    -90233.517    -52421.374   -134060.109   -180484.548     62637.32
    123337.813    573742.002    360469.521    210944.176    478820.298
    260783.825    140203.228     13406.132     42449.877   -255521.262
   -215873.809   -242873.159   -240962.354   -140109.788   -104723.415
       878.556      3820.558   -100133.66     -70055.845      2216.965
   -251249.627   -186771.953   -223558.566    308742.875    327226.379
    149667.481    153659.069    766517.031    756269.267    119690.801
      5265.571      1149.186    332458.737    233062.491   1624323.963
    391677.573    123977.135    124079.52      46210.63     159186.036
      8601.211    137949.629   1659243.264    568310.794      8209.085
     14798.41    -234631.216   -228315.907   -174686.482    -81364.782
    -91310.221   -241311.496   -253807.826   -219124.38    -217138.545
   -160909.043   -129852.232   -129248.721   -190160.833   -240678.343
   -150913.382   -191152.441       389.921      3755.638    -83339.991
    -61839.446      4460.908      6880.252    -85616.774      3097.089
    -69053.438      8954.769       273.148    284893.318    164529.743
    181262.867    324304.165    452866.404    461608.765     69774.479
    -76223.69       3295.637      5761.938    -48747.611    -80945.729
      7827.857    -20021.965      7435.146     75688.11      99502.355
      9642.673     13540.637    139587.636    377095.958   4053428.685
    738840.537    406996.612     76091.178    155989.664    302197.927
    244011.278    156386.987    145242.519   -203592.499   -203863.216
   -199742.349   -214976.567   -189951.071   -178582.762   -204935.09
   -197100.481   -219220.925   -192752.312   -142071.359   -127895.002
   -129103.204   -220298.728   -245182.531   -198937.8     -215027.915
   -211031.546   -160488.051   -136577.809   -164545.092       315.115
      7045.325    -67007.561    -73854.258    179293.632     99022.595
     28277.127    117387.01       -131.449      3901.645   -139730.634
   -187720.67    -148684.062    180813.68     355677.012    438315.373
    248204.088    271077.793      7889.053      6722.117   -239375.141
   -215866.771   -257230.143   -236623.802   -121794.723   -151925.556
   -169747.5     -150463.356   -204222.187   -192235.732   -174357.644
   -175346.129   -266236.452   -259832.715   -255340.173   -244724.361
   -243291.7     -130873.497    -95364.803    -26661.958   -140501.21
   -112363.792   -114904.602      4572.476    -76564.245      5487.191
    -64673.683   -111671.406   -114794.115]

In [21]:
print vor.regions
ins=0
out=0
for r in vor.regions: 
    if any(i in r for i in inds): out+=1
    else: ins+=1
print ins, out


[[], [4, 1, -1, 2, 3], [8, 3, 2, 0, 7], [17, 13, 14, 15, 16], [17, 8, 3, 4, 13], [20, 14, 13, 4, 1, 18], [19, -1, 1, 18], [28, 9, 16, 17, 8, 7, 27], [31, 10, 15, 16, 9, 30], [39, 21, 5, 6, 38], [43, 22, 5, 6, -1, 42], [58, 26, 19, 18, 20, 57], [59, 10, 15, 14, 20, 57], [64, 61, 59, 10, 31, 63], [67, 30, 31, 63, 66], [72, 46, 34, 35, 68], [70, 41, 42, 43, 40, 69], [77, 75, 74, 73, 76], [74, 35, 68, 70, 41, 73], [76, -1, 42, 41, 73], [75, 33, 34, 35, 74], [77, 49, 52, 33, 75], [81, 69, 70, 68, 72, 71, 80], [83, 40, 43, 22, 82], [84, 79, 81, 69, 40, 83], [90, 21, 5, 22, 82, 89], [95, 93, 92, 91, 94], [92, 38, 6, -1, 91], [98, 39, 38, 92, 93, 96], [101, 11, 23, 99], [105, 46, 72, 71, 47, 48, 104], [112, 100, 102, 101, 99, 110], [112, 100, 88, 87, 45, 44, 111], [118, 114, 115, 116, 113, 117], [116, 50, 51, 52, 49, 113], [117, -1, 76, 77, 49, 113], [116, 50, 106, 109, 53, 24, 115], [120, 53, 24, 119], [121, 54, 108, 109, 53, 120], [125, 122, 0, 2, -1, 124], [128, 96, 93, 95, 126], [131, 58, 26, 25, 55, 129], [131, 58, 57, 59, 61, 60, 130], [133, 25, 26, 19, -1, 132], [135, 129, 131, 130, 134], [137, 56, 102, 101, 11, 12, 136], [139, 28, 9, 30, 67, 138], [143, 62, 29, 141], [144, 66, 63, 64, 65, 62, 143], [151, 148, 48, 104, 103, 150], [157, 153, 152, 154, 155, 156], [154, 45, 87, 86, 37, 152], [153, 90, 21, 39, 98, 97, 37, 152], [157, 89, 90, 153], [155, 36, 44, 45, 154], [156, 84, 79, 78, 36, 155], [157, 89, 82, 83, 84, 156], [162, 158, 160, 159, 161], [162, 105, 104, 103, 158], [160, 107, 108, 109, 106, 159], [161, 51, 50, 106, 159], [162, 105, 46, 34, 33, 52, 51, 161], [168, 166, 165, 164, 163, 167], [167, 118, 114, 163], [164, 119, 24, 115, 114, 163], [168, 124, 125, 123, 166], [168, 124, -1, 117, 118, 167], [176, 174, 173, 171, 172, 175], [172, 128, 96, 98, 97, 171], [173, 85, 86, 37, 97, 171], [181, 179, 178, 177, 180], [181, 127, 126, 128, 172, 175, 180], [186, 183, 132, -1, 184], [185, 94, 91, -1, 184], [192, 187, 188, 189, 190, 191], [190, 133, 132, 183, 189], [191, 55, 25, 133, 190], [192, 135, 129, 55, 191], [194, 136, 12, 142, 141, 29, 193], [197, 140, 139, 138, 196], [198, 145, 32, 140, 197], [205, 202, 201, 200, 203, 204], [203, 47, 48, 148, 200], [202, 111, 112, 110, 149, 201], [205, 78, 36, 44, 111, 202], [204, 80, 71, 47, 203], [205, 78, 79, 81, 80, 204], [207, 149, 201, 200, 148, 151, 206], [213, 210, 209, 208, 211, 212], [211, 147, 146, 145, 198, 199, 208], [213, 150, 151, 206, 210], [212, 107, 108, 54, 147, 211], [213, 150, 103, 158, 160, 107, 212], [215, 169, 165, 164, 119, 120, 121, 214], [216, 146, 147, 54, 121, 214], [217, 32, 145, 146, 216], [223, 219, 218, 221, 220, 222], [221, 122, 125, 123, 218], [219, 169, 165, 166, 123, 218], [223, 215, 169, 219], [222, 217, 32, 140, 139, 28, 27, 220], [221, 122, 0, 7, 27, 220], [223, 215, 214, 216, 217, 222], [231, 229, 228, 227, 230], [230, 182, 178, 179, 227], [228, 127, 181, 179, 227], [229, 185, 94, 95, 126, 127, 228], [231, 186, 184, 185, 229], [231, 186, 183, 189, 188, 182, 230], [233, 187, 192, 135, 134, 232], [236, 142, 141, 143, 144, 234], [235, 196, 138, 67, 66, 144, 234], [237, 199, 198, 197, 196, 235], [243, 239, 238, 241, 240, 242], [239, 224, 170, 176, 174, 238], [241, 85, 173, 174, 238], [243, 225, 226, 224, 239], [241, 85, 86, 87, 88, 240], [242, 56, 102, 100, 88, 240], [243, 225, 195, 137, 56, 242], [245, 195, 137, 136, 194, 244], [250, 247, 246, 248, 249], [248, 207, 149, 110, 99, 23, 246], [247, 236, 142, 12, 11, 23, 246], [250, 237, 235, 234, 236, 247], [249, 209, 210, 206, 207, 248], [250, 237, 199, 208, 209, 249], [256, 254, 251, 252, 253, 255], [254, 193, 194, 244, 251], [255, 60, 130, 134, 232, 253], [256, 65, 62, 29, 193, 254], [256, 65, 64, 61, 60, 255], [262, 258, 257, 259, 260, 261], [258, 252, 253, 232, 233, 257], [259, 177, 178, 182, 188, 187, 233, 257], [262, 245, 244, 251, 252, 258], [260, 170, 176, 175, 180, 177, 259], [261, 226, 224, 170, 260], [262, 245, 195, 225, 226, 261]]
0 139

In [1]:
import cv2

In [2]:
from numpy import *

In [110]:
f=Image.open('/media/cmdata/datagoe/140916/Collective_many_droplets_12p5wtpcTTAB_10uL_50um_Droplets_2x_Olympus_1-data/mask.png')

In [111]:
f=array(f)
print amax(f)
th=f.copy()[:,:,0].astype(uint8)


255

In [112]:
contours, hierarchy=cv2.findContours(th,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
print contours
(xm,ym),rm=cv2.minEnclosingCircle(contours[0])
print xm, ym, rm
# xm,ym,rm=300,300,100


[array([[[858,  42]],

       [[857,  43]],

       [[845,  43]],

       ..., 
       [[936,  43]],

       [[924,  43]],

       [[923,  42]]], dtype=int32)]
890.5 578.5 553.607971191

In [113]:
cv2.circle(f,(int(xm),int(ym)),int(rm),(255,0,100),4)

In [114]:
circ


Out[114]:
array([ 883.5       ,  565.5       ,  550.66552734], dtype=float32)

In [115]:
import Image

In [116]:
Image.fromarray(f).save('test.png')

In [ ]: