ij.plugin.frame.RoiManager Java Examples

The following examples show how to use ij.plugin.frame.RoiManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DetectCirclesJ_.java    From IJ-OpenCV with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {

    // Converters
    ImagePlusMatConverter ic = new ImagePlusMatConverter();
    opencv_core.Mat m = ic.convert(imp, Mat.class);
    MatListOvalRoiConverter cc = new MatListOvalRoiConverter();

    Mat gray = new Mat();
    opencv_imgproc.cvtColor(m, gray, opencv_imgproc.COLOR_BGR2GRAY);

    Mat circles = new Mat();
    opencv_imgproc.HoughCircles(gray, circles, opencv_imgproc.CV_HOUGH_GRADIENT, 1.2, 100);

    ArrayList<OvalRoi> or = new ArrayList<OvalRoi>();
    or = cc.convert(circles, or.getClass());

    RoiManager rm = new RoiManager();

    for (int i = 0; i < or.size(); i++) {
        rm.add(imp, or.get(i), i);
    }

}
 
Example #2
Source File: FaceDetectionJ_.java    From IJ-OpenCV with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
    //Converters
    RectRoiConverter rc = new RectRoiConverter();
    opencv_core.Mat img2 = ImagePlusMatConverter.toMat(imp, 8); // also does RGB to Gray automatically

    // Detect the faces and store them as an array of rectangles
    opencv_core.RectVector rv = detectFaces(img2);

    // Add rectangles to ROI Manager
    RoiManager rm = new RoiManager();
    rm.setVisible(true);
    for (int i = 0; i < rv.size(); i++) {
        Roi r = rc.convert(rv.get(i), Roi.class);
        rm.add(imp, r, 0);
    }
    
    //Show all ROI
    rm.runCommand("Show All");

}
 
Example #3
Source File: ConvexHullFromPolygonROIJ_.java    From IJ-OpenCV with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void run() {
    // Get the ROI and check that it is a polygon
    PolygonRoi r = (PolygonRoi) imp.getRoi();

    if (r == null) {
        IJ.error("Select first a polygon ROI");
        return;
    }

    // Converter
    PolygonRoiMatConverter pc = new PolygonRoiMatConverter();
    MatPointRoiConverter mpc = new MatPointRoiConverter();

    Mat m = pc.convert(r, Mat.class);
    Mat convexHull = new Mat();
    opencv_imgproc.convexHull(m, convexHull);

    PolygonRoi pr = mpc.convert(convexHull, PolygonRoi.class);

    RoiManager rm = new RoiManager();
    rm.add(imp, r, 0);
    rm.add(imp, pr, 1);

}
 
Example #4
Source File: HoughLinesJ_.java    From IJ-OpenCV with GNU General Public License v3.0 5 votes vote down vote up
@Override
  public void run() {
 
// Do line detection
ArrayList<Line> linesIJ = HoughLines(imp, min_length, step_line, min_theta, max_theta, step_theta);

// Add lines to RoiManager
      RoiManager rm = new RoiManager();
      rm.setVisible(true);

      for (int i = 0; i < linesIJ.size(); i++) {
          rm.add(imp, linesIJ.get(i), i);
       }
}
 
Example #5
Source File: GeodesicDiameterPlugin.java    From MorphoLibJ with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Adds the specified paths to the list of ROI of the image plus.
 * 
 * @param target The ImagePlus that will be associated with ROIS
 * @param geodDiams the list of paths
 */
public void createPathRois(ImagePlus target, Map<Integer, GeodesicDiameter.Result> geodDiams)
{
	// get instance of ROI Manager
	RoiManager manager = RoiManager.getRoiManager();
	Calibration calib = target.getCalibration();
	
	// add each path to the ROI Manager
	int index = 0;
	for (GeodesicDiameter.Result result : geodDiams.values())
	{
	    manager.add(target, createPathRoi(result.path, calib), index++);
	}
}
 
Example #6
Source File: Coloc_2.java    From Colocalisation_Analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * A method to fill the masks array with data based on the ROI manager.
 */
protected boolean createMasksFromRoiManager(final int width,
	final int height)
{
	final RoiManager roiManager = RoiManager.getInstance();
	if (roiManager == null) {
		IJ.error("Could not get ROI Manager instance.");
		return false;
	}
	final Roi[] selectedRois = roiManager.getSelectedRoisAsArray();
	// create the ROIs
	createMasksAndRois(selectedRois, width, height);
	return true;
}
 
