/** Refine segmentation with ADD/SUB regions of interests */ private synchronized void refine() { if (controlPanel.status != ControlJPanel.SEGMENTATED_STATUS) return; if (controlPanel.addJRadioButton.isSelected()) addRoi = imp.getRoi(); else subRoi = imp.getRoi(); if (null != addRoi) { final float alpha = controlPanel.addThreshold.getValue() / 100.0f; final Shape shape = ShapeRoiHelper.getShape(new ShapeRoi(addRoi)); final AffineTransform trans = new AffineTransform(); trans.translate(addRoi.getBounds().getX(), addRoi.getBounds().getY()); final Area area = new Area(shape); area.transform(trans); siox.subpixelRefine(area, SioxSegmentator.ADD_EDGE, alpha, (float[]) confMatrix.getPixels()); } if (null != subRoi) { final float alpha = controlPanel.subThreshold.getValue() / 100.0f; final Shape shape = ShapeRoiHelper.getShape(new ShapeRoi(subRoi)); final AffineTransform trans = new AffineTransform(); trans.translate(subRoi.getBounds().getX(), subRoi.getBounds().getY()); final Area area = new Area(shape); area.transform(trans); siox.subpixelRefine(area, SioxSegmentator.SUB_EDGE, alpha, (float[]) confMatrix.getPixels()); } updateResult(); }
/** Segment image based on the current foreground and background ROIs */ private synchronized void segment() { if (controlPanel.bgJRadioButton.isSelected()) backgroundRoi = imp.getRoi(); else foregroundRoi = imp.getRoi(); if (foregroundRoi == null) { IJ.error("Siox Segmentation", "ERROR: no foreground selected!"); return; } // Create confidence matrix and initialize to unknown region of confidence confMatrix = new FloatProcessor(imp.getWidth(), imp.getHeight()); final float[] imgData = (float[]) confMatrix.getPixels(); confMatrix.add(SioxSegmentator.UNKNOWN_REGION_CONFIDENCE); // Set foreground ROI if (foregroundRoi != null) { confMatrix.setValue(SioxSegmentator.CERTAIN_FOREGROUND_CONFIDENCE); confMatrix.fill(foregroundRoi); } // Set background ROI if (backgroundRoi != null) { confMatrix.setValue(SioxSegmentator.CERTAIN_BACKGROUND_CONFIDENCE); confMatrix.fill(backgroundRoi); } else { // Workaround: select border pixels which are not foreground as background if no background // was specified. int w = imp.getWidth(), h = imp.getHeight(); for (int i = 0; i < w; i++) { if (imgData[i] < 0.8f) imgData[i] = 0; if (imgData[i + w * (h - 1)] < 0.8f) imgData[i + w * (h - 1)] = 0; } for (int i = 0; i < h; i++) { if (imgData[w * i] < 0.8f) imgData[w * i] = 0; if (imgData[w - 1 + w * i] < 0.8f) imgData[w - 1 + w * i] = 0; } } // Call SIOX segmentation method int[] pixels = (int[]) ip.getPixels(); final int smoothes = controlPanel.smoothness.getValue(); siox = new SioxSegmentator(imp.getWidth(), imp.getHeight(), null); boolean success = siox.segmentate(pixels, imgData, smoothes, controlPanel.multipart.isSelected() ? 4 : 0); if (!success) IJ.error("Siox Segmentation", "The segmentation failed!"); updateResult(); // Set status flag to segmented controlPanel.status = ControlJPanel.SEGMENTATED_STATUS; controlPanel.updateComponentEnabling(); roiOverlay.setComposite(transparency100); // Set up next panel components controlPanel.subJRadioButton.setSelected(true); controlPanel.addJRadioButton.setSelected(false); lastButton = controlPanel.subJRadioButton; }