def qc_background_regions(intensity_imp, bg_rois):
	"""allow the user to view and correct automatically-determined background regions"""
	imp = Duplicator().run(intensity_imp);
	imp.setTitle("Background region QC");
	imp.show();
	imp.setPosition(1);
	autoset_zoom(imp);
	imp.setRoi(bg_rois[0]);
	IJ.setTool("freehand");
	
	notOK = True;
	while notOK:
		listener = UpdateRoiImageListener(bg_rois, is_area=True);
		imp.addImageListener(listener);
		dialog = NonBlockingGenericDialog("Background region quality control");
		dialog.enableYesNoCancel("Continue", "Use this region for all t");
		dialog.setCancelLabel("Cancel analysis");
		dialog.addMessage("Please redraw background regions as necessary...")
		dialog.showDialog();
		if dialog.wasCanceled():
			raise KeyboardInterrupt("Run canceled");
		elif not(dialog.wasOKed()):
			this_roi = imp.getRoi();
			bg_rois = [this_roi for _ in listener.getRoiList()];
			imp.removeImageListener(listener);
		else:
			last_roi = imp.getRoi();
			qcd_bg_rois = listener.getRoiList();
			if imp.getNFrames() > imp.getNSlices():
				qcd_bg_rois[imp.getT() - 1] = last_roi;
			else:
				qcd_bg_rois[imp.getZ() - 1] = last_roi;
			notOK = False;
	imp.removeImageListener(listener);
	imp.changes = False;
	imp.close();
	
	return qcd_bg_rois;
def generate_background_rois(input_mask_imp,
                             params,
                             membrane_edges,
                             dilations=5,
                             threshold_method=None,
                             membrane_imp=None):
    """automatically identify background region based on auto-thresholded image, existing membrane edges and position of midpoint anchor"""
    if input_mask_imp is None and membrane_imp is not None:
        segmentation_imp = Duplicator().run(membrane_imp)
        # do thresholding using either previous method if threhsold_method is None or using (less conservative?) threshold method
        if (threshold_method is None
                or not (threshold_method in params.listThresholdMethods())):
            mask_imp = make_and_clean_binary(segmentation_imp,
                                             params.threshold_method)
        else:
            mask_imp = make_and_clean_binary(segmentation_imp,
                                             threshold_method)
        segmentation_imp.close()
    else:
        input_mask_imp.killRoi()
        mask_imp = Duplicator().run(input_mask_imp)

    rois = []
    IJ.setForegroundColor(0, 0, 0)
    roim = RoiManager(True)
    rt = ResultsTable()

    for fridx in range(mask_imp.getNFrames()):
        mask_imp.setT(fridx + 1)
        # add extra bit to binary mask from loaded membrane in case user refined edges...
        # flip midpoint anchor across the line joining the two extremes of the membrane,
        # and fill in the triangle made by this new point and those extremes
        poly = membrane_edges[fridx].getPolygon()
        l1 = (poly.xpoints[0], poly.ypoints[0])
        l2 = (poly.xpoints[-1], poly.ypoints[-1])
        M = (0.5 * (l1[0] + l2[0]), 0.5 * (l1[1] + l2[1]))
        Mp1 = (params.manual_anchor_midpoint[0][0] - M[0],
               params.manual_anchor_midpoint[0][1] - M[1])
        p2 = (M[0] - Mp1[0], M[1] - Mp1[1])
        new_poly_x = list(poly.xpoints)
        new_poly_x.append(p2[0])
        new_poly_y = list(poly.ypoints)
        new_poly_y.append(p2[1])
        mask_imp.setRoi(PolygonRoi(new_poly_x, new_poly_y, PolygonRoi.POLYGON))
        IJ.run(mask_imp, "Fill", "slice")
        mask_imp.killRoi()

        # now dilate the masked image and identify the unmasked region closest to the midpoint anchor
        ip = mask_imp.getProcessor()
        dilations = 5
        for d in range(dilations):
            ip.dilate()
        ip.invert()
        mask_imp.setProcessor(ip)
        mxsz = mask_imp.getWidth() * mask_imp.getHeight()
        pa = ParticleAnalyzer(
            ParticleAnalyzer.ADD_TO_MANAGER | ParticleAnalyzer.SHOW_PROGRESS,
            ParticleAnalyzer.CENTROID, rt, 0, mxsz)
        pa.setRoiManager(roim)
        pa.analyze(mask_imp)
        ds_to_anchor = [
            math.sqrt((x - params.manual_anchor_midpoint[0][0])**2 +
                      (y - params.manual_anchor_midpoint[0][1])**2)
            for x, y in zip(
                rt.getColumn(rt.getColumnIndex("X")).tolist(),
                rt.getColumn(rt.getColumnIndex("Y")).tolist())
        ]
        if len(ds_to_anchor) > 0:
            roi = roim.getRoi(ds_to_anchor.index(min(ds_to_anchor)))
            rois.append(roi)
        else:
            rois.append(None)
        roim.reset()
        rt.reset()
    roim.close()
    mask_imp.close()
    return rois
