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;
Пример #2
0
def get_no_nuclei_fully_enclosed(roi, full_nuclei_imp, overlap_threshold=0.65):
    """for a given cell roi and ImagePlus with binary nuclei, return how many nuclei lie ENTIRELY within the cell"""
    bbox = roi.getBounds()
    full_nuclei_imp.setRoi(roi)
    cropped_nuc_imp = full_nuclei_imp.crop()
    roi.setLocation(0, 0)
    cropped_nuc_imp.setRoi(roi)
    cropped_nuc_imp.killRoi()
    roim = RoiManager(False)
    mxsz = cropped_nuc_imp.getWidth() * cropped_nuc_imp.getHeight()
    pa = ParticleAnalyzer(
        ParticleAnalyzer.ADD_TO_MANAGER, ParticleAnalyzer.AREA
        | ParticleAnalyzer.SLICE | ParticleAnalyzer.CENTROID, None, 0, mxsz)
    pa.setRoiManager(roim)
    pa.analyze(cropped_nuc_imp)
    cell_imp = IJ.createImage("Cell binary", cropped_nuc_imp.getWidth(),
                              cropped_nuc_imp.getHeight(), 1, 8)
    IJ.run(cell_imp, "Select All", "")
    IJ.run(cell_imp, "Set...", "value=0 slice")
    cell_imp.setRoi(roi)
    IJ.run(cell_imp, "Set...", "value=255 slice")
    no_enclosed_nuclei = 0
    for idx, nuc_roi in enumerate(roim.getRoisAsArray()):
        test_imp = Duplicator().run(cell_imp)
        test_imp.setRoi(nuc_roi)
        IJ.run(test_imp, "Set...", "value=255 slice")
        test_imp.killRoi()
        IJ.run(test_imp, "Create Selection", "")
        IJ.run(test_imp, "Make Inverse", "")
        test_roi = test_imp.getRoi()
        test_roi_stats = test_roi.getStatistics()
        cell_roi_stats = roi.getStatistics()
        nuc_roi_stats = nuc_roi.getStatistics()
        if test_roi_stats.area < (
                cell_roi_stats.area +
            (1 - overlap_threshold) * nuc_roi_stats.area
        ):  # i.e. if more than (100*overlap_threshold)% of nucleus is inside cell...
            no_enclosed_nuclei += 1
        test_imp.changes = False
        test_imp.close()
    roi.setLocation(bbox.getX(), bbox.getY())
    cropped_nuc_imp.changes = False
    cropped_nuc_imp.close()
    cell_imp.changes = False
    cell_imp.close()
    return no_enclosed_nuclei
Пример #3
0
class gui(JFrame):
    def __init__(self):  # constructor
        #origing of coordinates
        self.coordx = 10
        self.coordy = 10

        #inintialize values
        self.Canvas = None

        #create panel (what is inside the GUI)
        self.panel = self.getContentPane()
        self.panel.setLayout(GridLayout(9, 2))
        self.setTitle('Subdividing ROIs')

        #define buttons here:
        self.Dapi_files = DefaultListModel()
        mylist = JList(self.Dapi_files, valueChanged=self.open_dapi_image)
        #mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        mylist.setLayoutOrientation(JList.VERTICAL)
        mylist.setVisibleRowCount(-1)
        listScroller1 = JScrollPane(mylist)
        listScroller1.setPreferredSize(Dimension(300, 80))

        self.output_files = DefaultListModel()
        mylist2 = JList(self.output_files)
        #mylist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        mylist2.setLayoutOrientation(JList.VERTICAL)
        mylist2.setVisibleRowCount(-1)
        listScroller2 = JScrollPane(mylist2)
        listScroller2.setPreferredSize(Dimension(300, 80))

        quitButton = JButton("Quit", actionPerformed=self.quit)
        selectInputFolderButton = JButton("Select Input",
                                          actionPerformed=self.select_input)
        cubifyROIButton = JButton("Cubify ROI",
                                  actionPerformed=self.cubify_ROI)
        saveButton = JButton("Save ROIs", actionPerformed=self.save)
        summsaveButton = JButton("Save Summary",
                                 actionPerformed=self.save_summary)

        self.textfield1 = JTextField('500')

        #add buttons here
        self.panel.add(listScroller1)
        self.panel.add(listScroller2)
        self.panel.add(selectInputFolderButton)
        self.panel.add(Label("Adjust the size of the ROIs"))
        self.panel.add(self.textfield1)
        self.panel.add(cubifyROIButton)
        self.panel.add(saveButton)
        self.panel.add(summsaveButton)
        self.panel.add(quitButton)

        #self.panel.add(saveButton)
        #self.panel.add(Zslider)

        #other stuff to improve the look
        self.pack()  # packs the frame
        self.setVisible(True)  # shows the JFrame
        self.setLocation(self.coordx, self.coordy)

    #define functions for the buttons:

    def quit(self, event):  #quit the gui
        self.dispose()
        IJ.run("Close All")

    def select_input(self, event):
        self.input_path = IJ.getDirectory(
            "Select Directory containing your data")

        # get the files in that directory
        self.input_files = listdir(self.input_path)
        self.input_dapi = [s for s in self.input_files if "DAPI.tif" in s]
        self.core_names = get_core_names(self.input_dapi)

        # create output directory
        self.output_path = path.join(
            path.dirname(path.dirname(self.input_path)), "Cubified_ROIs")
        if path.isdir(self.output_path):
            print "Output path already created"
        else:
            mkdir(self.output_path)
            print "Output path created"
        # get the processed data
        self.processed_files = listdir(self.output_path)
        self.out_core_names = get_core_names(
            get_core_names(self.processed_files)
        )  # needs the channel and roi to be removed

        # populate the lists
        for f in set(self.core_names):
            if f not in set(self.out_core_names):  # skip if processed
                self.Dapi_files.addElement(f)
        for f in set(self.out_core_names
                     ):  # a set so that only unique ones are shown
            self.output_files.addElement(f)

    def open_dapi_image(self, e):
        sender = e.getSource()
        IJ.run("Close All")
        if not e.getValueIsAdjusting():
            self.name = sender.getSelectedValue()
            print self.name
            dapi_filename = path.join(self.input_path, self.name + '_DAPI.tif')
            #print dapi_filename
            if not path.exists(dapi_filename):
                print "I don't find the DAPI file, which is weird as I just found it before"
            else:
                #open stack and make the reslice
                self.dapi_image = ImagePlus(dapi_filename)  #read the image
                # duplicate image to play with that one, and do the real processing in the background
                self.imp_main = Duplicator().run(self.dapi_image)
                self.imp_main.setTitle(self.name)
                self.imp_main.show()  #show image on the left
                #self.original_image.getWindow().setLocation(self.coordx-self.original_image.getWindow().getWidth()-10,self.coordy-10) #reposition image

    def cubify_ROI(self, e):
        self.L = int(self.textfield1.text)
        # get info
        tit = self.imp_main.getTitle()
        self.roi = self.imp_main.getRoi()

        # get corners
        corners = get_corners(self.roi, self.L)
        self.corners_cleaned = clean_corners(corners, self.roi, self.L)
        print 'found corners'
        # get the overlay
        self.ov = overlay_corners(self.corners_cleaned, self.L)
        self.ov = overlay_roi(self.roi, self.ov)
        # write roi name
        self.ov = write_roi_numbers(self.ov, self.corners_cleaned, self.L)
        # overlay
        self.imp_main.setOverlay(self.ov)
        self.imp_main.updateAndDraw()

    def save(self, e):
        # open the other channels
        d1_filename = path.join(self.input_path, self.name + '_D1.tif')
        d2_filename = path.join(self.input_path, self.name + '_D2.tif')
        self.d1_image = ImagePlus(d1_filename)  #read the image
        print "d1 image opened"
        self.d2_image = ImagePlus(d2_filename)  #read the image
        print "d2 image opened"
        for image in [self.dapi_image, self.d1_image, self.d2_image]:
            print "saving rois for image " + image.getTitle()
            save_rois(image, self.corners_cleaned, self.L, self.output_path)
        print "ROIs saved"

    def save_summary(self, e):
        IJ.selectWindow(self.name)
        IJ.run("Flatten")
        imp = IJ.getImage()
        # downsample
        # scale ENLARGING or SHRINKING the canvas dimensions
        scale_factor = .2
        new_width = str(int(imp.getWidth() * scale_factor))
        new_height = str(int(imp.getHeight() * scale_factor))
        IJ.selectWindow(self.name + "-1")
        str_to_scale = "x=" + str(scale_factor) + " y=" + str(
            scale_factor
        ) + " width=" + new_width + " height=" + new_height + " interpolation=Bilinear average create"
        IJ.run("Scale...", str_to_scale)
        # save
        imp2 = IJ.getImage()
        #IJ.saveAs("Tiff", path.join(OUTDIR, self.name + "_summaryOfROIs"))
        IJ.saveAsTiff(
            imp2, path.join(self.output_path,
                            self.name + "_summaryOfROIs.tif"))
        print "summary image saved"
