Skip to content

Instantly share code, notes, and snippets.

@hirokai
Created March 15, 2014 20:55
Show Gist options
  • Save hirokai/9573785 to your computer and use it in GitHub Desktop.
Save hirokai/9573785 to your computer and use it in GitHub Desktop.
Nanodot spacing analysis
import httplib, urllib, marshal
from math import floor
def getVal(table, i):
x = table.getValue('X', i)
y = table.getValue('Y', i)
return [x, y]
def draw_triangles(imp, triangles):
ip = imp.getProcessor()
for tri in triangles:
ip.drawLine(int(tri[0]),
int(tri[1]),
int(tri[2]),
int(tri[3]))
# Use a remote server for calculating Delaunay triangulation
def delaunay(coords):
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
params = urllib.urlencode({'data': coords, 'json': 0})
host = "groveslab.cchem.berkeley.edu:3000"
# host = "localhost:3000"
conn = httplib.HTTPConnection(host)
print "connected"
conn.request("POST", "/programming/delaunay", params, headers)
r = conn.getresponse()
# print r1.status, r1.reason
result = r.read()
ls = result.split("\n")
vs = [map(float,l.split(",")) for l in ls]
conn.close()
return vs
# imp is a thresholded image (binary image).
def find_dots(imp):
IJ.run(imp, "Set Measurements...", " centroid redirect=None decimal=2")
IJ.run(imp, "Analyze Particles...", "size=20-Infinity circularity=0.00-1.00 show=Ellipses display clear")
# Get a Results table. This content can be shown from Window -> Results
table = ResultsTable.getResultsTable()
# Get the number of rows.
n = table.getCounter()
# Call getVal to get every (x,y) coordinate.
return [getVal(table, i) for i in range(0, n)]
def main():
# path = "/Volumes/Macintosh HD/Dropbox/Groves Lab Data/SEM/130712 EML/20 80k.tiff"
imp = IJ.getImage()
th_imp = threshold(imp)
coords = find_dots(th_imp)
imp_ps = IJ.getImage()
triangles = delaunay(coords)
# print triangles
draw_triangles(imp_ps,triangles)
# Show an original image and a thresholded image.
imp_ps.repaintWindow()
# th_imp.show()
print "Done"
def threshold(imp):
imp2 = imp.duplicate()
ip2 = imp2.getProcessor()
IJ.run(imp2, "Auto Threshold", "method=RenyiEntropy white")
IJ.run(imp2, "Convert to Mask", "")
return imp2
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment