/** Reset overlays and set plugin to initial status */ private void reset() { roiOverlay.setRoi(null); resultOverlay.setImage(null); confMatrix = null; // Set initial status controlPanel.status = ControlJPanel.FG_ADDED_STATUS; lastButton = controlPanel.fgJRadioButton.isSelected() ? controlPanel.fgJRadioButton : controlPanel.bgJRadioButton; roiOverlay.setColor(controlPanel.fgJRadioButton.isSelected() ? Color.RED : Color.GREEN); roiOverlay.setComposite(transparency050); if (controlPanel.bgJRadioButton.isSelected()) { imp.setRoi(backgroundRoi); roiOverlay.setRoi(foregroundRoi); } else { imp.setRoi(foregroundRoi); roiOverlay.setRoi(backgroundRoi); } controlPanel.updateComponentEnabling(); imp.changes = true; imp.updateAndDraw(); }
/** 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; }