Example usage for org.opencv.imgproc Imgproc getStructuringElement

List of usage examples for org.opencv.imgproc Imgproc getStructuringElement

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc getStructuringElement.

Prototype

public static Mat getStructuringElement(int shape, Size ksize) 

Source Link

Usage

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Apply a morphological operation (e.g., opening, closing) to the image with a given kernel element.
 *
 * See:/*from   w  w w  . j  av a2 s  . co m*/
 * http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html
 * 
 * @param operation
 *    The morphological operation to apply: Imgproc.MORPH_CLOSE, MORPH_OPEN,
 *    MORPH_TOPHAT, MORPH_BLACKHAT, MORPH_GRADIENT.
 * @param kernelElement
 *    The shape to apply the operation with: Imgproc.MORPH_RECT, MORPH_CROSS, or MORPH_ELLIPSE.
 * @param width
 *    Width of the shape.
 * @param height
 *    Height of the shape.
 */
public void morphX(int operation, int kernelElement, int width, int height) {
    Mat kernel = Imgproc.getStructuringElement(kernelElement, new Size(width, height));
    Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), operation, kernel);
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Close the image with a circle of a given size.
 *
 * See:/*w ww  .j  av  a 2s  .  c  om*/
 * http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#closing
 *
 * @param size
 *    Radius of the circle to close with.
 */
public void close(int size) {
    Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
    Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_CLOSE, kernel);
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

/**
 * Open the image with a circle of a given size.
 *
 * See://  ww w .j av  a  2s  . c o m
 * http://docs.opencv.org/doc/tutorials/imgproc/opening_closing_hats/opening_closing_hats.html#opening
 *
 * @param size
 *    Radius of the circle to open with.
 */
public void open(int size) {
    Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(size, size));
    Imgproc.morphologyEx(getCurrentMat(), getCurrentMat(), Imgproc.MORPH_OPEN, kernel);
}

From source file:hotgoaldetection.HotGoalDetection.java

/**
 * @param args the command line arguments
 *//*from  ww  w.  ja va  2  s  .  c o m*/
public static void main(String[] args) throws IOException {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    img = Highgui.imread("..\\Robot-26ft.png", CV_LOAD_IMAGE_COLOR);
    //CameraWindow cWindow = new CameraWindow();
    //cWindow.setVisible(true);
    Webcam.ImagePanel panel = Webcam.createPanel(img, "img");
    Webcam.ImagePanel panel2 = Webcam.createPanel(img, "hsv");
    //Webcam.ImagePanel panel3 = Webcam.createPanel(filter, "filter");

    binary = new Mat();
    hsv = new Mat();
    filter = new Mat();
    dst = new Mat();

    GaussianBlur(img, img, new Size(3, 3), 2, 2);

    while (true) {
        Imgproc.cvtColor(img, hsv, Imgproc.COLOR_BGR2HSV);

        //Core.inRange(img, new Scalar(120, 170, 10), new Scalar(160, 240, 40), filter);
        //Core.inRange(hsv, new Scalar(cWindow.get_hLower(), cWindow.get_sLower(), cWindow.get_vLower()), new Scalar(cWindow.get_hUpper(), cWindow.get_sUpper(), cWindow.get_vUpper()), hsv);
        Core.inRange(hsv, new Scalar(40, 52, 64), new Scalar(97, 255, 255), binary);
        erode(binary, binary, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3, 3)));

        double[] condensedMat = new double[binary.width()];
        for (int y = 0; y < binary.size().width; y++) {
            double totalX = 0;
            for (int x = 0; x < binary.size().height; x++) {
                if (binary.get(x, y)[0] == 255.0)
                    totalX++;
                //System.out.println(java.util.Arrays.toString(binary.get(x,y)));
            }
            condensedMat[y] = totalX;
        }
        double[] rightOfMat = Arrays.copyOfRange(condensedMat, 0, (condensedMat.length / 2) - 1);
        double[] leftOfMat = Arrays.copyOfRange(condensedMat, condensedMat.length / 2, condensedMat.length);
        double totalRight = 0, totalLeft = 0;

        for (double s : rightOfMat)
            totalRight += s;
        for (double s : leftOfMat)
            totalLeft += s;

        //System.out.println(totalRight + ", " + totalLeft);

        if (totalRight > totalLeft)
            direction = true;
        else
            direction = false;

        totalLeft = 0;
        totalRight = 0;

        panel.updateImage(toBufferedImage(img));
        panel2.updateImage(toBufferedImage(binary));
        // panel3.updateImage(toBufferedImage(filter));
    }
}