Example #7
Source File: TemplateMatchingJ_.java    From IJ-OpenCV with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void run() {
    ImagePlus original = (ImagePlus) imp.clone();

    Roi r = imp.getRoi();

    if (r == null) {
        IJ.error("Select a rectangle first");
        return;
    }

    if (r.isArea() && (r.getType() == Roi.RECTANGLE)) {

        // Crop the selection to be the template
        IJ.run(imp, "Crop", "");

        //Converters
        ImagePlusMatConverter ic = new ImagePlusMatConverter();
        RectRoiConverter rc = new RectRoiConverter();

        // Convert the ImageJ images to OpenCV images
        opencv_core.Mat matching = ic.convert(original, Mat.class);
        opencv_core.Mat template = ic.convert(imp, Mat.class);

        opencv_core.Mat gray = new opencv_core.Mat();
        opencv_imgproc.cvtColor(matching, gray, opencv_imgproc.COLOR_BGR2GRAY);
        opencv_imgproc.cvtColor(template, template, opencv_imgproc.COLOR_BGR2GRAY);

        opencv_core.Mat results = new opencv_core.Mat();

        // Matching and normalisation
        matchTemplate(gray, template, results, TM_CCOEFF_NORMED);
        normalize(results, results, 0, 1, NORM_MINMAX, -1, new opencv_core.Mat());

        DoublePointer minVal = new DoublePointer();
        DoublePointer maxVal= new DoublePointer();
        opencv_core.Point minLoc = new opencv_core.Point();
        opencv_core.Point maxLoc = new opencv_core.Point();
        opencv_core.Point matchLoc;

        minMaxLoc(results, minVal, maxVal, minLoc, maxLoc, new opencv_core.Mat());

        ArrayList<opencv_core.Point> locations = obtainLocations(results, 0.99, template.cols(), template.rows());
        RoiManager rm = new RoiManager();
        rm.setVisible(true);

        opencv_core.Rect solution;
        Roi solutionIJ;
        opencv_core.Point p;
        for (int i = 0; i < locations.size(); i++) {
            p = locations.get(i);
            solution = new opencv_core.Rect(p.x(), p.y(), template.cols(), template.rows());
            solutionIJ = rc.convert(solution, Roi.class);
            rm.add(original, solutionIJ, i);

        }

        imp.changes = false;
        imp.close();
        original.show();

    } else {
        IJ.error("Select a rectangle");
    }
}
 
Example #8
Source File: RoiPicker.java    From Stitching with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Functional method of the RoiPicker. Determines which Rois match the x,y
 * coordinates of the received MouseEvent and the C,Z,T coordinates of the
 * active image. The user can cycle through these Rois with repeated clicks.
 */
@Override
public void mouseReleased(MouseEvent e) {
	ImageCanvas source = (ImageCanvas) e.getSource();
	if (source != canvas) {
		// We changed image window. Update fields accordingly
		ImageWindow window = (ImageWindow) source.getParent();
		imp = window.getImagePlus();
		canvas = source;
	}
	// Convert global coords to local
	double x = canvas.offScreenXD(e.getX());
	double y = canvas.offScreenYD(e.getY());
	// Get the RoiManager
	RoiManager rm = RoiManager.getInstance();
	if (rm == null) return;

	Roi[] rois = rm.getRoisAsArray();
	// Get the active ImagePlus's current z,c,t coords
	int[] position = imp.convertIndexToPosition(imp.getCurrentSlice());

	Set<Integer> matchedSet = new HashSet<Integer>();
	List<Integer> matchedIndices = new ArrayList<Integer>();

	// generate list of all rois containing x,y and matching the ImagePlus's
	// position
	for (int i = 0; i < rois.length; i++) {
		Roi r = rois[i];
		// Check position
		if (containsPoint(r, position, x, y)) {
			// Matched. Add to the matched set and list
			Integer index = i;
			matchedSet.add(index);
			matchedIndices.add(index);
		}
	}

	// If we discovered the currently known roi set, display the next roi in
	// the series
	if (same(roiSet, matchedSet)) {
		incrementIndex();
	}
	else {
		// otherwise, update the cached indices and display the union of the rois
		roiSet = matchedSet;
		roiIndices = matchedIndices;
		roiIndex = roiIndices.size();
	}

	// Perform the roi selection
	if (matchedIndices.size() > 0) selectRois(rm);
}
 
