In [2]:
import cv2
import numpy as np
import math
from matplotlib import pyplot as plt
%matplotlib inline
import skimage.measure as ski

In [3]:
afmdata=np.genfromtxt('../Data/BackgroundedTXTFiles/500nmGood-1')
afmdata= afmdata*(10**9)

In [17]:
#factor=(255)/(afmdata.max()-afmdata.min())
#for i in range(height):
#    for j in range(width):
#        intensity=np.int((afmdata[i][j]-afmdata.min())*factor)
#        #afmimg[i][j]=np.array([np.int((afmdata[i][j]-afmdata.min())*factor),0,0])
#        afmimg[i][j]=np.array([intensity, intensity, intensity])


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-17-38a88e8beb7e> in <module>()
      2 for i in range(height):
      3     for j in range(width):
----> 4         intensity=np.int((afmdata[i][j]-afmdata.min())*factor)
      5         #afmimg[i][j]=np.array([np.int((afmdata[i][j]-afmdata.min())*factor),0,0])
      6         afmimg[i][j]=np.array([intensity, intensity, intensity])

/Users/peacenick36/anaconda/lib/python2.7/site-packages/numpy/core/_methods.pyc in _amin(a, axis, out, keepdims)
     27 
     28 def _amin(a, axis=None, out=None, keepdims=False):
---> 29     return umr_minimum(a, axis, None, out, keepdims)
     30 
     31 def _sum(a, axis=None, dtype=None, out=None, keepdims=False):

KeyboardInterrupt: 

In [4]:
plt.imshow(afmdata[100:300,400:600])


Out[4]:
<matplotlib.image.AxesImage at 0x118503490>

In [28]:
snip11=afmdata[100:300,400:600]
#zeros_idx = np.where(snip11 < 0)
#snip11[zeros_idx] = 0
#print snip11
factor=(255)/(snip11.max()-snip11.min())
height, width = snip11.shape
# afmimg=np.zeros((height, width, 3))
afmimg=np.zeros((height, width))
count = 0
for i in range(height):
    for j in range(width):
        intensity=np.uint8((snip11[i][j]-snip11.min())*factor)
        #afmimg[i][j]=np.array([np.int((afmdata[i][j]-afmdata.min())*factor),0,0])
        #afmimg[i][j]=np.array([intensity, intensity, intensity])
        afmimg[i][j]=intensity
#print afmimg
afmimg
plt.imshow(afmimg)


Out[28]:
<matplotlib.image.AxesImage at 0x117e31f10>

In [29]:
afmimg = np.uint8(afmimg)

In [32]:
### Cropping out the part that we are interesting in
# snip11=np.uint8(afmdata[300:400,400:500])
# Plotting the image that is interesting to us

#print afmimg
plt.subplot(131),plt.imshow(afmimg)
copy_im=np.copy(afmimg)

# Detecting borders using the Canny algorithm - setting thresholds 
# From the image information - background ~ 69, wire ~100
#edges = cv2.Canny(afmimg,52,100)
#plt.subplot(132),plt.imshow(edges,cmap='gray')


# Different algorithm!
# Locating contours
ret,thresh = cv2.threshold(afmimg,145,255,cv2.THRESH_TOZERO)
plt.subplot(132)
plt.imshow(thresh)
#thresh=cv2.cvtColor(thresh, cv2.COLOR_BGR2GRAY)
derp, contours, hierarchy =cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#print contours
cnts = max(contours, key=cv2.contourArea)
hull = cv2.convexHull(cnts)
#print hull
hull = np.reshape(hull,(len(hull),2))
plt.subplot(133)
plt.imshow(afmimg)
plt.scatter(hull[:,0],hull[:,1],color='r')

#different method
#cnts = max(contours, key=cv2.contourArea)
#rect = cv2.minAreaRect(cnts)
#box = cv2.boxPoints(rect)
#box = np.int0(box)
#hello=cv2.drawContours(copy_im,[box],0,(0,0,255),2)
#hello=cv2.drawContours(snip11, [cnts], 0, (0,255,0), 3)
#plt.subplot(133),plt.imshow(hello)
#plt.scatter(box[:,0],box[:,1], c='yellow')


Out[32]:
<matplotlib.collections.PathCollection at 0x11f03bdd0>

In [38]:
copy_im=np.copy(afmimg)
rows,cols = afmimg.shape[:2]
[line_x, line_y, int_x, int_y] = cv2.fitLine(cnts,distType=2,param=0,reps=0.01,aeps=0.01)
lefty = int((-int_x*line_y/line_x) + int_y)
righty = int(((cols-int_x)*line_y/line_x)+int_y)
#cv2.line(copy_im,(cols-1,righty),(0,lefty),(0,255,0),2)
cv2.line(copy_im,(righty,0),(lefty,cols-1),(0,255,0),2)
plt.imshow(copy_im)
print lefty
print righty
#plt.plot(line_x + int_x, line_y + int_y,color='r')


37
131

In [41]:
profile = ski.profile_line(afmimg,(righty,0),(lefty,cols-1))

In [42]:
plt.plot(profile)


Out[42]:
[<matplotlib.lines.Line2D at 0x11f607b50>]

In [43]:
profile


Out[43]:
array([ 103.,   97.,   97.,   96.,   93.,   92.,   96.,   94.,   92.,
         92.,   95.,   95.,   95.,   95.,   94.,   95.,   95.,   97.,
         95.,   86.,   89.,   88.,   86.,   89.,   91.,   87.,   82.,
         78.,   73.,   77.,   73.,   70.,   74.,   69.,   64.,   64.,
         60.,   60.,   58.,   56.,   54.,   54.,   54.,   66.,   68.,
         68.,   71.,   72.,   76.,   80.,   86.,   88.,   88.,   94.,
         99.,   99.,   99.,  103.,  110.,  107.,  106.,  103.,  103.,
        100.,  101.,  100.,   98.,   98.,   99.,  100.,   98.,  103.,
        103.,  107.,  109.,  108.,  109.,  109.,  107.,  113.,  117.,
        119.,  115.,  118.,  124.,  128.,  130.,  135.,  139.,  147.,
        149.,  152.,  159.,  167.,  178.,  181.,  184.,  192.,  209.,
        216.,  208.,  205.,  209.,  209.,  215.,  216.,  214.,  216.,
        221.,  225.,  223.,  222.,  224.,  226.,  223.,  221.,  219.,
        217.,  217.,  212.,  208.,  198.,  192.,  190.,  183.,  180.,
        183.,  183.,  180.,  181.,  176.,  169.,  156.,  149.,  138.,
        138.,  134.,  129.,  123.,  119.,  118.,  117.,  114.,  110.,
        104.,  101.,  107.,  104.,  100.,   94.,   97.,   96.,   93.,
         99.,   99.,   96.,   95.,  100.,  101.,   98.,  101.,   93.,
         91.,   89.,   81.,   77.,   77.,   78.,   75.,   70.,   71.,
         77.,   81.,   73.,   75.,   77.,   82.,   80.,   77.,   76.,
         75.,   74.,   74.,   78.,   82.,   80.,   79.,   82.,   83.,
         82.,   81.,   82.,   78.,   75.,   79.,   82.,   81.,   82.,
         82.,   82.,   83.,   87.,   87.,   91.,   92.,   91.,   89.,
         91.,   84.,   79.,   79.,   73.,   72.,   67.,   65.,   61.,
         59.,   58.,   63.,   67.,   67.,   72.])

In [44]:
adjusted_profile = profile/255 * 1e-9

In [45]:
plt.plot(adjusted_profile)


Out[45]:
[<matplotlib.lines.Line2D at 0x11f6df710>]

In [ ]:
# here are some functions!
def afm_copier(afmimg):
    '''this makes a copy of the snipped and backgrounded afm image'''
    image_copy = np.copy(afmimg)
    return image_copy

In [ ]:
def contour_finder(copied_image):
    '''Here we use a threshold to find the contours
    of the selected image.'''
    # now determine the threshold - we choose half the max intensity
    ret,thresh = cv2.threshold(copied_image,130,255,cv2.THRESH_TOZERO)
    # this determines the contours
    dummy, contours, hierarchy =cv2.findContours(thresh,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    return contours

In [ ]:
def convex_hull_determiner(afmimg,contours):
    '''This uses the calculated contours to determine
    the existence of a convex hull (i.e. a nanowire)'''
    # This turns the contours into an array of points
    cnts = max(contours, key=cv2.contourArea)
    # now we determine whether or not there is a hull
    hull = cv2.convexHull(cnts)
    hull = np.reshape(hull,(len(hull),2))
    return cnts, hull

In [ ]:
def draw_line(cnts,afmimg,copied_image):
    '''Here we use the convex hull to fit a line along
    the nanowire, and then switch the indeces so that
    the line lies along the short axis of the wire'''
    rows,cols = afmimg.shape[:2]
    # Here we fit a line along the nanowire
    [line_x, line_y, int_x, int_y] = cv2.fitLine(cnts,distType=2,param=0,reps=0.01,aeps=0.01)
    #this determines the left and right side endpoints of the line...
    lefty = int((-int_x*line_y/line_x) + int_y)
    righty = int(((cols-int_x)*line_y/line_x)+int_y)
    # ...but we inverse the indeces, thus generating a perpendular line
    wire_with_line = cv2.line(copied_image,(righty,0),(lefty,cols-1),(0,255,0),2)
    return wire_with_line,righty,lefty,cols

In [ ]:
def wire_profile(afmimg,righty,lefty,cols):
    '''Now we compute the profile and adjust to real units'''
    profile = skimage.measure.profile_line(afmimg,(righty,0),(lefty,cols-1))
    return profile