Search in sources :

Example 1 with RotatedRectRoi

use of ij.gui.RotatedRectRoi in project mia by mianalysis.

the class RectangleHoughDetection method process.

@Override
public Status process(Workspace workspace) {
    // Getting input image
    String inputImageName = parameters.getValue(INPUT_IMAGE);
    Image inputImage = workspace.getImage(inputImageName);
    ImagePlus ipl = inputImage.getImagePlus();
    // Getting parameters
    String outputObjectsName = parameters.getValue(OUTPUT_OBJECTS);
    boolean outputTransformImage = parameters.getValue(OUTPUT_TRANSFORM_IMAGE);
    String outputImageName = parameters.getValue(OUTPUT_IMAGE);
    // Getting parameters
    String xRange = parameters.getValue(X_RANGE);
    String yRange = parameters.getValue(Y_RANGE);
    String widthRange = parameters.getValue(WIDTH_RANGE);
    String lengthRange = parameters.getValue(LENGTH_RANGE);
    String oriRange = parameters.getValue(ORIENTATION_RANGE);
    int samplingRate = parameters.getValue(DOWNSAMPLE_FACTOR);
    boolean multithread = parameters.getValue(ENABLE_MULTITHREADING);
    boolean normaliseScores = parameters.getValue(NORMALISE_SCORES);
    String detectionMode = parameters.getValue(DETECTION_MODE);
    double detectionThreshold = parameters.getValue(DETECTION_THRESHOLD);
    int nObjects = parameters.getValue(NUMBER_OF_OBJECTS);
    int exclusionRadius = parameters.getValue(EXCLUSION_RADIUS);
    boolean showTransformImage = parameters.getValue(SHOW_TRANSFORM_IMAGE);
    boolean showDetectionImage = parameters.getValue(SHOW_DETECTION_IMAGE);
    boolean showHoughScore = parameters.getValue(SHOW_HOUGH_SCORE);
    int labelSize = parameters.getValue(LABEL_SIZE);
    // Storing the image calibration
    SpatCal cal = SpatCal.getFromImage(ipl);
    int nFrames = ipl.getNFrames();
    double frameInterval = ipl.getCalibration().frameInterval;
    Objs outputObjects = new Objs(outputObjectsName, cal, nFrames, frameInterval, TemporalUnit.getOMEUnit());
    xRange = resampleRange(xRange, samplingRate);
    yRange = resampleRange(yRange, samplingRate);
    widthRange = resampleRange(widthRange, samplingRate);
    lengthRange = resampleRange(lengthRange, samplingRate);
    int nThreads = multithread ? Prefs.getThreads() : 1;
    // Iterating over all images in the ImagePlus
    int count = 1;
    int total = ipl.getNChannels() * ipl.getNSlices() * ipl.getNFrames();
    for (int c = 0; c < ipl.getNChannels(); c++) {
        for (int z = 0; z < ipl.getNSlices(); z++) {
            for (int t = 0; t < ipl.getNFrames(); t++) {
                ipl.setPosition(c + 1, z + 1, t + 1);
                // Applying scaling
                ImageProcessor ipr = ipl.getProcessor();
                if (samplingRate != 1)
                    ipr = ipr.resize(ipr.getWidth() / samplingRate);
                // Initialising the Hough transform
                String[] paramRanges = new String[] { xRange, yRange, widthRange, lengthRange, oriRange };
                RectangleTransform transform = new RectangleTransform(ipr, paramRanges);
                transform.setnThreads(nThreads);
                // Running the transforms
                transform.run();
                // Normalising scores based on the number of points in that rectangle
                if (normaliseScores)
                    transform.normaliseScores();
                // Getting the accumulator as an image
                if (outputTransformImage || (showOutput && showTransformImage)) {
                    ImagePlus showIpl = new Duplicator().run(transform.getAccumulatorAsImage());
                    if (outputTransformImage) {
                        Image outputImage = new Image(outputImageName, showIpl);
                        workspace.addImage(outputImage);
                    }
                    if (showOutput && showTransformImage) {
                        IntensityMinMax.run(showIpl, true);
                        showIpl.setTitle("Accumulator");
                        showIpl.show();
                    }
                }
                // Getting rectangle objects and adding to workspace
                ArrayList<double[]> rectangles;
                switch(detectionMode) {
                    default:
                    case DetectionModes.ALL_ABOVE_SCORE:
                        rectangles = transform.getObjects(detectionThreshold, exclusionRadius);
                        break;
                    case DetectionModes.N_HIGHEST_SCORES:
                        rectangles = transform.getNObjects(nObjects, exclusionRadius);
                        break;
                }
                for (double[] rectangle : rectangles) {
                    // Initialising the object
                    Obj outputObject = outputObjects.createAndAddNewObject(VolumeType.QUADTREE);
                    // Getting rectangle parameters
                    int x = (int) Math.round(rectangle[0]) * samplingRate;
                    int y = (int) Math.round(rectangle[1]) * samplingRate;
                    int w = (int) Math.round(rectangle[2]) * samplingRate;
                    int l = (int) Math.round(rectangle[3]) * samplingRate;
                    int ori = (int) Math.round(rectangle[4]);
                    double score = rectangle[5];
                    double thetaRads = -Math.toRadians((double) ori);
                    int x1 = (int) Math.round((-l / 2) * Math.cos(thetaRads));
                    int y1 = (int) Math.round(-(-l / 2) * Math.sin(thetaRads));
                    RotatedRectRoi roi = new RotatedRectRoi(x + x1, y + y1, x - x1, y - y1, w);
                    for (Point point : roi.getContainedPoints()) {
                        try {
                            try {
                                outputObject.add(point.x, point.y, z);
                            } catch (PointOutOfRangeException e) {
                            }
                        } catch (IntegerOverflowException e) {
                            return Status.FAIL;
                        }
                    }
                    // Adding measurements
                    outputObject.setT(t);
                    outputObject.addMeasurement(new Measurement(Measurements.SCORE, score));
                }
                writeProgressStatus(count++, total, "images");
            }
        }
    }
    ipl.setPosition(1, 1, 1);
    workspace.addObjects(outputObjects);
    if (showOutput && showDetectionImage)
        showDetectionImage(inputImage, outputObjects, showHoughScore, labelSize);
    return Status.PASS;
}
Also used : RotatedRectRoi(ij.gui.RotatedRectRoi) Measurement(io.github.mianalysis.mia.object.Measurement) Duplicator(ij.plugin.Duplicator) Point(java.awt.Point) Image(io.github.mianalysis.mia.object.Image) ImagePlus(ij.ImagePlus) Point(java.awt.Point) SpatCal(io.github.sjcross.common.object.volume.SpatCal) ImageProcessor(ij.process.ImageProcessor) RectangleTransform(io.github.sjcross.common.process.houghtransform.transforms.RectangleTransform) PointOutOfRangeException(io.github.sjcross.common.object.volume.PointOutOfRangeException) Obj(io.github.mianalysis.mia.object.Obj) Objs(io.github.mianalysis.mia.object.Objs) IntegerOverflowException(io.github.sjcross.common.exceptions.IntegerOverflowException)

Aggregations

ImagePlus (ij.ImagePlus)1 RotatedRectRoi (ij.gui.RotatedRectRoi)1 Duplicator (ij.plugin.Duplicator)1 ImageProcessor (ij.process.ImageProcessor)1 Image (io.github.mianalysis.mia.object.Image)1 Measurement (io.github.mianalysis.mia.object.Measurement)1 Obj (io.github.mianalysis.mia.object.Obj)1 Objs (io.github.mianalysis.mia.object.Objs)1 IntegerOverflowException (io.github.sjcross.common.exceptions.IntegerOverflowException)1 PointOutOfRangeException (io.github.sjcross.common.object.volume.PointOutOfRangeException)1 SpatCal (io.github.sjcross.common.object.volume.SpatCal)1 RectangleTransform (io.github.sjcross.common.process.houghtransform.transforms.RectangleTransform)1 Point (java.awt.Point)1