def RoiSelection(index): imp_roi = IJ.getImage() #IJ.run("Invert", "stack"); IJ.run("Fill Holes", "stack"); IJ.run("Create Selection"); rm.runCommand("add") rm.runCommand("split") #number_selected=rm.getCount() IJ.run("Select None"); rm.runCommand("deselect") #rm.select(0) #print number_selected roi_array=rm.getRoisAsArray() max_roi=None max_points=-1 for roi in roi_array: polygon=roi.getPolygon() if polygon is not None: x=[] y=[] for i in range(1,polygon.npoints): x.append(polygon.xpoints[i]) y.append(polygon.ypoints[i]) area=PolyArea(x,y,polygon.npoints) if max_points < area: max_points=area max_roi=roi #print max_points #sorted_roi_array=sorted(roi_array, key=methodcaller('getLength'), reverse=True) #length_array=[] #index=0 #for roi in roi_array: # index=index+1 # length_array.append((index,roi.getLength())) #sorted_length_array=sorted(length_array, key=itemgetter(0)) rm.runCommand("Select All") rm.runCommand("Delete") #for roi in roi_array: interpolated_polygon=max_roi.getInterpolatedPolygon(20,True) roi_polygon=PolygonRoi(interpolated_polygon,Roi.POLYGON) roi_polygon.setName(str(index)) roi_polygon.setImage(imp) roi_polygon.setPosition(index) regions_array.append(roi_polygon)
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