def drawLines(imp, points=None): if points and (len(points) % 2 == 0): # points is numeric list of even length pRoi = PointRoi(points[0::2], points[1::2], len(points) / 2) pRoi.setShowLabels(True) pRoi.setSize(3) imp.setRoi(pRoi) roi = imp.getRoi() pp = roi.getFloatPolygon() # print "Added", pp.npoints if pp.npoints <= 1: # don't draw if only one point return xys = [] for i in xrange(pp.npoints): xys.append([pp.xpoints[i], pp.ypoints[i]]) ol = Overlay() x0 = xys[0][0] y0 = xys[0][1] cal = imp.getCalibration() for i in xrange(1, pp.npoints): xi = xys[i][0] yi = xys[i][1] # prepare text label d = math.sqrt((xi - x0)**2 + (yi - y0)**2) * cal.pixelWidth dText = String.format("%.2f ", d) + cal.getUnits() textOffset = 30 xt = xi yt = yi # if xi > x0: # xt += textOffset if xi < x0: xt -= textOffset # if yi > y0: # yt += textOffset if yi < y0: yt -= textOffset dTextRoi = TextRoi(xt, yt, dText) ol.add(dTextRoi) lineRoi = Line(x0, y0, xi, yi) lineRoi.setStrokeWidth(1) lineRoi.setStrokeColor(Color(255, 255, 0)) ol.add(lineRoi) imp.setOverlay(ol) imp.updateAndDraw()
def drawLines(imp, points=None): if points and (len(points)%2 == 0): # points is numeric list of even length pRoi = PointRoi(points[0::2], points[1::2], len(points)/2) pRoi.setShowLabels(True) pRoi.setSize(3) imp.setRoi(pRoi) roi = imp.getRoi() pp = roi.getFloatPolygon() # print "Added", pp.npoints if pp.npoints <= 1: # don't draw if only one point return xys = [] for i in xrange(pp.npoints): xys.append([pp.xpoints[i], pp.ypoints[i]]) ol = Overlay() x0 = xys[0][0] y0 = xys[0][1] cal = imp.getCalibration() for i in xrange(1, pp.npoints): xi = xys[i][0] yi = xys[i][1] # prepare text label d = math.sqrt((xi - x0)**2 + (yi - y0)**2) * cal.pixelWidth dText = String.format("%.2f ", d) + cal.getUnits() textOffset = 30 xt = xi yt = yi # if xi > x0: # xt += textOffset if xi < x0: xt -= textOffset # if yi > y0: # yt += textOffset if yi < y0: yt -= textOffset dTextRoi = TextRoi(xt, yt, dText) ol.add(dTextRoi) lineRoi = Line(x0, y0, xi, yi) lineRoi.setStrokeWidth(1) lineRoi.setStrokeColor(Color(255,255,0)) ol.add(lineRoi) imp.setOverlay(ol) imp.updateAndDraw()
def draw_scale(image, ticks): cal = image.getCalibration() overlay = Overlay() image.setOverlay(overlay) TextRoi.setGlobalJustification(TextRoi.CENTER) offset = image.getHeight() - extend for tick in ticks: tick_pos = cal.getRawX(tick) line = Line(tick_pos, offset, tick_pos, offset + font_size) line.setWidth(font_size // 8) line.setStrokeColor(Color(1.00, 1.00, 1.00)) overlay.add(line) text = TextRoi(tick_pos, offset + font_size, str(tick), font) text_width = text.getFloatWidth() text_y = text.getYBase() text.setLocation(tick_pos - text_width/2, text_y) text.setStrokeColor(Color(1.00, 1.00, 1.00)) overlay.add(text)
def getCells(dicStack): outStack = ImageStack(W,H) cells = [None for t in range(T+1)] for t in range(1,T+1): mapp = dicStack.getProcessor(t).convertToFloatProcessor() mapp.subtract( mapp.getStatistics().mean ) mapp.abs() RankFilters().rank(mapp, 1.0, RankFilters.VARIANCE) mapp.sqrt() mapp.blurGaussian(5) hist = mapp.getHistogram(256) stats = mapp.getStatistics() thresh = AutoThresholder().getThreshold( AutoThresholder.Method.Otsu, hist ) thresh = (thresh/float(255)) * (stats.max-stats.min) + stats.min mask = ByteProcessor(W,H) for i in range(W*H): value = mapp.getf(i) bite = 255 if value>=thresh else 0 mask.set(i, bite) fillHoles(mask) ed = 3 for e in range(ed): mask.erode(1, 0) for d in range(ed): mask.dilate(1, 0) watershed(mask) minA = 5000 #px² mask.setThreshold(255,255, ImageProcessor.NO_LUT_UPDATE) composite = ThresholdToSelection().convert(mask) rois = ShapeRoi(composite).getRois() keep = [] for roi in rois: if roi.getStatistics().area >= minA: if not onEdge(roi): keep.append(roi) else: edgeRoi = ShapeRoi(roi) edgeRoi.setPosition(0,0,t) edgeRoi.setStrokeColor(Color.YELLOW) ol.add(edgeRoi) print("T"+str(t)+" using "+str(len(keep))+"/"+str(len(rois))+" ROIs") rois = keep #rois = [ roi for roi in rois if roi.getStatistics().area >= minA and not onEdge(roi) ] #keep big enough and not on edges # if there is only one Roi, cut it along the fitted ellipse minor axis if len(rois)==1: el = EllipseFitter() mask.setRoi(rois[0]) el.fit(mask, None) el.makeRoi(mask) theta = el.angle * (maths.pi/180.0) length = el.major/2.0 dy = maths.sin(theta)* length dx = maths.cos(theta)* length #major axis lineX0 = el.xCenter - dx lineY0 = el.yCenter + dy lineX1 = el.xCenter + dx lineY1 = el.yCenter - dy line = Line(lineX0, lineY0, lineX1, lineY1) line.setStrokeColor(Color.BLUE) line.setStrokeWidth(1) line.setPosition(0,0,t) ol.add(line) #minor axis scaled length to make sure cut ends are outside Roi cutX0 = el.xCenter + dy*100 cutY0 = el.xCenter + dx*100 cutX1 = el.yCenter - dy*100 cutY1 = el.yCenter - dx*100 cut = Line(cutX0,cutY0, cutX1, cutY1) cut.setStrokeWidth(2) cut = PolygonRoi( cut.getFloatPolygon(), PolygonRoi.POLYGON ) mask.setColor(0) mask.fill(cut) composite = ThresholdToSelection().convert(mask) rois = ShapeRoi(composite).getRois() rois = [ roi for roi in rois if roi.getStatistics().area >= minA ] print(str(t) + ":" + str(len(rois))) rois = [ PolygonRoi(roi.getInterpolatedPolygon(20, True), PolygonRoi.POLYGON) for roi in rois ] rois = [ PolygonRoi(roi.getConvexHull(), PolygonRoi.POLYGON) for roi in rois ] rois = sorted(list(rois), key=lambda roi:roi.getLength() ) #size order rois = rois[-2:] #keep 2 biggest rois = sorted(list(rois), key=lambda roi:roi.getStatistics().xCentroid+roi.getStatistics().yCentroid ) #top left to bottom right order if len(rois)>0: rois[0].setStrokeColor(Color.RED) rois[0].setPosition(0, 0, t) ol.add(rois[0]) if len(rois)>1: rois[1].setStrokeColor(Color.GREEN) rois[1].setPosition(0, 0, t) ol.add(rois[1]) cells[t] = (rois[0], rois[1]) return cells
def main(imp,options): from ij.plugin import ChannelSplitter from ij.gui import Roi,PointRoi, PolygonRoi, Overlay, Line from java.awt import Color from ij import WindowManager from ij.measure import ResultsTable from ij.text import TextWindow active_z=imp.getZ() imps = ChannelSplitter.split(imp) imp.setZ(active_z) roi_int = imp.getRoi() comp_imp=Zproj(imps[options["comp_ch"]], "SUM", active_z, options["z_range"]) comp_imp=mode_subtract(comp_imp,roi_int) loci_imp=Zproj(imps[options["loci_ch"]], "SUM", imp.getZ(), options["z_range"]) loci_imp=mode_subtract(loci_imp,roi_int) #Finding the boundaries of compartment and loci comp_roi=thresh(sum_prj=comp_imp,thresh=options["comp_T"],roi=roi_int,method="boundary") print "ok" if (options["loci_method"]== "locus center"): loci_roi=thresh(sum_prj=loci_imp, thresh=options["loci_T"], roi=roi_int, method="point") elif options["loci_method"]== "locus boundary": loci_roi=thresh(sum_prj=loci_imp, thresh=options["loci_T"], roi=roi_int, method="boundary") if options["loci_method"]== "locus center": dist,xc,yc,xl,yl=get_center_edge_dist(imp,comp_roi, loci_roi) elif options["loci_method"]== "locus boundary": dist,xc,yc,xl,yl=get_closest_points(imp,comp_roi,loci_roi) rt_exist = WindowManager.getWindow("Loci distance to compartment") if rt_exist==None or not isinstance(rt_exist, TextWindow): table= ResultsTable() else: table = rt_exist.getTextPanel().getOrCreateResultsTable() table.incrementCounter() table.addValue("Label", imp.title) table.addValue("Distance(micron)", dist) if options['measure_feret']: feret_roi,loci_feret,loci_area= feret(sum_prj=loci_imp,thresh=options["loci_T"], roi=roi_int,pixel_size=imp.getCalibration().pixelWidth) table.addValue("Loci feret", loci_feret) table.addValue("Loci area", loci_area) table.show("Loci distance to compartment") ## Adding loci overlay ov=imp.getOverlay() if ov==None: ov=Overlay() line = Line(xc,yc, xl,yl) line.setStrokeWidth(0.2) line.setStrokeColor(Color.PINK) ov.add(line) if options["loci_method"]== "locus center": ov.add(PointRoi(loci_roi["x"],loci_roi["y"])) elif options["loci_method"]== "locus boundary": ov.add(loci_roi) if options['measure_feret']: ov.add(feret_roi) ov.add(comp_roi) imp.setOverlay(ov)