Example usage for org.opencv.core Scalar Scalar

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

Introduction

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

Prototype

public Scalar(double v0, double v1, double v2) 

Source Link

Usage

From source file:classes.FloodFiller.java

public void fillFrom(Point seed, int lo, int up) {

    Scalar backgroundColor = new Scalar(255, 255, 255); // black background
    Scalar contourFillingColor = new Scalar(0, 0, 0); // white filled contour
    fillFrom(seed, lo, up, backgroundColor, contourFillingColor);

    backgroundColor = new Scalar(0, 0, 0); // white background
    contourFillingColor = new Scalar(255, 255, 255); // black filled contour
    fillFrom(seed, lo, up, backgroundColor, contourFillingColor);

}

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 .  ja  v  a2  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.FloodFillFacade.java

public Mat fill(Mat image, Mat mask, int x, int y, Scalar newVal) {

    Point seedPoint = new Point(x, y);

    Rect rect = new Rect();

    //        Scalar newVal = isColored() ? new Scalar(b, g, r) : new Scalar(r * 0.299 + g * 0.587 + b * 0.114);
    Scalar lowerDifference = new Scalar(lowerDiff, lowerDiff, lowerDiff);
    Scalar upperDifference = new Scalar(upperDiff, upperDiff, upperDiff);
    if (range == NULL_RANGE) {
        lowerDifference = new Scalar(0, 0, 0);
        upperDifference = new Scalar(0, 0, 0);
    }/*from   w w w .j  a  v  a 2s  .com*/
    int flags = connectivity + (newMaskVal << 8)
            + ((range == FIXED_RANGE ? Imgproc.FLOODFILL_FIXED_RANGE : 0) | 0);//Imgproc.FLOODFILL_MASK_ONLY);
    int area = 0;
    if (masked) {
        area = Imgproc.floodFill(image, mask, seedPoint, newVal, rect, lowerDifference, upperDifference, flags);
    } else {
        area = Imgproc.floodFill(image, new Mat(), seedPoint, newVal, rect, lowerDifference, upperDifference,
                flags);
    }

    //        Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\image_after_flood_" + cont + ".png", image);
    //        Highgui.imwrite("C:\\Users\\Gonzalo\\Documents\\NetBeansProjects\\iVoLVER\\uploads\\mask_" + cont + ".png", mask);

    //        System.out.println("area: " + area);

    cont++;

    return image;

}

From source file:classes.ObjectFinder.java

private void computeObjectHistogram() {
    // Converting the current fram to HSV color space
    Mat hsvImage = new Mat(this.objectImage.size(), CvType.CV_8UC3);

    System.out.println(this.objectImage);

    Imgproc.cvtColor(this.objectImage, hsvImage, Imgproc.COLOR_BGR2HSV);

    // Getting the pixels that are in te specified ranges
    Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1);
    int hmin = thresholdsVector.get(0);
    int hmax = thresholdsVector.get(1);
    int smin = thresholdsVector.get(2);
    int smax = thresholdsVector.get(3);
    int vmin = thresholdsVector.get(4);
    int vmax = thresholdsVector.get(5);

    Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage);

    Mat hueImage = new Mat(hsvImage.size(), CvType.CV_8UC1);

    MatOfInt fromto = new MatOfInt(0, 0);
    Core.mixChannels(Arrays.asList(hsvImage), Arrays.asList(hueImage), fromto);

    MatOfInt sizes = new MatOfInt(16);
    MatOfFloat ranges = new MatOfFloat(0, 180);
    MatOfInt channels = new MatOfInt(0);

    Mat histogram = new Mat();
    boolean accumulate = false;
    Imgproc.calcHist(Arrays.asList(hueImage), channels, maskImage, histogram, sizes, ranges, accumulate);

    Highgui.imwrite("histogram.png", histogram);

    // The resulting histogram is normalized and placed in the class variable
    Core.normalize(histogram, objectHistogram, 0, 255, Core.NORM_MINMAX);

}

From source file:classes.ObjectFinder.java

private void backprojectObjectHistogram() {

    // Converting the current fram to HSV color space
    Mat hsvImage = new Mat(this.objectImage.size(), CvType.CV_8UC3);

    Imgproc.cvtColor(this.inputFrame, hsvImage, Imgproc.COLOR_BGR2HSV);

    // Getting the pixels that are in te specified ranges    
    int hmin = this.thresholdsVector.get(0);
    int hmax = this.thresholdsVector.get(1);
    int smin = this.thresholdsVector.get(2);
    int smax = this.thresholdsVector.get(3);
    int vmin = this.thresholdsVector.get(4);
    int vmax = this.thresholdsVector.get(5);

    Mat maskImage = new Mat(this.objectImage.size(), CvType.CV_8UC1);
    Core.inRange(hsvImage, new Scalar(hmin, smin, vmin), new Scalar(hmax, smax, vmax), maskImage);

    // Taking the hue channel of the image
    Mat hueImage = new Mat(hsvImage.size(), hsvImage.depth());

    MatOfInt fromto = new MatOfInt(0, 0);
    Core.mixChannels(Arrays.asList(hsvImage), Arrays.asList(hueImage), fromto);

    // Backprojecting the histogram over that hue channel image
    MatOfFloat ranges = new MatOfFloat(0, 180);
    MatOfInt channels = new MatOfInt(0);

    Imgproc.calcBackProject(Arrays.asList(hueImage), channels, this.objectHistogram, this.backprojectionImage,
            ranges, 1);//from  ww w.j av a2s  .c  om

    Core.bitwise_and(backprojectionImage, maskImage, backprojectionImage);

}