Пример #3
0
def ColMigBud():
    def setupDialog(imp):

        gd = GenericDialog("Collective migration buddy options")
        gd.addMessage("Collective migration buddy 2.0, you are analyzing: " +
                      imp.getTitle())
        calibration = imp.getCalibration()

        if (calibration.frameInterval > 0):
            default_interval = calibration.frameInterval
            default_timeunit = calibration.getTimeUnit()

        else:
            default_interval = 8
            default_timeunit = "min"

        gd.addNumericField("Frame interval:", default_interval,
                           2)  # show 2 decimals
        gd.addCheckbox("Do you want to use a gliding window?", True)
        gd.addCheckbox(
            "Project hyperStack? (defaluts to projecting current channel only)",
            False)
        gd.addStringField("time unit", default_timeunit, 3)
        gd.addSlider("Start compacting at frame:", 1, imp.getNFrames(), 1)
        gd.addSlider("Stop compacting at frame:", 1, imp.getNFrames(),
                     imp.getNFrames())
        gd.addNumericField("Number of frames to project in to one:", 3,
                           0)  # show 0 decimals

        gd.addChoice('Method to use for frame projection:', methods_as_strings,
                     methods_as_strings[5])

        gd.showDialog()

        if gd.wasCanceled():
            IJ.log("User canceled dialog!")
            return

        return gd

    #Start by getting the active image window and get the current active channel and other stats
    imp = WindowManager.getCurrentImage()
    cal = imp.getCalibration()
    nSlices = 1  #TODO fix this in case you want to do Z-stacks
    title = imp.getTitle()
    current_channel = imp.getChannel()
    #zp = ZProjector(imp)

    #Make a dict containg method_name:const_fieled_value pairs for the projection methods
    methods_as_strings = [
        'Average Intensity', 'Max Intensity', 'Min Intensity', 'Sum Slices',
        'Standard Deviation', 'Median'
    ]
    methods_as_const = [
        ZProjector.AVG_METHOD, ZProjector.MAX_METHOD, ZProjector.MIN_METHOD,
        ZProjector.SUM_METHOD, ZProjector.SD_METHOD, ZProjector.MEDIAN_METHOD
    ]
    medthod_dict = dict(zip(methods_as_strings, methods_as_const))

    # Run the setupDialog, read out and store the options
    gd = setupDialog(imp)
    frame_interval = gd.getNextNumber()
    time_unit = gd.getNextString()
    glidingFlag = gd.getNextBoolean()
    hyperstackFlag = gd.getNextBoolean()

    #Set the frame interval and unit, and store it in the ImagePlus calibration
    cal.frameInterval = frame_interval
    cal.setTimeUnit(time_unit)
    imp.setCalibration(cal)

    start_frame = int(gd.getNextNumber())
    stop_frame = int(gd.getNextNumber())

    #If a subset of the image is to be projected, these lines of code handle that
    if (start_frame > stop_frame):
        IJ.showMessage("Start frame > Stop frame, can't go backwards in time!")
        raise RuntimeException("Start frame > Stop frame!")

    if ((start_frame != 1) or (stop_frame != imp.getNFrames())):
        imp = Duplicator().run(imp, 1, nChannels, 1, nSlices, start_frame,
                               stop_frame)

    no_frames_per_integral = int(gd.getNextNumber())

    #the doHyperstackProjection method can't project past the end of the stack
    if hyperstackFlag:
        total_no_frames_to_project = imp.getNFrames() - no_frames_per_integral
    #the doProjection method can project past the end, it just adds black frames at the end
    #When not projecting hyperstacks, just copy the current active channel from the active image
    else:
        total_no_frames_to_project = imp.getNFrames()
        imp = Duplicator().run(imp, current_channel, current_channel, 1,
                               nSlices, start_frame, stop_frame)

    #The Z-Projection magic happens here through a ZProjector object
    zp = ZProjector(imp)
    projection_method = gd.getNextChoice()
    chosen_method = medthod_dict[projection_method]
    zp.setMethod(chosen_method)
    outstack = imp.createEmptyStack()

    if glidingFlag:
        frames_to_advance_per_step = 1
    else:
        frames_to_advance_per_step = no_frames_per_integral

    for frame in range(1, total_no_frames_to_project,
                       frames_to_advance_per_step):
        zp.setStartSlice(frame)
        zp.setStopSlice(frame + no_frames_per_integral)
        if hyperstackFlag:
            zp.doHyperStackProjection(False)
            projected_stack = zp.getProjection().getStack()
            for channel in range(projected_stack.getSize()):
                outstack.addSlice(projected_stack.getProcessor(channel + 1))
        else:
            zp.doProjection()
            outstack.addSlice(zp.getProjection().getProcessor())

    #Create an image processor from the newly created Z-projection stack
    nChannels = imp.getNChannels()
    nFrames = outstack.getSize() / nChannels
    imp2 = ImagePlus(
        title + '_' + projection_method + '_' + str(no_frames_per_integral) +
        '_frames', outstack)
    imp2 = HyperStackConverter.toHyperStack(imp2, nChannels, nSlices, nFrames)
    imp2.show()

    Startmenu()