Example usage for org.opencv.core Core split

List of usage examples for org.opencv.core Core split

Introduction

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

Prototype

public static void split(Mat m, List<Mat> mv) 

Source Link

Usage

From source file:Retrive.java

public Mat query_histo(Mat query_img) {
    Vector<Mat> bgr_planes = new Vector<>();
    Core.split(query_img, bgr_planes);

    MatOfInt histSize = new MatOfInt(256);
    final MatOfFloat histRange = new MatOfFloat(0f, 256f);
    //boolean accumulate = false;
    Mat q_hist = new Mat();
    int hist_w = 512;
    int hist_h = 600;
    long bin_w;/* w w  w .  j a  v  a2 s.  c  om*/
    bin_w = Math.round((double) (hist_w / 256));

    Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3);

    Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), q_hist, histSize, histRange);
    Core.normalize(q_hist, q_hist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    return q_hist;
}

From source file:Retrive.java

public Mat histo(Mat imgs) {
    Vector<Mat> bgr_planes = new Vector<>();
    Core.split(imgs, bgr_planes);

    MatOfInt histSize = new MatOfInt(256);
    final MatOfFloat histRange = new MatOfFloat(0f, 256f);
    boolean accumulate = false;
    Mat b_hist = new Mat();
    int hist_w = 512;
    int hist_h = 600;
    //long bin_w;
    //bin_w = Math.round((double) (hist_w / 256));

    Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC3);

    Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), b_hist, histSize, histRange, accumulate);
    Core.normalize(b_hist, b_hist, 0, histImage.rows(), Core.NORM_MINMAX, -1, new Mat());
    return b_hist;
}

From source file:airhockeyjava.detection.PS3EyeFrameGrabber.java

License:Open Source License

/**
 * Grab one frame; the caller have to make a copy of returned image before
 * processing./* www.  jav  a  2s.c o  m*/
 * 
 * It will throw null pointer exception if not started before grabbing.
 * 
 * @return "read-only" RGB, 3-channel
 */
public Mat grabMat() {
    Mat matImg = new Mat(this.imageHeight, this.imageWidth, CvType.CV_8UC4);
    int[] img = grab_RGB4();
    ByteBuffer byteBuffer = ByteBuffer.allocate(img.length * 4);
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(img);

    byte[] array = byteBuffer.array();
    matImg.put(0, 0, array);
    List<Mat> mv = new ArrayList<Mat>();
    Core.split(matImg, mv);
    mv.remove(0);
    Core.merge(mv, matImg);
    return matImg;

}

From source file:Clases.Segmentador.java

public Mat descompCanImg(Mat grayscale, int canal) {
    List<Mat> rgb = new ArrayList<Mat>(3);
    Core.split(grayscale, rgb);
    return rgb.get(canal);
}

From source file:classes.FloodFiller.java

