In [1]:
%matplotlib inline

Find the brightfield center of the aperture in TEMCA optical images


In [2]:
import numpy as np
import matplotlib.pyplot as plt
import boto3
from skimage import io
from skimage import data
from skimage import feature
from skimage import color
from skimage import filters
from skimage import transform 
from skimage import draw
from skimage import measure
from skimage import morphology
import skdemo
from scipy import ndimage as ndi
from toolbox.TAO.tao import TAO
from toolbox.TAO.taoDB import TAOdb

In [3]:
tao =  TAO()
taoDB = TAOdb()
specimen = '239849_7R'
media = '1'
barcode = '000001'
tao = taoDB.get_tao (specimen, media, barcode)
print tao
url = taoDB.get_aperture_image_url(specimen, media, barcode)
#url = r"\\AIBSDATA2\nc-em\adamb\atumDevelopment\tapeRuns\20160923\sectionImagesFromStack\239849_7R_cut201609230000.jpg"
im = io.imread(url)


{'media_id': '1', 'specimen_id': '239849_7R', 'aperture_im': '239849_7R_cut201609232598.jpg', 'barcode': '000001', 'bucket': 'temcaoptical2016', 'aperture_centroid': {'y': 686, 'x': 537}, 'rois': [{'width_nm': 1000000, 'angle_deg': 0, 'height_nm': 1000000, 'placement_id': 35, 'center': {'y': 214905, 'x': 582153}}], 'media': 'tape'}

In [4]:
# crop the image to the interesting part
imc = im[350:, 100:1000, :]
imgray = color.rgb2gray(imc)
skdemo.imshow_with_histogram(imc);



In [5]:
# threshold
threshold = filters.threshold_otsu(imgray)
imb = imgray <= threshold

# label it
imlabel = morphology.label(imb)

# remove small blobs
imnosmall = morphology.remove_small_objects(imlabel, min_size=4000, connectivity=1, in_place=False)

# fill small holes
imnosmall = morphology.remove_small_holes(imnosmall, min_size=200, connectivity=2, in_place=False)

# trim tendrils
se = morphology.disk(11)
imnosmall = morphology.opening(imnosmall, se)

fig, (ax_1, ax_2, ax_3) = plt.subplots(ncols=3, figsize=(10, 5))
ax_1.imshow(imb)
ax_1.set_title('threshold')
ax_2.imshow(imlabel, cmap='jet')
ax_2.set_title('label')
ax_3.imshow(imnosmall, cmap='gray')
ax_3.set_title('after morphology')


C:\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\skimage\morphology\misc.py:194: UserWarning: Any labeled images will be returned as a boolean array. Did you mean to use a boolean array?
  "Did you mean to use a boolean array?", UserWarning)
C:\WinPython-64bit-2.7.10.2\python-2.7.10.amd64\lib\site-packages\skimage\morphology\misc.py:122: UserWarning: Only one label was provided to `remove_small_objects`. Did you mean to use a boolean array?
  warn("Only one label was provided to `remove_small_objects`. "
Out[5]:
<matplotlib.text.Text at 0x1fd31390>

In [6]:
# show the centroid and other shape measurements
imint = imnosmall.astype(int)
imlabel = measure.label(imint, connectivity=1)
improps = measure.regionprops(imlabel)
region = improps[0]
print(region.centroid)

for prop in region:
    print(prop, region[prop])


(336.43367711443693, 437.50693363131768)
('area', 109178)
('bbox', (184L, 242L, 490L, 633L))
('centroid', (336.43367711443693, 437.50693363131768))
('convex_area', 109629)
('convex_image', array([[False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       ..., 
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False]], dtype=bool))
('coords', array([[184, 368],
       [184, 400],
       [184, 401],
       ..., 
       [488, 534],
       [488, 535],
       [489, 427]], dtype=int64))
('eccentricity', 0.6161171827144201)
('equivalent_diameter', 372.84010918233514)
('euler_number', 1L)
('extent', 0.91250856693913707)
('filled_area', 109178)
('filled_image', array([[False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       ..., 
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False]], dtype=bool))
('image', array([[False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       ..., 
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False],
       [False, False, False, ..., False, False, False]], dtype=bool))
('label', 1)
('major_axis_length', 422.6492206726981)
('minor_axis_length', 332.90156213879635)
('moments', array([[  1.09178000e+05,   1.66424040e+07,   3.29312663e+09,
          7.32419695e+11],
       [  2.13450560e+07,   3.24908571e+09,   6.42794657e+11,
          1.42982582e+14],
       [  5.39198058e+09,   8.20411484e+11,   1.60866918e+14,
          3.54482596e+16],
       [  1.53038424e+12,   2.32810192e+14,   4.52146732e+16,
          9.86138269e+18]]))
('moments_central', array([[  1.09178000e+05,  -1.01678493e-05,   7.56263792e+08,
         -1.23848259e+08],
       [  3.86490910e-08,  -4.61966429e+06,   3.73952370e+08,
         -5.95275809e+10],
       [  1.21887414e+09,   2.98409833e+08,   6.98557804e+12,
          2.82842948e+12],
       [ -3.82043051e+08,  -1.17196157e+11,   6.97727294e+12,
         -1.03933027e+15]]))
('moments_hu', array([  1.65701775e-01,   1.50682681e-03,   2.12749827e-07,
         1.96858230e-09,  -2.70008801e-17,  -7.59322435e-11,
        -2.98997520e-17]))
('moments_normalized', array([[             nan,              nan,   6.34458236e-02,
         -3.14450340e-05],
       [             nan,  -3.87561072e-04,   9.49463893e-05,
         -4.57417558e-05],
       [  1.02255951e-01,   7.57661630e-05,   5.36780766e-03,
          6.57767461e-06],
       [ -9.70006109e-05,  -9.00550283e-05,   1.62260475e-05,
         -7.31497822e-06]]))
('orientation', 0.009984753408054327)
('perimeter', 1253.7371567773473)
('solidity', 0.99588612502166396)

In [ ]: