The function is here:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_alpha_shape(infile,radius): | |
command = "%s -A -aa %s -r -m10000000 -oN -oFpoints < %s" % (hull_path, str(radius), infile) | |
print >> sys.stderr, "Running command: %s" % command | |
retcode = subprocess.call(command, shell=True) | |
results_file = open("points-alf") | |
results_file.next() | |
results_indices = [[int(i) for i in line.rstrip().split()] for line in results_file] | |
results_file.close() | |
return results_indices |
The above is essentially the same wrapper as posted here, except a different way of reading in the data, and providing the option to specify a probe radius. It uses Ken Clarkson's C-code, instructions on how to compile are here
An example implementation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t = linspace(0.6,5.7,500) | |
C = 2*np.vstack([cos(t),sin(t)] ) | |
C=C+np.random.rand(2,500) |
The above data generation is translated from the example in a matlab alpha shape function . Then:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hull_path = "./hull" | |
infile='C.txt' | |
with open(infile, 'w') as f: | |
np.savetxt(f, C.T, delimiter=' ', fmt="%0.7f %0.7f\n") | |
radius=1 | |
indices = get_alpha_shape(infile,radius) | |
alpha=C.T[indices] | |
plot(C[0],C[1],'.'), plot(alpha[1:].T[0],alpha[1:].T[1],'r') | |
savefig("C_radius="+str(radius)+".png") | |
close() |
Which produces (points are blue dots, alpha shape is red line):
and on a larger data set (with radius=1, this is essentially the convex hull):
and a true concave hull:
No comments:
Post a Comment