private void fillFrom(Point seed, int lo, int up, Scalar backgroundColor, Scalar contourFillingColor) {

    Mat object = ObjectGenerator.extract(image, seed.x, seed.y, 10, 10);
    this.meanColor = Core.mean(object);

    Rect ccomp = new Rect();
    Mat mask = Mat.zeros(image.rows() + 2, image.cols() + 2, CvType.CV_8UC1);

    int connectivity = 4;
    int newMaskVal = 255;
    int ffillMode = 1;

    int flags = connectivity + (newMaskVal << 8) + (ffillMode == 1 ? Imgproc.FLOODFILL_FIXED_RANGE : 0);

    Scalar newVal = new Scalar(0.299, 0.587, 0.114);

    Imgproc.threshold(mask, mask, 1, 128, Imgproc.THRESH_BINARY);

    filledArea = Imgproc.floodFill(image.clone(), mask, seed, newVal, ccomp, new Scalar(lo, lo, lo),
            new Scalar(up, up, up), flags);

    //        Highgui.imwrite("mask.png", mask);
    ImageUtils.saveImage(mask, "mask.png", request);

    morphologicalImage = new Mat(image.size(), CvType.CV_8UC3);

    Mat element = new Mat(3, 3, CvType.CV_8U, new Scalar(1));

    ArrayList<Mat> mask3 = new ArrayList<Mat>();
    mask3.add(mask);//from   w ww. java 2  s . c  o  m
    mask3.add(mask);
    mask3.add(mask);
    Core.merge(mask3, mask);

    // Applying morphological filters
    Imgproc.erode(mask, morphologicalImage, element);
    Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_CLOSE, element,
            new Point(-1, -1), 9);
    Imgproc.morphologyEx(morphologicalImage, morphologicalImage, Imgproc.MORPH_OPEN, element, new Point(-1, -1),
            2);
    Imgproc.resize(morphologicalImage, morphologicalImage, image.size());

    //        Highgui.imwrite("morphologicalImage.png", morphologicalImage);
    ImageUtils.saveImage(morphologicalImage, "morphologicalImage.png", request);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();

    Core.split(mask, mask3);
    Mat binarymorphologicalImage = mask3.get(0);

    Imgproc.findContours(binarymorphologicalImage.clone(), contours, new Mat(), Imgproc.RETR_EXTERNAL,
            Imgproc.CHAIN_APPROX_NONE);

    contoursImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor);

    int thickness = -1; // Thicknes should be lower than zero in order to drawn the filled contours
    Imgproc.drawContours(contoursImage, contours, -1, contourFillingColor, thickness); // Drawing all the contours found
    //        Highgui.imwrite("allContoursImage.png", contoursImage);
    ImageUtils.saveImage(contoursImage, "allContoursImage.png", request);

    if (contours.size() > 1) {

        int minContourWith = 20;
        int minContourHeight = 20;
        int maxContourWith = 6400 / 2;
        int maxContourHeight = 4800 / 2;

        contours = filterContours(contours, minContourWith, minContourHeight, maxContourWith, maxContourHeight);
    }

    if (contours.size() > 0) {

        MatOfPoint biggestContour = contours.get(0); // getting the biggest contour
        contourArea = Imgproc.contourArea(biggestContour);

        if (contours.size() > 1) {
            biggestContour = Collections.max(contours, new ContourComparator()); // getting the biggest contour in case there are more than one
        }

        Point[] points = biggestContour.toArray();
        path = "M " + (int) points[0].x + " " + (int) points[0].y + " ";
        for (int i = 1; i < points.length; ++i) {
            Point v = points[i];
            path += "L " + (int) v.x + " " + (int) v.y + " ";
        }
        path += "Z";

        biggestContourImage = new Mat(image.size(), CvType.CV_8UC3, backgroundColor);

        Imgproc.drawContours(biggestContourImage, contours, 0, contourFillingColor, thickness);

        //            Highgui.imwrite("biggestContourImage.png", biggestContourImage);
        ImageUtils.saveImage(biggestContourImage, "biggestContourImage.png", request);

        Mat maskForColorExtraction = biggestContourImage.clone();

        if (isWhite(backgroundColor)) {
            Imgproc.dilate(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3);
        } else {
            Imgproc.erode(maskForColorExtraction, maskForColorExtraction, new Mat(), new Point(-1, -1), 3);
        }

        //            Highgui.imwrite("maskForColorExtraction.png", maskForColorExtraction);
        ImageUtils.saveImage(maskForColorExtraction, "maskForColorExtraction.png", request);

        Mat extractedColor = new Mat();

        if (isBlack(backgroundColor) && isWhite(contourFillingColor)) {
            Core.bitwise_and(maskForColorExtraction, image, extractedColor);

        } else {
            Core.bitwise_or(maskForColorExtraction, image, extractedColor);
        }

        //            Highgui.imwrite("extractedColor.png", extractedColor);
        ImageUtils.saveImage(extractedColor, "extractedColor.png", request);

        computedSearchWindow = Imgproc.boundingRect(biggestContour);
        topLeftCorner = computedSearchWindow.tl();

        Rect croppingRect = new Rect(computedSearchWindow.x, computedSearchWindow.y,
                computedSearchWindow.width - 1, computedSearchWindow.height - 1);

        Mat imageForTextRecognition = new Mat(extractedColor.clone(), croppingRect);
        //            Highgui.imwrite(outImageName, imageForTextRecognition);
        ImageUtils.saveImage(imageForTextRecognition, outImageName, request);

        //            
        //
        //            Mat data = new Mat(imageForTextRecognition.size(), CvType.CV_8UC3, backgroundColor);
        //            imageForTextRecognition.copyTo(data);
        //            data.convertTo(data, CvType.CV_8UC3);
        //
        //            // The meanColor variable represents the color in the GBR space, the following line transforms this to the RGB color space, which
        //            // is assumed in the prepareImage method of the TextRecognitionPreparer class
        //            Scalar userColor = new Scalar(meanColor.val[2], meanColor.val[1], meanColor.val[0]);
        //
        //            ArrayList<String> recognizableImageNames = TextRecognitionPreparer.generateRecognizableImagesNames(data, backgroundColor, userColor);
        //            for (String imageName : recognizableImageNames) {
        //
        //                try {
        //                    // First recognition step
        //                    String recognizedText = TextRecognizer.recognize(imageName, true).trim();
        //                    if (recognizedText != null && !recognizedText.isEmpty()) {
        //                        recognizedStrings.add(recognizedText);
        //                    }
        //                    // Second recognition step
        //                    recognizedText = TextRecognizer.recognize(imageName, false).trim();
        //                    if (recognizedText != null && !recognizedText.isEmpty()) {
        //                        recognizedStrings.add(recognizedText);
        //                    }
        //                    
        //                } catch (Exception e) {
        //                }
        //            }
        //            
        ////            ArrayList<BufferedImage> recognizableBufferedImages = TextRecognitionPreparer.generateRecognizableBufferedImages(data, backgroundColor, userColor);
        ////            for (BufferedImage bufferedImage : recognizableBufferedImages) {
        ////                try {
        ////                    // First recognition step
        ////                    String recognizedText = TextRecognizer.recognize(bufferedImage, true).trim();
        ////                    if (recognizedText != null && !recognizedText.isEmpty()) {
        ////                        recognizedStrings.add(recognizedText);
        ////                    }
        ////                    // Second recognition step
        ////                    recognizedText = TextRecognizer.recognize(bufferedImage, false).trim();
        ////                    if (recognizedText != null && !recognizedText.isEmpty()) {
        ////                        recognizedStrings.add(recognizedText);
        ////                    }
        ////                    
        ////                } catch (Exception e) {
        ////                }
        ////            }
        //
        //            
        //            

        // compute all moments
        Moments mom = Imgproc.moments(biggestContour);
        massCenter = new Point(mom.get_m10() / mom.get_m00(), mom.get_m01() / mom.get_m00());

        // draw black dot
        Core.circle(contoursImage, massCenter, 4, contourFillingColor, 8);
    }

}

