Esempio n. 1
0
def create_skeleton(image, name, min_branch_length=10, nuclei_rois=None, sample_width=4):
	print "Creating skeleton for %s, new name %s" %(image.getTitle(), name)
	# analyze skeleton
	skel = AnalyzeSkeleton_()
	skel.setup("", image)
	skelResult = skel.run(AnalyzeSkeleton_.NONE, False, True, None, True, False)
	
	# create copy of input image
	pruned_img = image.duplicate()
	outStack = pruned_img.getStack()
	
	# get graphs (one per skeleton in the image)
	graph = skelResult.getGraph()
	
	# list of end-points
	endPoints = skelResult.getListOfEndPoints()

	if graph:
		for i in range(len(graph)):
			listEdges = graph[i].getEdges()
			# go through all branches and remove branches < min_branch_length in duplicate image
			for  e in listEdges:
				p = e.getV1().getPoints();
				v1End = endPoints.contains( p.get(0) )
				p2 = e.getV2().getPoints();
				# print "p=",p, "p2=",p2
				v2End = endPoints.contains( p2.get(0) )
				# if any of the vertices is end-point 
				if v1End or v2End :
					if e.getLength() < min_branch_length:
						if v1End:
							outStack.setVoxel( p.get(0).x, p.get(0).y, p.get(0).z, 0 )
						if v2End:
							outStack.setVoxel( p2.get(0).x, p2.get(0).y, p2.get(0).z, 0 )
						for p in e.getSlabs():
							outStack.setVoxel( p.x, p.y, p.z, 0 )
	pruned_img.setTitle(image.getTitle()+"-longestpath")

	sppoints = skel.getShortestPathPoints()
	if len(sppoints) == 0:
		return None, None, None, None, None, None

	ais_skeleton = pruned_img.duplicate()
	ais_skeleton.setTitle(name);
	IJ.run(ais_skeleton, "Select All", "")
	IJ.run(ais_skeleton, "Clear", "slice")
	points = []
	angle = []
	b_length = [len(b) for b in sppoints]
	longest_branch_idx = b_length.index(max(b_length))
	points = [p for p in sppoints[longest_branch_idx]]
	closest_nucleus = None
	if nuclei_rois is not None:
		nroi1,dist1 = find_closest_roi(points[0].x, points[0].y, nuclei_rois)
		nroi2,dist2 = find_closest_roi(points[len(points)-1].x, points[len(points)-1].y, nuclei_rois)
		if nroi1 != nroi2 and dist2<dist1:
			# reverse order
		    points = points[::-1]
		    closest_nucleus = nroi2
		else:
		    closest_nucleus = nroi1
		closest_nucleus.setName('%s-nucleus-ROI' % (name))


	poly = Polygon()
	for p in points:
		poly.addPoint(int(p.x), int(p.y))

	#for branch in sppoints:
	#	print "Branch %s, len=%d" % (branch, len(branch))
	#	for p in branch:
	#		poly.addPoint(int(p.x), int(p.y))
	#		points.append(p)
	angles,orthogonals = local_angles(points, scope=sample_width//2)
	ais_roi = PolygonRoi(poly, PolygonRoi.POLYLINE)
	ais_roi.setFillColor(Color(0.0,1.0,1.0,0.5));
	ais_roi.setStrokeColor(Color(0.0,1.0,1.0,0.5));
	ais_roi.setName('%s-AIS-ROI' % (name))
	#ais_roi.setStrokeWidth(sample_width)
	IJ.run(ais_skeleton, "Analyze Particles...", "size=20-Infinity pixel exclude clear add");
	IJ.run(ais_skeleton, "Clear", "slice")

	ip = ais_skeleton.getProcessor()
	for n in nuclei_rois:
		ais_skeleton.setRoi(n)
		if n == closest_nucleus:
			ip.setValue(128)
		else:
			ip.setValue(255)		
		ip.fill(n.getMask())
		
	ais_skeleton.setRoi(ais_roi)
	#ip.setValue(200)
	#ip.fill()
	
	#rois = RoiManager.getInstance2().getRoisAsArray()
	#for roi in rois:
	#	ais_skeleton.setRoi(roi)
	#	ip.setValue(255)
	#	ip.fill(roi.getMask());

	for p,a,o in zip(points,angles,orthogonals):
		# print "p=%s, a=%s, o=%s" % (p,a,o)
		ip.set(int(p.x), int(p.y), int(33+a/2))
		ip.set(int(o[0].x), int(o[0].y),255)
		ip.set(int(o[1].x), int(o[1].y),255)

	IJ.run(ais_skeleton, "Fire", "")
	# pruned_img.setRoi(ais_roi)
	print "Created ais=%s" % ais_skeleton.getTitle()
	print len(skel.getShortestPathPoints()), len(points), len(orthogonals)
	return ais_skeleton, skel.getShortestPathPoints(), points, orthogonals, ais_roi, closest_nucleus
		if first:
			IJ.run("Remove Overlay", "")
		imp = IJ.getImage()
		channel = max(1, c);
		imp.setC(c);
		width = imp.getWidth()
		height = imp.getHeight()
		with open(aFile, 'r') as textfile:
			for line in textfile:
				xy = map(int, line.rstrip().split(','))
				X = xy[::2]
				Y = xy[1::2]
				roi = PolygonRoi(X, Y, Roi.POLYGON)	
				if EXCLUDE_ON_EDGES:
					bounds = roi.getBounds()
					if (bounds.x<DISTANCE_TO_BORDER or bounds.y<DISTANCE_TO_BORDER or bounds.x+bounds.width>width-DISTANCE_TO_BORDER or bounds.y+bounds.height>height-DISTANCE_TO_BORDER):
						continue
				imp.setRoi(roi)
				roi.setStrokeColor(Color.yellow)
				roi = imp.getRoi()
				roi.setGroup(g)
				rm.addRoi(roi)
		rm.runCommand("Associate", "true")	 
		rm.runCommand("Show All with labels")		
		IJ.run("From ROI Manager", "")
		imp.getOverlay().setStrokeColor(COLORS[g-1])
		rm.reset()
		IJ.save(imp, imageFile)
		imp.close()
	c = c + 1	
	first = False