Example usage for org.opencv.core Mat Mat

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

Introduction

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

Prototype

public Mat() 

Source Link

Usage

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Applies morphological erosion.//w w w .  ja  v  a2  s . c  o m
 *
 * @param src - source image.
 * @param radius - radius of structure element.
 * @return eroded image.
 */
public static Mat erode(Mat src, int radius) {
    Mat dest = new Mat();
    int kernelSize = radius * 2 + 1;
    Mat kernel = getStructuringElement(Imgproc.CV_SHAPE_ELLIPSE, new Size(kernelSize, kernelSize),
            new Point(radius, radius));
    Imgproc.erode(src, dest, kernel);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Applies morphological dilation./*from w w  w.  j  a va2  s. c o m*/
 *
 * @param src - source image.
 * @param radius - radius of structure element.
 * @return dilated image.
 */
public static Mat dilate(Mat src, int radius) {
    Mat dest = new Mat();
    int kernelSize = radius * 2 + 1;
    Mat kernel = getStructuringElement(Imgproc.CV_SHAPE_ELLIPSE, new Size(kernelSize, kernelSize),
            new Point(radius, radius));
    Imgproc.dilate(src, dest, kernel);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

public static Mat xor(Mat first, Mat second) {
    Mat dest = new Mat();
    Core.bitwise_xor(first, second, dest);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

public static Mat and(Mat first, Mat second) {
    Mat dest = new Mat();
    Core.bitwise_and(first, second, dest);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

public static Mat or(Mat first, Mat second) {
    Mat dest = new Mat();
    Core.bitwise_or(first, second, dest);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Equalizes a histogram for the image (color).
 *
 * @param src - color image to be applyed auto contrast.
 * @return the source image with equalized histogram.
 *//*from w ww .  j  ava  2  s.com*/
public static Mat equalize(Mat src) {
    Mat dest = new Mat();
    Imgproc.equalizeHist(grayscale(src), dest);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Remove all small contours on binary image with areas less than specified threshold.
 *
 * @param src - binary source image./*from   w w w.j  a v  a2 s  . c o m*/
 * @param thresh - minimum area of contour.
 * @return a source image with removed all contours with area less than {@param thresh}.
 */
public static Mat clearContours(Mat src, int thresh) {
    if (src.channels() > 1)
        return src;

    Mat dest = src.clone();
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();

    findContours(src, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_TC89_L1);

    Mat maskWhite = new Mat(src.rows(), src.cols(), CvType.CV_8UC1, new Scalar(255));
    Mat maskBlack = maskWhite.clone();

    for (int i = 0; i < contours.size(); i++) {
        Double contourArea = contourArea(contours.get(i));

        if (contourArea < thresh) {
            int pixelColor = averageIntensity(src, contours.get(i));
            drawContours(pixelColor > 127 ? maskWhite : maskBlack, contours, i, new Scalar(0), Core.FILLED);
        }
    }

    maskWhite = erode(maskWhite, 2);
    maskBlack = erode(maskBlack, 2);
    dest = and(maskWhite, dest);
    dest = or(invert(maskBlack), dest);

    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Apply Gaussian blur on source image.// ww w. j a  v  a 2s .c  o  m
 *
 * @param src - source image.
 * @param size - kernel's size (must be odd).
 * @return image with applied Gaussian blur.
 */
public static Mat gaussianBlur(Mat src, int size) {
    Mat dest = new Mat();
    GaussianBlur(src, dest, new Size(size, size), 1.4, 1.4);
    return dest;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Apply Canny edge detector for image with given min and max thresholds.
 *
 * @param src - source image for applying filter.
 * @param minThresh - minimal threshold for filter.
 * @param maxThresh - maximum threshold for filter.
 * @return output image after applying Canny filter for {@param src} image.
 *//*from   www .j a  va2  s .c  om*/
public static Mat cannyFilter(Mat src, int minThresh, int maxThresh) {
    Mat result = new Mat();

    minThresh = Math.min(Math.max(minThresh, 0), 255);
    maxThresh = Math.max(Math.min(maxThresh, 255), 0);

    Mat blurredImage = gaussianBlur(grayscale(src), 9);
    Canny(blurredImage, result, minThresh, maxThresh, 3, true);

    return result;
}

From source file:com.astrocytes.core.operationsengine.CoreOperations.java

License:Open Source License

/**
 * Draw all contours of source image./*  w ww  . j av a 2 s.  c  om*/
 *
 * @param src - source image.
 * @return binary image with black background and white contours.
 */
public static int drawAllContours(Mat src, Mat dest) {
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    findContours(grayscale(src), contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_TC89_L1);
    for (int i = 0; i < contours.size(); i++) {
        drawContours(dest, contours, i, new Scalar(255));
    }

    return contours.size();
}