Example usage for org.opencv.core Mat get

List of usage examples for org.opencv.core Mat get

Introduction

In this page you can find the example usage for org.opencv.core Mat get.

Prototype

public double[] get(int row, int col) 

Source Link

Usage

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerDetector.java

License:Open Source License

@Override
public void process(ImageBuffers buffers) {
    final ArrayList<MatOfPoint> contours = new ArrayList<>();
    final Mat hierarchy = new Mat();
    // Make sure the image is rotated before the contours are generated, if necessary
    //if (Feature.get(this.context, R.bool.feature_combined_markers).isEnabled() || outlineDisplay != OutlineDisplay.none || codeDisplay == CodeDisplay.visible)
    //{/*from  www  . j av a  2s  .  c  o m*/
    // if statement commented out as there was a bug where the overlay was not getting cleared
    // after cycling through the threshold views.
    buffers.getOverlay();
    //}
    try {
        final List<Marker> foundMarkers = new ArrayList<>();
        Imgproc.findContours(buffers.getImageInGrey(), contours, hierarchy, Imgproc.RETR_TREE,
                Imgproc.CHAIN_APPROX_NONE);
        for (int i = 0; i < contours.size(); i++) {
            final Marker marker = createMarkerForNode(i, contours, hierarchy);
            if (marker != null) {
                final String markerCode = getCodeKey(marker);
                if (validCodes.isEmpty() || validCodes.contains(markerCode)) {
                    // If this marker has a minimum size set and is smaller: continue in loop.
                    Action action = experience.getActionForCode(markerCode);
                    if (action != null && action.getMinimumSize() != null) {
                        double minimumSize = action.getMinimumSize();
                        Rect boundingRect = Imgproc.boundingRect(contours.get(i));
                        if (!(boundingRect.width / (float) buffers.getImageInAnyFormat().cols() > minimumSize
                                || boundingRect.height
                                        / (float) buffers.getImageInAnyFormat().rows() > minimumSize)) {
                            continue;
                        }
                    }

                    foundMarkers.add(marker);

                    if (outlineDisplay != OutlineDisplay.none) {
                        Mat overlay = buffers.getOverlay();
                        if (outlineDisplay == OutlineDisplay.regions) {
                            double[] nodes = hierarchy.get(0, i);
                            int currentRegionIndex = (int) nodes[FIRST_NODE];

                            while (currentRegionIndex >= 0) {
                                Imgproc.drawContours(overlay, contours, currentRegionIndex, outlineColour, 4);
                                Imgproc.drawContours(overlay, contours, currentRegionIndex, regionColour, 2);

                                nodes = hierarchy.get(0, currentRegionIndex);
                                currentRegionIndex = (int) nodes[NEXT_NODE];
                            }
                        }

                        Imgproc.drawContours(overlay, contours, i, outlineColour, 7);
                        Imgproc.drawContours(overlay, contours, i, detectedColour, 5);
                    }

                    if (codeDisplay == CodeDisplay.visible) {
                        Mat overlay = buffers.getOverlay();
                        Rect bounds = Imgproc.boundingRect(contours.get(i));
                        Imgproc.putText(overlay, markerCode, bounds.tl(), Core.FONT_HERSHEY_SIMPLEX, 1,
                                outlineColour, 5);
                        Imgproc.putText(overlay, markerCode, bounds.tl(), Core.FONT_HERSHEY_SIMPLEX, 1,
                                detectedColour, 3);
                    }
                }
            }
        }

        buffers.setDetected(!foundMarkers.isEmpty());
        handler.onMarkersDetected(foundMarkers, contours, hierarchy, buffers.getImageInGrey().size());
    } finally {
        contours.clear();
        hierarchy.release();
    }
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerDetector.java

License:Open Source License

protected boolean isValidDot(int nodeIndex, Mat hierarchy) {
    double[] nodes = hierarchy.get(0, nodeIndex);
    return nodes[FIRST_NODE] < 0;
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerDetector.java

License:Open Source License

protected Marker createMarkerForNode(int nodeIndex, List<MatOfPoint> contours, Mat hierarchy) {
    List<MarkerRegion> regions = null;
    for (int currentNodeIndex = (int) hierarchy.get(0,
            nodeIndex)[FIRST_NODE]; currentNodeIndex >= 0; currentNodeIndex = (int) hierarchy.get(0,
                    currentNodeIndex)[NEXT_NODE]) {
        final MarkerRegion region = createRegionForNode(currentNodeIndex, contours, hierarchy);
        if (region != null) {
            if (this.ignoreEmptyRegions && region.value == 0) {
                continue;
            } else if (regions == null) {
                regions = new ArrayList<>();
            } else if (regions.size() >= maxRegions) {
                return null;
            }//from  w w  w.j a  v a 2  s.c  o  m

            regions.add(region);
        } else {
            return null;
        }
    }

    if (regions != null) {
        Marker marker = new Marker(nodeIndex, regions);
        sortCode(marker);
        if (isValidRegionList(marker)) {
            return marker;
        }
    }

    return null;
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerDetector.java

License:Open Source License

protected MarkerRegion createRegionForNode(int regionIndex, List<MatOfPoint> contours, Mat hierarchy) {
    // Find the first dot index:
    double[] nodes = hierarchy.get(0, regionIndex);
    int currentNodeIndex = (int) nodes[FIRST_NODE];
    if (currentNodeIndex < 0 && !(this.ignoreEmptyRegions || this.maxEmptyRegions > 0)) {
        return null; // There are no dots in this region, and empty regions are not allowed.
    }//from  w  w  w.  ja va 2s. c  o  m

    // Count all the dots and check if they are leaf nodes in the hierarchy:
    int dotCount = 0;
    while (currentNodeIndex >= 0) {
        if (isValidDot(currentNodeIndex, hierarchy)) {
            dotCount++;
            // Get next dot node:
            nodes = hierarchy.get(0, currentNodeIndex);
            currentNodeIndex = (int) nodes[NEXT_NODE];

            if (dotCount > maxRegionValue) {
                // Too many dots
                return null;
            }
        } else {
            // Not a dot
            return null;
        }
    }

    return new MarkerRegion(regionIndex, dotCount);
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerEmbeddedChecksumAreaOrderDetector.java

License:Open Source License

protected Marker createMarkerForNode(int nodeIndex, List<MatOfPoint> contours, Mat hierarchy) {
    List<MarkerRegion> regions = null;
    MarkerRegion checksumRegion = null;//w w  w .  j  a  v a  2 s.  c  o m
    for (int currentNodeIndex = (int) hierarchy.get(0,
            nodeIndex)[FIRST_NODE]; currentNodeIndex >= 0; currentNodeIndex = (int) hierarchy.get(0,
                    currentNodeIndex)[NEXT_NODE]) {
        final MarkerRegion region = createRegionForNode(currentNodeIndex, contours, hierarchy);
        if (region != null) {
            if (this.ignoreEmptyRegions && region.value == 0) {
                continue;
            } else if (regions == null) {
                regions = new ArrayList<>();
            } else if (regions.size() >= maxRegions) {
                return null;
            }

            regions.add(region);
        } else if (checksumRegion == null) {
            checksumRegion = getChecksumRegionAtNode(currentNodeIndex, hierarchy);
            if (checksumRegion == null) {
                return null;
            }
        } else {
            return null;
        }
    }

    if (regions != null && checksumRegion != null) {
        Marker marker = new MarkerWithEmbeddedChecksum(nodeIndex, regions, checksumRegion);
        sortByValue(marker);
        if (isValidRegionList(marker)) {
            addAreaToRegions(marker, contours);
            sortByArea(marker);
            return marker;
        }
    }

    return null;
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerEmbeddedChecksumDetector.java

License:Open Source License

protected Marker createMarkerForNode(int nodeIndex, List<MatOfPoint> contours, Mat hierarchy) {
    List<MarkerRegion> regions = null;
    MarkerRegion checksumRegion = null;/*ww  w  .  j a va  2  s  .c o m*/
    for (int currentNodeIndex = (int) hierarchy.get(0,
            nodeIndex)[FIRST_NODE]; currentNodeIndex >= 0; currentNodeIndex = (int) hierarchy.get(0,
                    currentNodeIndex)[NEXT_NODE]) {
        final MarkerRegion region = createRegionForNode(currentNodeIndex, contours, hierarchy);
        if (region != null) {
            if (this.ignoreEmptyRegions && region.value == 0) {
                continue;
            } else if (regions == null) {
                regions = new ArrayList<>();
            } else if (regions.size() >= maxRegions) {
                return null;
            }

            regions.add(region);
        } else if (checksumRegion == null) {
            checksumRegion = getChecksumRegionAtNode(currentNodeIndex, hierarchy);
            if (checksumRegion == null) {
                return null;
            }
        } else {
            return null;
        }
    }

    if (regions != null) {
        Marker marker = new MarkerWithEmbeddedChecksum(nodeIndex, regions, checksumRegion);
        sortCode(marker);
        if (isValidRegionList(marker)) {
            return marker;
        }
    }

    return null;
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerEmbeddedChecksumDetector.java

License:Open Source License

protected MarkerRegion getChecksumRegionAtNode(int regionIndex, Mat hierarchy) {
    // Find the first dot index:
    double[] nodes = hierarchy.get(0, regionIndex);
    int currentDotIndex = (int) nodes[FIRST_NODE];
    if (currentDotIndex < 0) {
        return null; // There are no dots in this region.
    }//from ww  w  .ja  va 2  s.c o  m

    // Count all the dots and check if they are leaf nodes in the hierarchy:
    int dotCount = 0;
    while (currentDotIndex >= 0) {
        if (isValidHollowDot(currentDotIndex, hierarchy)) {
            dotCount++;
        } else if (!(this.relaxedEmbeddedChecksumIgnoreNonHollowDots
                && this.isValidDot(currentDotIndex, hierarchy))) {
            return null; // Dot is not a leaf in the hierarchy.
        }

        // Get next dot node:
        nodes = hierarchy.get(0, currentDotIndex);
        currentDotIndex = (int) nodes[NEXT_NODE];
    }

    return new MarkerRegion(regionIndex, dotCount);
}

From source file:uk.ac.horizon.artcodes.detect.marker.MarkerEmbeddedChecksumDetector.java

License:Open Source License

private boolean isValidHollowDot(int nodeIndex, Mat hierarchy) {
    double[] nodes = hierarchy.get(0, nodeIndex);
    return nodes[FIRST_NODE] >= 0 && // has a child node, and
            (hierarchy.get(0, (int) nodes[FIRST_NODE])[NEXT_NODE] < 0
                    || this.relaxedEmbeddedChecksumIgnoreMultipleHollowSegments)
            && //the child has no siblings, and
            isValidDot((int) nodes[FIRST_NODE], hierarchy);// the child is a leaf
}

From source file:uom.research.thalassemia.logic.BloodCellData.java

private void process(final Mat currentSelection) {
    sgf = 0;//from   w  w w  .  j  a  va 2 s . c o  m
    getMinMaxDiameter(currentSelection);
    if (minDiameter != 0) {
        //calculate shape geometric factor
        sgf = maxDiameter / minDiameter;
    }
    double[] circles;

    for (int a = 0; a < currentSelection.cols(); a++) {

        Map<Point, Double> points = getPallorBloodCellsPointList();
        areaPreparation = 0;
        circles = currentSelection.get(0, a);
        x = circles[0];
        y = circles[1];
        r = Math.round(circles[2]);

        //get area value
        area = calculateArea(r);
        //get perimeter value
        perimeter = calculatePerimeter(r);
        //get diameter value
        diameter = calculateDiameter(area, perimeter);
        // calculate deviational value
        deviationValue = sgf / area;

        Point point = new Point(x, y);
        if (points.containsKey(point)) {
            areaPreparation = calculateArea(points.get(point)) / area;
        }
        getAbnormalCellTypes();
    }
}

From source file:uom.research.thalassemia.logic.BloodCellData.java

/**
 * get min and max diameters./*from  w  w  w  .ja v a  2s  .c  o m*/
 *
 * @param currentSelection currentSelection of Mat
 */
private void getMinMaxDiameter(final Mat currentSelection) {
    double[] circles;
    List<Double> diameters = new ArrayList<>();
    double r, diameter;
    for (int a = 0; a < currentSelection.cols(); a++) {
        circles = currentSelection.get(0, a);
        r = Math.round(circles[2]);
        diameter = calculateDiameter(r);
        diameters.add(diameter);
    }
    if (diameters.size() > 1) {
        diameters.sort(null);
    }
    minDiameter = diameters.get(0);
    maxDiameter = diameters.get(diameters.size() - 1);
}