def detect_NCC_pos(nuclei, thickness):

    IJ.selectWindow("NCC")
    imp = IJ.getImage()
    IJ.run("Remove Overlay", "")
    IJ.run("Overlay Options...", "stroke=green width=0 fill=green set")
    IJ.run("Set Measurements...", "area mean redirect=NCC decimal=3")
    pos_idx = []
    neg_idx = []
    idx = 0
    #IJ.log("nuclei :"+str(len(nuclei)))
    IJ1.batchMode = True
    for idx in nuclei:
        #IJ.log("idx: "+str(idx))
        ncc_roi = rm.select(idx)
        IJ.run("Make Band...", "band=" + str(thickness))
        #rm.runCommand("Update")
        if IJ.getValue(wm.getImage("NCC"), "Mean") > ncc_threshold:
            pos_idx.append(idx)
            rm.rename(idx, str(idx) + "-NCC-POS")
            IJ.run(imp, "Add Selection...", "")
            IJ.run(imp, "Select None", "")
        else:
            neg_idx.append(idx)
            rm.rename(idx, str(idx) + "-NCC-NEG")
    IJ1.batchMode = False

    IJ.run("Show Overlay", "")
    IJ.log("Number of NCC+ cells: " + str(len(pos_idx)))
    IJ.log("Number of NCC- cells: " + str(len(neg_idx)))
    return pos_idx
def detect_MR_pos(nuclei):

    IJ.selectWindow("MR")
    IJ.run("Remove Overlay", "")
    IJ.run("Overlay Options...", "stroke=red width=0 fill=red set")
    IJ.run("Set Measurements...", "area mean redirect=MR decimal=3")
    pos_idx = []
    neg_idx = []
    idx = 0

    for idx in nuclei:
        rm.select(idx)
        MR_mean = IJ.getValue(wm.getImage("MR"), "Mean")
        if MR_mean > mr_threshold:
            pos_idx.append(idx)
            rm.rename(idx, str(idx) + "-MR-POS")
            IJ.run(IJ.getImage(), "Add Selection...", "")
        else:
            neg_idx.append(idx)
            rm.rename(idx, str(idx) + "-MR-NEG")

    IJ.run("Show Overlay", "")
    IJ.log("Number of MR+ cells: " + str(len(pos_idx)))
    IJ.log("Number of MR- cells: " + str(len(neg_idx)))
    return pos_idx
def filter_nuclei():

    area_idx = []
    area = 0
    stardist_rois = rm.getCount()
    IJ.run("Set Measurements...", "area mean redirect=NCC decimal=3")
    IJ.selectWindow("Nuclei")
    rm.runCommand("Show All without labels")
    IJ.selectWindow("NCC")

    for nuclei in range(0, stardist_rois):
        roi = rm.select(nuclei)
        area = IJ.getValue(wm.getImage("NCC"), "Area")
        if ((area > min_nucleus_area) and (area < max_nucleus_area)):
            area_idx.append(nuclei)
        else:
            rm.rename(nuclei, str(nuclei) + "-Excluded_(area)")
    IJ.log("Number of cells fitting area criteria: " + str(len(area_idx)))
    return area_idx
    background_auto = 0
    # defines the golgi and background at this point.
    if man_bck_det is True:
    	background_auto = low()
    else:
    	background= low()
    	
    #try:
    #    background
    #except NameError:
    #    background = low()
    #else:
    #    background_auto = low()

    if mean_max_det is True:
        cell_area = IJ.getValue(imp, "Area")
        bck_int = []
        mean_int = []
        maximum_int = []
        for channel in channel_iterator:
            channel_first = int(channel)
            IJ.setSlice(channel_first)
            # Data Collection
            if man_bck_det is True:
            	bck_int=man_bck_list
            else:
            	bck_int.append(low())  # background
            mean_int.append(selection_mean())
            maximum_int.append(high(bit_depth))
        IJ.run("Select None")
        to_transport_channel()