From source file:classes.TextRecognitionPreparer.java

public static Mat equalizeIntensity(Mat inputImage) {

    if (inputImage.channels() >= 3) {

        Mat ycrcb = new Mat();

        Imgproc.cvtColor(inputImage, ycrcb, Imgproc.COLOR_BGR2YUV);

        ArrayList<Mat> channels = new ArrayList<Mat>();

        Core.split(ycrcb, channels);

        Mat equalized = new Mat();

        Imgproc.equalizeHist(channels.get(0), equalized);

        channels.set(0, equalized);// w w  w .ja v a 2 s  . c om
        Core.merge(channels, ycrcb);

        Mat result = new Mat();
        Imgproc.cvtColor(ycrcb, result, Imgproc.COLOR_YUV2BGR);

        return result;
    }

    return null;
}

From source file:com.shootoff.camera.Camera.java

License:Open Source License

public static Mat colorTransfer(Mat source, Mat target) {
    Mat src = new Mat();
    Mat dst = new Mat();

    Imgproc.cvtColor(source, src, Imgproc.COLOR_BGR2Lab);
    Imgproc.cvtColor(target, dst, Imgproc.COLOR_BGR2Lab);

    ArrayList<Mat> src_channels = new ArrayList<Mat>();
    ArrayList<Mat> dst_channels = new ArrayList<Mat>();
    Core.split(src, src_channels);
    Core.split(dst, dst_channels);/*from   ww w .j  a  v  a  2s. c o m*/

    for (int i = 0; i < 3; i++) {
        MatOfDouble src_mean = new MatOfDouble(), src_std = new MatOfDouble();
        MatOfDouble dst_mean = new MatOfDouble(), dst_std = new MatOfDouble();
        Core.meanStdDev(src_channels.get(i), src_mean, src_std);
        Core.meanStdDev(dst_channels.get(i), dst_mean, dst_std);

        dst_channels.get(i).convertTo(dst_channels.get(i), CvType.CV_64FC1);
        Core.subtract(dst_channels.get(i), dst_mean, dst_channels.get(i));
        Core.divide(dst_std, src_std, dst_std);
        Core.multiply(dst_channels.get(i), dst_std, dst_channels.get(i));
        Core.add(dst_channels.get(i), src_mean, dst_channels.get(i));
        dst_channels.get(i).convertTo(dst_channels.get(i), CvType.CV_8UC1);
    }

    Core.merge(dst_channels, dst);

    Imgproc.cvtColor(dst, dst, Imgproc.COLOR_Lab2BGR);

    return dst;
}