From source file:jarvis.module.colourtracking.ColourTrackingModule.java

@Override
public void run() {
    while (running) {
        // Get webcam image
        if (cap != null)
            cap.read(imageOriginal);// w  ww  .j ava 2s. com
        // Convert from BGR to HSV
        Imgproc.cvtColor(imageOriginal, imageHSV, Imgproc.COLOR_RGB2HSV);
        // Threshold
        Core.inRange(imageHSV, new Scalar(lowH.getValue(), lowS.getValue(), lowV.getValue()),
                new Scalar(highH.getValue(), highS.getValue(), highV.getValue()), imageThresholded);
        // Remove small objects in the foreground 'morphological opening'
        Mat structure = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(5, 5));
        Imgproc.erode(imageThresholded, imageThresholded, structure);
        Imgproc.dilate(imageThresholded, imageThresholded, structure);
        // Remove holes in the foreground 'morphological closing'
        Imgproc.dilate(imageThresholded, imageThresholded, structure);
        Imgproc.erode(imageThresholded, imageThresholded, structure);

        // Get the spatial moment
        //            Moments moments = Imgproc.moments(imageThresholded);
        //            int area = (int)moments.get_m00();

        Imgproc.cvtColor(imageThresholded, imageThresholded, Imgproc.COLOR_GRAY2RGB);
        if (cap != null)
            frame.getContentPane().getGraphics().drawImage(mat2image.getImage(imageThresholded), 0, 50, null);
        if (cap != null)
            frame.repaint();
    }
}

From source file:kamerka.Filters.java

public void erosion(String sourcePath, int size) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat source = Highgui.imread(sourcePath, Highgui.CV_LOAD_IMAGE_COLOR);
    Mat destination = new Mat(source.rows(), source.cols(), source.type());
    destination = source;//  ww w  . ja  v  a 2 s .co  m
    Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * size + 1, 2 * size + 1));
    Imgproc.erode(source, destination, element);
    Highgui.imwrite(sourcePath, destination);
}

From source file:kamerka.Filters.java

public void dilation(String sourcePath, int size) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat source = Highgui.imread(sourcePath, Highgui.CV_LOAD_IMAGE_COLOR);
    Mat destination = new Mat(source.rows(), source.cols(), source.type());
    destination = source;//w  w  w.j ava  2  s  .  c o m
    Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2 * size + 1, 2 * size + 1));
    Imgproc.dilate(source, destination, element);
    Highgui.imwrite(sourcePath, destination);
}

From source file:karthik.Barcode.Barcode.java

License:Open Source License

protected void connectComponents() {
    // connect large components by doing morph close followed by morph open
    // use larger element size for erosion to remove small elements joined by dilation
    Mat small_elemSE, large_elemSE;// w  w w.  j a v  a 2s.  c o m

    if (searchParams.is_VSmallMatrix) {
        // test out slightly different process for small codes in a large image
        small_elemSE = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, searchParams.elem_size);
        large_elemSE = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, searchParams.large_elem_size);

        Imgproc.dilate(img_details.probabilities, img_details.probabilities, small_elemSE);
        Imgproc.erode(img_details.probabilities, img_details.probabilities, large_elemSE);

        Imgproc.erode(img_details.probabilities, img_details.probabilities, small_elemSE);
        Imgproc.dilate(img_details.probabilities, img_details.probabilities, large_elemSE);
        return;
    }

    small_elemSE = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, searchParams.elem_size);
    large_elemSE = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, searchParams.large_elem_size);

    Imgproc.dilate(img_details.probabilities, img_details.probabilities, small_elemSE);
    Imgproc.erode(img_details.probabilities, img_details.probabilities, large_elemSE);

    Imgproc.erode(img_details.probabilities, img_details.probabilities, small_elemSE);
    Imgproc.dilate(img_details.probabilities, img_details.probabilities, large_elemSE);
}

From source file:logic.featurepointextractor.EyeBrowsFPE.java

/**
 * getSkeleton  obtain thin 1-pixel region from contour. 
 * @param src   input binary image/*from  w w  w.ja  va2s.  co m*/
 * @return      binary image 
 */

