In [1]:
%matplotlib inline

Find the centroid 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 [7]:
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 [8]:
# crop the image to the interesting part
imc = im[300:, 100:1000, :]
imgray = color.rgb2gray(imc)
skdemo.imshow_with_histogram(imc);



In [9]:
# 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')


Out[9]:
<matplotlib.text.Text at 0x1fdcf2b0>

In [10]:
# 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])


(386.44013084351923, 437.50215323718595)
('area', 109138)
('bbox', (234L, 242L, 539L, 633L))
('centroid', (386.44013084351923, 437.50215323718595))
('convex_area', 109478)
('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([[234, 400],
       [234, 401],
       [234, 402],
       ..., 
       [538, 533],
       [538, 534],
       [538, 535]], dtype=int64))
('eccentricity', 0.6160991256932928)
('equivalent_diameter', 372.7718034311423)
('euler_number', 1L)
('extent', 0.91516498260031021)
('filled_area', 109138)
('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.5671996464066)
('minor_axis_length', 332.84292632549716)
('moments', array([[  1.09138000e+05,   1.66370110e+07,   3.29186764e+09,
          7.32085783e+11],
       [  2.13367140e+07,   3.24800157e+09,   6.42549820e+11,
          1.42918964e+14],
       [  5.38932916e+09,   8.20069520e+11,   1.60795176e+14,
          3.54305741e+16],
       [  1.52946852e+12,   2.32691512e+14,   4.51910169e+16,
          9.85578030e+18]]))
('moments_central', array([[  1.09138000e+05,   1.44011251e-05,   7.55719509e+08,
         -1.30913163e+08],
       [ -3.69353756e-08,  -4.56990693e+06,   3.75881634e+08,
         -5.86949725e+10],
       [  1.21795563e+09,   3.06331539e+08,   6.97796566e+12,
          2.75079761e+12],
       [ -3.82828685e+08,  -1.16534091e+11,   7.05825455e+12,
         -1.04024165e+15]]))
('moments_hu', array([  1.65700450e-01,   1.50658471e-03,   2.18539253e-07,
         1.99044792e-09,  -2.75653879e-17,  -7.68809376e-11,
        -3.10408250e-17]))
('moments_normalized', array([[             nan,              nan,   6.34466436e-02,
         -3.32692753e-05],
       [             nan,  -3.83667819e-04,   9.55236983e-05,
         -4.51515778e-05],
       [  1.02253807e-01,   7.78487664e-05,   5.36785598e-03,
          6.40534712e-06],
       [ -9.72891692e-05,  -8.96447833e-05,   1.64354405e-05,
         -7.33213202e-06]]))
('orientation', 0.009885232493637196)
('perimeter', 1255.9797974644666)
('solidity', 0.99689435320338338)

In [ ]: