50,531
社区成员
发帖
与我相关
我的任务
分享
var greyVal;
for(greyVal=2000;greyVal<12000;greyVal+=500)
{
run("Image Sequence...", "open=[C:\\Users\\microway\\Desktop\\blender&fuji sources\\00001.tif] number=150 starting=1 increment=1 scale=100 file=[] or=[] sort");
run("Properties...", "channels=1 slices=150 frames=1 unit=pixel pixel_width=1.0000 pixel_height=1.0000 voxel_depth=5 frame=[0 sec] origin=0,0");
setAutoThreshold("Default");
//run("Threshold...");
setAutoThreshold("Default");
setThreshold(greyVal, greyVal+500);
run("Convert to Mask", " black");
//setTool("hand");
run("3D Viewer");
call("ij3d.ImageJ3DViewer.setCoordinateSystem", "false");
call("ij3d.ImageJ3DViewer.add", "blender&fuji sources", "None", "blender&fuji sources", "50", "true", "true", "true", "2", "2");
//应该是在这里加入那一行代码..导出.stl(binary)文件
close();
call("ij3d.ImageJ3DViewer.close");
}
public static void saveAsSTL(Collection meshgroups, int filetype) {
if (null == meshgroups || 0 == meshgroups.size())
return;
meshgroups = filterMeshes(meshgroups);
if (0 == meshgroups.size()) {
IJ.log("No meshes to export!");
return;
}
SaveDialog sd = new SaveDialog("Save as STL ("
+ ((filetype == ASCII) ? "ASCII" : "binary") + ")",
"untitled", ".stl");
String dir = sd.getDirectory();
if (null == dir)
return;
if (IJ.isWindows())
dir = dir.replace('\\', '/');
if (!dir.endsWith("/"))
dir += "/";
String stl_filename = sd.getFileName();
if (!stl_filename.toLowerCase().endsWith(".stl"))
stl_filename += ".stl";
File stl_file = new File(dir + "/" + stl_filename);
// check if file exists
if (!IJ.isMacOSX()) {
if (stl_file.exists()) {
YesNoCancelDialog yn = new YesNoCancelDialog(IJ.getInstance(),
"Overwrite?", "File " + stl_filename
+ " exists!\nOverwrite?");
if (!yn.yesPressed())
return;
}
}
OutputStreamWriter dos = null;
DataOutputStream out = null;
try {
if (filetype == ASCII) {
dos = new OutputStreamWriter(new BufferedOutputStream(
new FileOutputStream(stl_file)), "8859_1");
writeAsciiSTL(meshgroups, dos, stl_filename);
dos.flush();
} else {
out = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(stl_file)));
writeBinarySTL(meshgroups, out);
out.flush();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (null != dos)
dos.close();
if (null != out)
out.close();
} catch (Exception e) {
}
}
}
private static void writeBinarySTL(Collection meshgroups,
DataOutputStream out) {
// get all the meshes and sort them into a hash
HashMap<String, CustomMesh> meshes = new HashMap<String, CustomMesh>();
for (Iterator<Content> it = meshgroups.iterator(); it.hasNext();) {
Content mob = (Content) it.next();
ContentNode node = mob.getContent();
// First CustomMultiMesh, which is also a CustomMeshNode:
if (node instanceof CustomMultiMesh) {
CustomMultiMesh multi = (CustomMultiMesh) node;
for (int i = 0; i < multi.size(); i++) {
meshes.put(mob.getName() + " [" + (i + 1) + "]", multi
.getMesh(i));
}
// Then CustomMeshNode (all custom meshes):
} else if (node instanceof CustomMeshNode) {
meshes
.put(mob.getName(), ((CustomMeshNode) node)
.getMesh());
// An image volume rendered as isosurface:
} else if (node instanceof MeshGroup) {
meshes.put(mob.getName(), ((MeshGroup) node).getMesh());
} else {
IJ.log("Ignoring " + mob.getName() + " with node of class "
+ node.getClass());
continue;
}
}
//count all the triangles and add them to a list
int triangles = 0;
ArrayList<List<Point3f>> surfaces = new ArrayList<List<Point3f>>();
for (String name : meshes.keySet()) {
CustomMesh cmesh = meshes.get(name);
if (cmesh.getClass() == CustomQuadMesh.class) {
IJ.log("Quad meshes are unsupported, can't save " + name
+ " as STL");
continue;
} else if (cmesh.getClass() != CustomTriangleMesh.class) {
IJ.log("Unsupported content type, can't save " + name
+ " as STL");
continue;
}
List<Point3f> vertices = cmesh.getMesh();
triangles += vertices.size() / 3;
surfaces.add(vertices);
}
String header = "Binary STL created by ImageJ 3D Viewer.";
for (int i = header.length(); i < 80; i++){
header = header+".";
}
try {
out.writeBytes(header);
out.writeByte(triangles & 0xFF);
out.writeByte((triangles >> 8) & 0xFF);
out.writeByte((triangles >> 16) & 0xFF);
out.writeByte((triangles >> 24) & 0xFF);
for (List<Point3f> vertices : surfaces){
for (int i = 0; i < vertices.size(); i+=3){
Point3f p0 = vertices.get(i);
Point3f p1 = vertices.get(i+1);
Point3f p2 = vertices.get(i+2);
Point3f n = unitNormal(p0, p1, p2);
ByteBuffer bb = ByteBuffer.allocate(50);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.putFloat(n.x);
bb.putFloat(n.y);
bb.putFloat(n.z);
bb.putFloat(p0.x);
bb.putFloat(p0.y);
bb.putFloat(p0.z);
bb.putFloat(p1.x);
bb.putFloat(p1.y);
bb.putFloat(p1.z);
bb.putFloat(p2.x);
bb.putFloat(p2.y);
bb.putFloat(p2.z);
bb.putShort((short)0);
out.write(bb.array());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}