private Mat getSkeleton(Mat src) {
    Mat skel = new Mat(src.rows(), src.cols(), CV_8UC1, new Scalar(0));
    Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(3, 3));
    Mat tmp = new Mat();
    Mat eroded = new Mat();
    boolean done = false;

    do {
        Imgproc.morphologyEx(src, eroded, Imgproc.MORPH_ERODE, element);
        Imgproc.morphologyEx(eroded, tmp, Imgproc.MORPH_DILATE, element);
        Core.subtract(src, tmp, tmp);
        Core.bitwise_or(skel, tmp, skel);
        eroded.copyTo(src);

        done = (Core.countNonZero(src) == 0);
    } while (!done);

    return skel;
}

From source file:logic.imagelocalizator.EyeBrowsLocalizator.java

private boolean detectEyeBrowBoundRect(MatContainer mc) {
    int eyePairW = mc.eyePairRect.width;
    int eyePairH = mc.eyePairRect.height;

    //contains eyebrow bounding rectangles
    Rect boundRectArr[] = new Rect[2];

    //for each eyebrow
    Mat binMat = new Mat();
    for (int i = 0; i < 2; ++i) {
        mc.eyeBrowMatArr[i] = mc.grayFrame.submat(mc.eyeBrowRectArr[i]);
        Scalar meanScalar = Core.mean(mc.eyeBrowMatArr[i]);
        //negate image
        Core.convertScaleAbs(mc.eyeBrowMatArr[i], mc.eyeBrowMatArr[i], 1, 255 - meanScalar.val[0]);
        Imgproc.equalizeHist(mc.eyeBrowMatArr[i], mc.eyeBrowMatArr[i]);
        Imgproc.blur(mc.eyeBrowMatArr[i], mc.eyeBrowMatArr[i], new Size(4, 4));

        //obtain binary image
        Imgproc.threshold(mc.eyeBrowMatArr[i], binMat, 70, 255, Imgproc.THRESH_BINARY_INV);

        Imgproc.morphologyEx(binMat, binMat, Imgproc.MORPH_OPEN,
                Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(4, 4)));

        //find contours
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Imgproc.findContours(binMat, contours, new Mat(), Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);

        //find the biggest contour
        int maxSize = -1;
        int tmpSize = -1;
        int index = -1;

        if (contours.size() != 0) {
            maxSize = contours.get(0).toArray().length;
            tmpSize = 0;/*from ww w  . j a  va  2  s  .c o  m*/
            index = 0;
        }

        //find max contour
        for (int j = 0; j < contours.size(); ++j) {
            //if contour is vertical, exclude it 
            Rect boundRect = Imgproc.boundingRect(contours.get(j));
            if (boundRect.height > boundRect.width)
                continue;

            if ((double) boundRect.height
                    / (double) mc.eyeBrowRectArr[i].height > Parameters.eyebrowBoundRectThresh) {
                LOG.warn("Reset brow rect");
                mc.eyeBrowBoundRectArr[i] = null;
                return false;
            }

            tmpSize = contours.get(j).toArray().length;

            LOG.info("Contour " + j + "; size = " + tmpSize);

            if (tmpSize > maxSize) {
                maxSize = tmpSize;
                index = j;
            }
        }

        binMat.setTo(new Scalar(0));
        boundRectArr[i] = Imgproc.boundingRect(contours.get(index));

        //save eyebrow bounding rectangle
        mc.eyeBrowBoundRectArr[i] = new Rect(mc.eyeBrowRectArr[i].x + boundRectArr[i].x,
                mc.eyeBrowRectArr[i].y + boundRectArr[i].y, boundRectArr[i].width, boundRectArr[i].height);

        //save binary eyebrow Mat for further FP detection (skeletonization)
        mc.eyeBrowBinMatArr[0] = binMat;

        //define tracking template for eyebrow
        mc.eyeBrowTrackingTemplateArr[i] = mc.grayFrame.submat(mc.eyeBrowBoundRectArr[i]);
    }

    //compute eyebrow interrocular distance
    mc.eyeBrowBaseDst = Math.abs(mc.eyeBrowBoundRectArr[0].x + mc.eyeBrowBoundRectArr[0].width / 2
            - (mc.eyeBrowBoundRectArr[1].x + mc.eyeBrowBoundRectArr[1].width / 2));

    LOG.info("eyeBrowBaseDst = " + mc.eyeBrowBaseDst);

    //define new bound rect centers for tracking template
    mc.eyeBrowCentersPointsArr = new Point[2];

    return true;
}