Example #9
Source File: Coloc_2.java    From Colocalisation_Analysis with GNU General Public License v3.0 4 votes vote down vote up
/** Programmatically initializes the colocalisation settings to match the given values. */
public boolean initializeSettings(ImagePlus imp1, ImagePlus imp2, int gdIndexMask, int gdIndexRegr,
		boolean gdAutoSavePdf, boolean gdDisplayImages, boolean gdDisplayShuffledCostes, boolean gdUseLiCh1,
		boolean gdUseLiCh2, boolean gdUseLiICQ, boolean gdUseSpearmanRank, boolean gdUseManders,
		boolean gdUseKendallTau, boolean gdUseScatterplot, boolean gdUseCostes, int gdPsf,
		int gdNrCostesRandomisations)
{
	// get image names for output
	Ch1Name = imp1.getTitle();
	Ch2Name = imp2.getTitle();

	// make sure neither image is RGB type
	if (imp1.getBitDepth() == 24 || imp2.getBitDepth() == 24) {
		IJ.showMessage("You should not use RGB color images to measure colocalization. Provide each channel as a 8-bit or 16-bit image.");
		return false;
	}

	// make sure both images have the same bit-depth
	if (imp1.getBitDepth() != imp2.getBitDepth()) {
		IJ.showMessage("Both images must have the same bit-depth.");
		return false;
	}

	// get information about the mask/ROI to use
	indexMask = gdIndexMask;
	if (indexMask == 0) roiConfig = RoiConfiguration.None;
	else if (indexMask == 1) roiConfig = RoiConfiguration.Img1;
	else if (indexMask == 2) roiConfig = RoiConfiguration.Img2;
	else if (indexMask == 3) roiConfig = RoiConfiguration.RoiManager;
	else {
		roiConfig = RoiConfiguration.Mask;
		/* Make indexMask the reference to the mask image to use.
		 * To do this we reduce it by three for the first three
		 * entries in the combo box.
		 */
		indexMask = indexMask - 4;
	}

	// save the ImgLib wrapped images as members
	img1 = ImagePlusAdapter.wrap(imp1);
	img2 = ImagePlusAdapter.wrap(imp2);

	/* check if we have a valid ROI for the selected configuration
	 * and if so, get the ROI's bounds. Alternatively, a mask can
	 * be selected (that is basically all, but a rectangle).
	 */
	if (roiConfig == RoiConfiguration.Img1 && hasValidRoi(imp1)) {
		createMasksFromImage(imp1);
	}
	else if (roiConfig == RoiConfiguration.Img2 && hasValidRoi(imp2)) {
		createMasksFromImage(imp2);
	}
	else if (roiConfig == RoiConfiguration.RoiManager) {
		if (!createMasksFromRoiManager(imp1.getWidth(), imp1.getHeight()))
			return false;
	}
	else if (roiConfig == RoiConfiguration.Mask) {
		// get the image to be used as mask
		final int[] windowList = WindowManager.getIDList();
		final ImagePlus maskImp = WindowManager.getImage(windowList[indexMask]);
		final Img<T> maskImg = ImagePlusAdapter.<T> wrap(maskImp);
		// get a valid mask info for the image
		final MaskInfo mi = getBoundingBoxOfMask(maskImg);
		masks.add(mi);
	}
	else {
		/* if no ROI/mask is selected, just add an empty MaskInfo
		 * to colocalise both images without constraints.
		 */
		masks.add(new MaskInfo(null, null));
	}

	// get information about the mask/ROI to use
	indexRegr = gdIndexRegr;

	// read out GUI data
	autoSavePdf = gdAutoSavePdf;
	displayImages = gdDisplayImages;

	// Parse algorithm options
	pearsonsCorrelation = new PearsonsCorrelation<>(
		PearsonsCorrelation.Implementation.Fast);

	if (gdUseLiCh1) liHistogramCh1 = new LiHistogram2D<>("Li - Ch1", true);
	if (gdUseLiCh2) liHistogramCh2 = new LiHistogram2D<>("Li - Ch2", false);
	if (gdUseLiICQ) liICQ = new LiICQ<>();
	if (gdUseSpearmanRank) {
		SpearmanRankCorrelation = new SpearmanRankCorrelation<>();
	}
	if (gdUseManders) mandersCorrelation = new MandersColocalization<>();
	if (gdUseKendallTau) kendallTau = new KendallTauRankCorrelation<>();
	if (gdUseScatterplot) histogram2D = new Histogram2D<>(
		"2D intensity histogram");
	if (gdUseCostes) {
		costesSignificance = new CostesSignificanceTest<>(pearsonsCorrelation,
			gdPsf, gdNrCostesRandomisations, gdDisplayShuffledCostes);
	}

	return true;
}
 
Example #10
Source File: FindContoursJ_.java    From IJ-OpenCV with GNU General Public License v3.0 3 votes vote down vote up
@Override
public void run() {
    
    // Converters
    ImagePlusMatConverter ic = new ImagePlusMatConverter();

    opencv_core.Mat m = ic.convert(imp,opencv_core.Mat.class);
    MatVectorListPolygonRoiConverter pc = new MatVectorListPolygonRoiConverter();
    
    
    opencv_core.Mat gray = new opencv_core.Mat();
    opencv_imgproc.cvtColor(m,gray,opencv_imgproc.COLOR_BGR2GRAY);
    
    MatVector contours = new opencv_core.MatVector();
    
    opencv_imgproc.findContours(gray,contours,opencv_imgproc.RETR_LIST,opencv_imgproc.CHAIN_APPROX_SIMPLE);
    
    ArrayList<PolygonRoi> contoursROI = new ArrayList<PolygonRoi>();
    contoursROI= pc.convert(contours,contoursROI.getClass());
    
     // Add rectangles to ROI Manager
    RoiManager rm = new RoiManager();
    rm.setVisible(true);
    for (PolygonRoi contoursROI1 : contoursROI) {
        rm.add(imp, contoursROI1, 0);
    }
    
    
    

}