Example usage for org.opencv.core Core merge

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

Introduction

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

Prototype

public static void merge(List<Mat> mv, Mat dst) 

Source Link

Usage

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.//  w  w w. jav a2 s  .  co 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: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   ww  w . j ava  2s . co  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);//from  ww  w . jav  a 2s  .c o m

        Mat equalized = new Mat();

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

        channels.set(0, equalized);
        Core.merge(channels, ycrcb);

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

        return result;
    }

    return null;
}

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

License:Open Source License

private Mat applyKmeans(Mat source) {
    Mat dest = new Mat();

    source.convertTo(source, CvType.CV_32F, 1.0 / 255.0);

    Mat centers = new Mat();
    Mat labels = new Mat();
    TermCriteria criteria = new TermCriteria(TermCriteria.COUNT, 20, 0.1);
    Core.kmeans(source, 4, labels, criteria, 10, Core.KMEANS_PP_CENTERS, centers);

    List<Mat> mats = showClusters(source, labels, centers);
    //mats.get(0).convertTo(dest, CvType.CV_8UC3);
    Core.merge(mats, dest);
    //centers.convertTo(dest, CvType.CV_8UC3);
    return dest;/*from ww w . jav a 2 s.  c o  m*/
}

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);//from  w ww.j  a va 2 s .  co m
    Core.split(dst, dst_channels);

    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:de.hftl_projekt.ict.MainActivity.java

/**
 * method to reduce the color (quantize) the given matrix (image)
 * @param image input matrix/*from   ww  w.  j  av  a 2  s.  c om*/
 * @return modified input matrix
 */
public Mat reduceColors(Mat image) {
    if (channels.size() == 0) {
        for (int i = 0; i < image.channels(); i++) {
            Mat channel = new Mat(); // fill array with a matrix for each channel
            channels.add(channel);
        }

    }
    int i = 0;
    // process each channel individually
    for (Mat c : channels) {
        Core.extractChannel(image, c, i);
        // binary quantization (set threshold so each color (R, G, B) can have the value (0 or 255) )
        // and using the Otsu algorithm to optimize the quantization
        Imgproc.threshold(c, c, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU);
        i++;
    }
    Core.merge(channels, image); // put the channel back together
    return image;
}

From source file:gab.opencv.OpenCV.java

License:Open Source License

public static void ARGBtoBGRA(Mat rgba, Mat bgra) {
    ArrayList<Mat> channels = new ArrayList<Mat>();
    Core.split(rgba, channels);//w  ww .  j a va 2s.  c  o  m

    ArrayList<Mat> reordered = new ArrayList<Mat>();
    // Starts as ARGB. 
    // Make into BGRA.

    reordered.add(channels.get(3));
    reordered.add(channels.get(2));
    reordered.add(channels.get(1));
    reordered.add(channels.get(0));

    Core.merge(reordered, bgra);
}

From source file:org.surmon.pattern.editor2d.components.Mapping.java

public static List<MatOfPoint> process(Mat source, List<Particle> particles) {

    Mat partImage = new Mat(source.size(), CvType.CV_8UC1);

    // Draw particles as images
    Point p;/*  ww w.j av a  2  s  . c  om*/
    for (Particle part : particles) {
        p = new Point(part.getPosition().toArray());
        Core.circle(partImage, p, 1, new Scalar(255));
    }

    // Blur with Gaussian kernel
    Mat blured = new Mat();
    Imgproc.GaussianBlur(partImage, blured, new Size(101, 101), -1, -1);

    // Equalize histogram
    List<Mat> eqChannels = new ArrayList<>();
    List<Mat> channels = new ArrayList<>();
    Core.split(blured, channels);

    for (Mat channel : channels) {
        Mat eqImage = new Mat();
        Imgproc.equalizeHist(channel, eqImage);
        eqChannels.add(eqImage);
    }
    Mat eqResult = new Mat();
    Core.merge(eqChannels, eqResult);

    // Binary threshold
    Mat bin = new Mat();
    Imgproc.threshold(eqResult, bin, 0, 255, Imgproc.THRESH_OTSU);
    //        Imgproc.threshold(eqResult, bin, 10, 255, Imgproc.THRESH_BINARY);

    // Find contours
    Mat imMat = bin.clone();
    Mat canny_output = new Mat();
    Mat hierarchy = new Mat();
    int thresh = 100;
    //median filter:
    List<MatOfPoint> borders = new ArrayList<>();
    Imgproc.Canny(imMat, canny_output, thresh, thresh * 2);
    Imgproc.findContours(canny_output, borders, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // Find contours

    return borders;

    //        Mat result = source.clone();
    //        Imgproc.drawContours(result, borders, -1, new Scalar(255, 0, 255));
    //        
    //        return result;
}