From source file:classes.ObjectFinder.java

private void computeTrackBox() {

    trackBox = new RotatedRect();

    if (computedSearchWindow.size().width > 0 && computedSearchWindow.size().height > 0
            && computedSearchWindow.area() > 1) {
        trackBox = Video.CamShift(thresholdedBackprojection, computedSearchWindow,
                new TermCriteria(2 | 1, 10, 1));
    }//from w  w  w.  j  ava  2  s. c o  m

    if (trackBox.size.width > 0 && trackBox.size.height > 0 && trackBox.size.area() > 1) {
        Core.ellipse(inputFrame, trackBox, new Scalar(0, 0, 255), 2);
    }

}

From source file:classes.ObjectFinder.java

private void drawOutput(Mat output) {

    Point center;/*from w w w .  j  av  a2s . com*/

    int fontFace = Core.FONT_HERSHEY_SIMPLEX;
    double fontScale = 0.7;
    int thickness = 1;

    Core.ellipse(output, trackBox, new Scalar(0, 0, 255), 2);

    center = new Point(trackBox.center.x, trackBox.center.y);

    System.out.println("center: " + center);

    Core.circle(output, center, 2, new Scalar(255, 0, 0), 2);

    Core.circle(output, massCenter, 2, new Scalar(0, 255, 0), 2);

    if (this.objectName != null) {
        Core.putText(output, getObjectName(), center, fontFace, fontScale, new Scalar(0, 0, 0), thickness, 8,
                false);
    }

}

From source file:classes.TextRecognitionPreparer.java

public static ArrayList<String> generateRecognizableImagesNames(Mat img, Scalar userPickedColor, String imageID,
        HttpServletRequest request) {/* www  .j  a  va  2  s .  c  o m*/

    ArrayList<String> imageNames = new ArrayList<String>();

    Mat filledImage = img.clone();
    Scalar newVal = new Scalar(userPickedColor.val[2], userPickedColor.val[1], userPickedColor.val[0]);

    Imgproc.floodFill(filledImage, new Mat(), new Point(0, 0), newVal);
    String file1 = imageID + "_filledImage.png";
    //        Highgui.imwrite(file1, filledImage);
    imageNames.add(ImageUtils.saveImage(filledImage, file1, request));

    Mat filledGrayImage = new Mat();
    Imgproc.cvtColor(filledImage, filledGrayImage, Imgproc.COLOR_BGR2GRAY);
    String file2 = imageID + "_filledGrayImage.png";
    //        Highgui.imwrite(file2, filledGrayImage);
    imageNames.add(ImageUtils.saveImage(filledGrayImage, file2, request));

    Mat gaussianGrayImage = new Mat();
    Imgproc.GaussianBlur(filledGrayImage, gaussianGrayImage, new Size(0, 0), 3);
    Core.addWeighted(filledGrayImage, 3.5, gaussianGrayImage, -1, 0, gaussianGrayImage);
    String file3 = imageID + "_sharpenedImage.png";
    //        Highgui.imwrite(file3, gaussianGrayImage);
    imageNames.add(ImageUtils.saveImage(gaussianGrayImage, file3, request));

    //        Mat filledBinarizedImage2 = new Mat();
    //        Imgproc.adaptiveThreshold(filledGrayImage, filledBinarizedImage2, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 75, 10);
    //        String file5 = imageID + "_filledBinarizedImage2.png";  
    ////        Highgui.imwrite(file11, filledBinarizedImage2);
    //        imageNames.add(ImageUtils.saveImage(filledBinarizedImage2, file5));
    //        
    //        Mat filledBinarizedImage1 = new Mat();
    //        Imgproc.adaptiveThreshold(filledGrayImage, filledBinarizedImage1, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 4);
    //        String file4 = imageID + "_filledBinarizedImage1.png";  
    ////        Highgui.imwrite(file4, filledBinarizedImage1);
    //        imageNames.add(ImageUtils.saveImage(filledBinarizedImage1, file4));

    return imageNames;
}

From source file:classes.TextRecognitionPreparer.java

public static ArrayList<BufferedImage> generateRecognizableBufferedImages(Mat img, Scalar backgroundColor,
        Scalar userPickedColor) {// w w w .  j  a  v a 2s  .  c  o  m

    ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();

    Mat filledImage = img.clone();
    Scalar newVal = new Scalar(userPickedColor.val[2], userPickedColor.val[1], userPickedColor.val[0]);

    Imgproc.floodFill(filledImage, new Mat(), new Point(0, 0), newVal);
    images.add(Util.mat2Img(filledImage));

    Mat filledGrayImage = new Mat();
    Imgproc.cvtColor(filledImage, filledGrayImage, Imgproc.COLOR_BGR2GRAY);
    images.add(Util.mat2Img(filledGrayImage));

    Mat gaussianGrayImage = new Mat();
    Imgproc.GaussianBlur(filledGrayImage, gaussianGrayImage, new Size(0, 0), 3);
    Core.addWeighted(filledGrayImage, 3.5, gaussianGrayImage, -1, 0, gaussianGrayImage);
    images.add(Util.mat2Img(gaussianGrayImage));

    Mat filledBinarizedImage2 = new Mat();
    Imgproc.adaptiveThreshold(filledGrayImage, filledBinarizedImage2, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
            Imgproc.THRESH_BINARY, 75, 10);
    images.add(Util.mat2Img(filledBinarizedImage2));

    Mat filledBinarizedImage1 = new Mat();
    Imgproc.adaptiveThreshold(filledGrayImage, filledBinarizedImage1, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
            Imgproc.THRESH_BINARY, 15, 4);
    images.add(Util.mat2Img(filledBinarizedImage1));

    return images;
}

From source file:classes.TextRecognitionPreparer.java

public static ArrayList<Mat> generateRecognizableImages(Mat img, Scalar backgroundColor,
        Scalar userPickedColor) {//from   w  w w.ja v a  2  s.  co  m

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

    Mat filledImage = img.clone();
    Scalar newVal = new Scalar(userPickedColor.val[2], userPickedColor.val[1], userPickedColor.val[0]);
    Imgproc.floodFill(filledImage, new Mat(), new Point(0, 0), newVal);
    String file1 = "filledImage.png";
    //        Highgui.imwrite(file1, filledImage);
    images.add(filledImage);

    Mat filledGrayImage = new Mat();
    Imgproc.cvtColor(filledImage, filledGrayImage, Imgproc.COLOR_BGR2GRAY);
    String file2 = "filledGrayImage.png";
    //        Highgui.imwrite(file2, filledGrayImage);
    images.add(filledGrayImage);

    Mat gaussianGrayImage = new Mat();
    Imgproc.GaussianBlur(filledGrayImage, gaussianGrayImage, new Size(0, 0), 3);
    Core.addWeighted(filledGrayImage, 3.5, gaussianGrayImage, -1, 0, gaussianGrayImage);
    //        Core.addWeighted(filledGrayImage, 2.5, gaussianGrayImage, -0.5, 0, gaussianGrayImage);
    String file3 = "sharpenedImage.png";
    //        Highgui.imwrite(file3, gaussianGrayImage);
    images.add(gaussianGrayImage);

    Mat filledBinarizedImage = new Mat();
    Imgproc.adaptiveThreshold(filledGrayImage, filledBinarizedImage, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C,
            Imgproc.THRESH_BINARY, 15, 4);
    String file4 = "filledBinarizedImage.png";
    //        Highgui.imwrite(file4, filledBinarizedImage);
    images.add(filledBinarizedImage);

    //        BackgroundSubtractorMOG2 backgroundSubtractorMOG2 = new BackgroundSubtractorMOG2();
    //        Mat foregroundMask = new Mat();
    //        backgroundSubtractorMOG2.apply(img, foregroundMask);
    //        Highgui.imwrite("mFGMask.png", foregroundMask);

    Scalar fillingColor = cluster(userPickedColor, img, 3);

    Mat replacedColor = replaceColor(img, backgroundColor, fillingColor);
    String file5 = "replacedColor.png";
    //        Highgui.imwrite(file5, replacedColor);
    images.add(replacedColor);

    Mat grayImage = new Mat();
    Imgproc.cvtColor(replacedColor, grayImage, Imgproc.COLOR_BGR2GRAY);
    String file6 = "grayImage.png";
    //        Highgui.imwrite(file6, grayImage);
    images.add(grayImage);

    Mat binarized = new Mat();
    Imgproc.adaptiveThreshold(grayImage, binarized, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,
            15, 4);
    String file7 = "binarized.png";
    //        Highgui.imwrite(file7, binarized);
    images.add(binarized);

    Mat colorReplacedEqualized = equalizeIntensity(replacedColor);
    String file8 = "colorReplacedEqualized.png";
    //        Highgui.imwrite(file8, colorReplacedEqualized);
    images.add(colorReplacedEqualized);

    Mat colorReducedImage = reduceColor(replacedColor, 64);
    String file9 = "replacedColorColorReduced.png";
    //        Highgui.imwrite(file9, colorReducedImage);
    images.add(colorReducedImage);

    // Equalizing image
    Mat colorReducedEqualized = equalizeIntensity(colorReducedImage);
    String file10 = "colorReducedEqualized.png";
    //        Highgui.imwrite(file10, colorReducedEqualized);
    images.add(colorReducedEqualized);

    return images;

}