From source file:edu.soict.hust.k57.mmdb.components.HistogramCaculator.java

@Override
public void accept(ImgEnt t) {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
    Mat m = Imgcodecs.imread(t.getF().getPath());
    List<Mat> images = new ArrayList<Mat>();
    Core.split(m, images);

    MatOfInt histSize = new MatOfInt(t.getBin()); // kch thc ca histogram
    MatOfInt channels = new MatOfInt(0); // Knh mu mun tnh
    MatOfFloat histRange = new MatOfFloat(0, 256);

    Mat bHist = new Mat();
    Mat gHist = new Mat();
    Mat rHist = new Mat();

    Imgproc.calcHist(images.subList(0, 1), channels, new Mat(), bHist, histSize, histRange, false);
    Core.normalize(bHist, bHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Imgproc.calcHist(images.subList(1, 2), channels, new Mat(), gHist, histSize, histRange, false);
    Core.normalize(gHist, gHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Imgproc.calcHist(images.subList(2, 3), channels, new Mat(), rHist, histSize, histRange, false);
    Core.normalize(rHist, rHist, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    t.setbHistogram(bHist);//from  w w w.j  av a2  s  . c  om
    t.setgHistogram(gHist);
    t.setrHistogram(rHist);
}

From source file:edu.sust.cse.util.Histogram.java

public static Mat getHistogram(Mat image) {

    try {/*  w w  w.  ja  va  2  s  .c o  m*/
        Mat src = new Mat(image.height(), image.width(), CvType.CV_8UC2);
        Imgproc.cvtColor(image, src, Imgproc.COLOR_RGB2GRAY);
        ArrayList<Mat> bgr_planes = new ArrayList<>();
        Core.split(src, bgr_planes);

        MatOfInt histSize = new MatOfInt(256);

        final MatOfFloat histRange = new MatOfFloat(0f, 256f);

        boolean accumulate = false;

        Mat b_hist = new Mat();

        Imgproc.calcHist(bgr_planes, new MatOfInt(0), new Mat(), b_hist, histSize, histRange, accumulate);

        int hist_w = 512;
        int hist_h = 600;
        long bin_w;
        bin_w = Math.round((double) (hist_w / 256));

        Mat histImage = new Mat(hist_h, hist_w, CvType.CV_8UC1);

        Core.normalize(b_hist, b_hist, 3, histImage.rows(), Core.NORM_MINMAX);

        for (int i = 1; i < 256; i++) {

            Core.line(histImage, new Point(bin_w * (i - 1), hist_h - Math.round(b_hist.get(i - 1, 0)[0])),
                    new Point(bin_w * (i), hist_h - Math.round(Math.round(b_hist.get(i, 0)[0]))),
                    new Scalar(255, 0, 0), 2, 8, 0);

        }

        return histImage;
    } catch (Exception ex) {
        System.out.println("[HISTOGRAM][ERROR][" + ex.getMessage() + "]");
        return null;
    }
}

From source file:es.ugr.osgiliart.features.opencv.Histogram.java

License:Open Source License

@Override
public double[] extract(Mat image) {
    Mat hsvImage = new Mat(image.width(), image.height(), image.type());
    Mat histHue = new Mat();
    Mat histSaturation = new Mat();

    Imgproc.cvtColor(image, hsvImage, Imgproc.COLOR_BGR2HSV);
    List<Mat> channels = new ArrayList<Mat>();
    Core.split(hsvImage, channels);

    //Histogram for hue
    Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(0) }), new MatOfInt(0), new Mat(), histHue,
            new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));

    //Histogram for saturation
    Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(1) }), new MatOfInt(0), new Mat(), histSaturation,
            new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));

    double sum = Core.sumElems(histHue).val[0];
    double[] values = new double[histHue.height() + histSaturation.height()];
    int k = 0;//from  w w  w.  j av a  2  s. c  om
    for (int i = 0; i < histHue.height(); ++i) {
        values[k++] = histHue.get(i, 0)[0] / sum;
    }
    sum = Core.sumElems(histSaturation).val[0];
    for (int i = 0; i < histSaturation.height(); ++i) {
        values[k++] = histSaturation.get(i, 0)[0] / sum;
    }
    return values;
}