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])
In [4]:
plt.imshow(afmdata[100:300,400:600])
Out[4]:
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]:
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]:
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')
In [41]:
profile = ski.profile_line(afmimg,(righty,0),(lefty,cols-1))
In [42]:
plt.plot(profile)
Out[42]:
In [43]:
profile
Out[43]:
In [44]:
adjusted_profile = profile/255 * 1e-9
In [45]:
plt.plot(adjusted_profile)
Out[45]:
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