Пример #4
0
def do_angular_projection(imp,
                          max_r_pix=60,
                          min_r_pix=10,
                          generate_roi_stack=True):
    """perform ray-based projection of vessel wall, c.f. ICY TubeSkinner (Lancino 2018)"""
    Prefs.blackBackground = True
    print("do angular projection input imp = " + str(imp))
    split_chs = ChannelSplitter().split(imp)
    mch_imp = split_chs[0]
    IJ.setAutoThreshold(mch_imp, "IsoData dark stack")
    egfp_imp = split_chs[1]
    proj_imp = Duplicator().run(egfp_imp)
    cl_imp = split_chs[2]
    if generate_roi_stack:
        egfp_imp_disp = Duplicator().run(egfp_imp)
        roi_stack = IJ.createImage("rois", egfp_imp.getWidth(),
                                   egfp_imp.getHeight(), egfp_imp.getNSlices(),
                                   16)

    centres = []
    projected_im_pix = []
    ring_rois = []
    for zidx in range(cl_imp.getNSlices()):
        if ((zidx + 1) % 100) == 0:
            print("Progress = " +
                  str(round(100 * (float(zidx + 1) / cl_imp.getNSlices()))))
        projected_im_row = []
        proj_imp.setZ(zidx + 1)
        mch_imp.setZ(zidx + 1)
        bp = mch_imp.createThresholdMask()
        bp.dilate()
        bp.erode()
        bp.erode()
        bp.erode()
        mask_imp = ImagePlus("mask", bp)
        IJ.run(mask_imp, "Create Selection", "")
        roi = mask_imp.getRoi()
        proj_imp.setRoi(roi)
        IJ.run(proj_imp, "Set...", "value=0 slice")
        IJ.run(proj_imp, "Make Inverse", "")
        roi = proj_imp.getRoi()
        centre = (roi.getStatistics().xCentroid, roi.getStatistics().yCentroid)
        centres.append(centre)
        ring_roi_xs = []
        ring_roi_ys = []
        for theta in range(360):
            pt1 = (centre[0] + min_r_pix * math.cos(math.radians(theta)),
                   centre[1] + min_r_pix * math.sin(math.radians(theta)))
            pt2 = (centre[0] + max_r_pix * math.cos(math.radians(theta)),
                   centre[1] + max_r_pix * math.sin(math.radians(theta)))
            roi = Line(pt1[0], pt1[1], pt2[0], pt2[1])
            proj_imp.setRoi(roi)
            profile = roi.getPixels()
            projected_im_row.append(max(profile))
            try:
                ring_roi_xs.append(roi.getContainedPoints()[profile.index(
                    max(profile))].x)
            except IndexError:
                ring_roi_xs.append(pt2[0])
            try:
                ring_roi_ys.append(roi.getContainedPoints()[profile.index(
                    max(profile))].y)
            except IndexError:
                ring_roi_ys.append(pt2[1])
            proj_imp.killRoi()
        ring_roi = PolygonRoi(ring_roi_xs, ring_roi_ys, Roi.FREELINE)
        ring_rois.append(ring_roi)
        if generate_roi_stack:
            roi_stack.setZ(zidx + 1)
            roi_stack.setRoi(ring_roi)
            IJ.run(roi_stack, "Line to Area", "")
            IJ.run(
                roi_stack, "Set...",
                "value=" + str(roi_stack.getProcessor().maxValue()) + " slice")
        #egfp_imp.setRoi(ring_roi);
        projected_im_pix.append(projected_im_row)


#	for ch in split_chs:
#		ch.close();

    out_imp = ImagePlus(
        "projected", FloatProcessor([list(x) for x in zip(*projected_im_pix)]))

    if generate_roi_stack:
        roi_stack.show()
        egfp_imp_disp.show()
        # merge?
    else:
        roi_stack = None
    return out_imp, roi_stack, ring_rois, centres