In [1]:
import os, os.path
from matplotlib import pyplot as plt
from pylab import get_cmap
import SimpleCV as cv
from glob import glob

In [2]:
def show_img(img, ax = None):
    if ax is not None:
        plt.sca(ax)
    nimg = img.getNumpy()
    return plt.imshow(nimg, aspect='equal')

In [3]:
path = '/home/will/Dropbox/burnimages/*.jpg'
norm_files = sorted(f for f in glob(path) if '-e' not in f)
masked_files = sorted(f for f in glob(path) if '-e' in f)

fig, axs = plt.subplots(6,6, figsize = (10,10))
for f, ax in zip(norm_files, axs.flatten()):
    img = cv.Image(f)
    show_img(img, ax = ax)
    ax.set_xticks([])
    ax.set_yticks([])
fig.tight_layout()



In [4]:
from itertools import islice, izip_longest
from dateutil.parser import parse
def make_wound_mask(norm_img, green_img, color,
                    minsize = None,
                    maxsize = None):
    
    wmask = green_img.hueDistance(color).invert().threshold(200)
    blobs = norm_img.findBlobsFromMask(wmask, 
                                       minsize = minsize,
                                       maxsize = maxsize)
    
    return wmask, blobs

fig, axs = plt.subplots(6,6, figsize = (10,10))
results = []
for fname, mf, of, ax in izip_longest(norm_files, masked_files, norm_files, axs.flatten()):
    mask_img = cv.Image(mf)
    norm_img = cv.Image(of)
    
    dt = parse(fname.rsplit(os.sep,1)[1].replace('.jpg', '').replace('.',':'))

    wound_mask, wound_blobs = make_wound_mask(norm_img, mask_img, cv.Color.GREEN,
                                              minsize = 1000)
    dime_mask, dime_blobs = make_wound_mask(norm_img, mask_img, cv.Color.BLUE,
                                            minsize = 500)
    layer = cv.DrawingLayer((norm_img.width, norm_img.height))
    wound_blobs[-1].drawHull(color=cv.Color.BLUE, width = 100, layer = layer)
    dime_blobs[-1].drawHull(color=cv.Color.RED, width = 100, layer = layer)
    
    norm_img.addDrawingLayer(layer)
    fnorm = norm_img.applyLayers()
    
    ratio = wound_blobs[-1].area()/dime_blobs[-1].area()
    results.append((dt, ratio))
    if ax is not None:
        show_img(fnorm, ax = ax)
        ax.set_xticks([])
        ax.set_yticks([])
        ax.set_title(ratio)
        
fig.tight_layout()



In [5]:
import pandas as pd

res_df = pd.DataFrame(sorted(results), columns = ['SampleTime', 'Ratio'])
dime_diameter = 18 #mm
dime_area = 3.141*(dime_diameter/2)**2
res_df['Area-mm2'] = dime_area*res_df['Ratio']
res_df.set_index('SampleTime', inplace=True)
res_df


Out[5]:
Ratio Area-mm2
SampleTime
2013-07-05 09:33:13 5.902900 1501.821832
2013-07-05 18:31:03 5.904545 1502.240254
2013-07-05 18:31:04 5.996602 1525.661366
2013-07-05 18:31:07 6.078266 1546.438423
2013-07-06 09:49:14 5.666436 1441.660426
2013-07-06 09:49:16 5.874674 1494.640333
2013-07-06 09:49:22 5.749179 1462.711823
2013-07-06 17:00:04 4.963127 1262.723629
2013-07-06 17:00:10 4.856689 1235.643571
2013-07-06 17:00:14 7.470487 1900.648670
2013-07-06 17:00:16 6.741573 1715.197646
2013-07-07 09:09:41 5.591015 1422.471751
2013-07-07 09:09:46 5.249570 1335.600922
2013-07-07 09:09:51 5.676721 1444.277095
2013-07-07 09:09:52 5.016876 1276.398735
2013-07-07 09:09:54 5.239944 1333.151704
2013-07-07 13:26:11 5.880744 1496.184763
2013-07-07 13:26:13 7.262800 1847.808779
2013-07-07 13:26:16 4.763216 1211.862153
2013-07-07 13:26:18 6.944207 1766.752118
2013-07-08 08:33:45 3.187813 811.046502
2013-07-08 08:33:48 3.296285 838.644196
2013-07-08 08:33:51 3.477146 884.658840
2013-07-08 21:46:07 4.427752 1126.513008
2013-07-08 21:46:11 4.297895 1093.474772
2013-07-08 21:46:20 5.805661 1477.082034
2013-07-09 18:49:30 4.557377 1159.492506
2013-07-09 18:49:37 5.165097 1314.109254
2013-07-09 18:49:42 4.499211 1144.693656
2013-07-13 10:45:21 2.637399 671.009802
2013-07-13 10:45:25 2.505289 637.398204
2013-07-13 10:45:30 3.440502 875.336046
2013-07-13 10:45:34 3.607857 917.914646
2013-07-17 14:11:08 0.341417 86.863608
2013-07-17 14:11:22 0.172350 43.849359
2013-07-17 14:11:23 0.343423 87.373994
2013-07-17 14:11:26 0.359233 91.396544

In [11]:
res_df['Area-mm2'].plot()
out = pd.ewma(res_df['Area-mm2'], freq='d', span = 1)
out.plot(lw = 10, alpha = 0.7)
plt.ylabel('Wound-Area-mm^2')


Out[11]:
<matplotlib.text.Text at 0x4306d90>

In [ ]: