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:org.lasarobotics.vision.ftc.resq.Beacon.java

License:Open Source License

/**
 * Set color tolerance for red beacon detector
 *
 * @param tolerance A color tolerance value from -1 to 1, where 0 is unmodified, 1 is maximum
 *                  tolerance (more colors detect as red), -1 is minimum (fery vew colors detect
 *                  as red)//w w w  .ja  v a2 s . com
 */
public void setColorToleranceRed(double tolerance) {
    //Reset the detector first
    redDetector = new ColorBlobDetector(Constants.COLOR_RED_LOWER, Constants.COLOR_RED_UPPER);
    double[] center = redDetector.getColorCenter().getScalar().val;
    double[] radius = redDetector.getColorRadius().val;
    radius = getColorWithTolerance(radius, tolerance);
    Scalar lower = new Scalar(center[0] - radius[0], center[1] - radius[1], center[2] - radius[2]);
    Scalar upper = new Scalar(center[0] + radius[0], center[1] + radius[1], center[2] + radius[2]);
    redDetector = new ColorBlobDetector(new ColorHSV(lower), new ColorHSV(upper));
}

From source file:org.lasarobotics.vision.ftc.resq.Beacon.java

License:Open Source License

/**
 * Set color tolerance for blue beacon detector
 *
 * @param tolerance A color tolerance value from -1 to 1, where 0 is unmodified, 1 is maximum
 *                  tolerance (more colors detect as blue), -1 is minimum (fery vew colors detect
 *                  as blue)//from   w  w  w . ja  v a  2s  . co m
 */
public void setColorToleranceBlue(double tolerance) {
    //Reset the detector first
    blueDetector = new ColorBlobDetector(Constants.COLOR_BLUE_LOWER, Constants.COLOR_BLUE_UPPER);
    double[] center = blueDetector.getColorCenter().getScalar().val;
    double[] radius = blueDetector.getColorRadius().val;
    radius = getColorWithTolerance(radius, tolerance);
    Scalar lower = new Scalar(center[0] - radius[0], center[1] - radius[1], center[2] - radius[2]);
    Scalar upper = new Scalar(center[0] + radius[0], center[1] + radius[1], center[2] + radius[2]);
    blueDetector = new ColorBlobDetector(new ColorHSV(lower), new ColorHSV(upper));
}

From source file:org.lasarobotics.vision.util.color.ColorHSV.java

License:Open Source License

/**
 * An HSV (hue, saturation, value) color
 * This is NOT an HSL color - they live in different spaces.
 *
 * @param h Hue, from 0 to 255/*from w  w w. j a v  a  2  s. c om*/
 * @param s Saturation, from 0 to 255
 * @param v Value, from 0 to 255
 */
public ColorHSV(int h, int s, int v) {
    super(new Scalar(h, s, v));
}

From source file:org.lasarobotics.vision.util.color.ColorHSV.java

License:Open Source License

/**
 * Parse a scalar value into the colorspace
 *
 * @param s Scalar value//  w w w .  j  av  a  2 s  .co m
 * @return Colorspace scalar value
 */
@Override
protected Scalar parseScalar(Scalar s) {
    if (s.val.length < 3)
        throw new IllegalArgumentException("Scalar must have 3 dimensions.");
    return new Scalar(s.val[0], s.val[1], s.val[2]);
}

From source file:org.openpnp.machine.reference.vision.OpenCvVisionProvider.java

License:Open Source License

private void locateTemplateMatchesDebug(Mat roiImage, Mat templateImage, org.opencv.core.Point matchLoc) {
    if (logger.isDebugEnabled()) {
        try {/*w  ww.  ja  v a  2  s.  co  m*/
            Core.rectangle(roiImage, matchLoc, new org.opencv.core.Point(matchLoc.x + templateImage.cols(),
                    matchLoc.y + templateImage.rows()), new Scalar(0, 255, 0));

            BufferedImage debugImage = OpenCvUtils.toBufferedImage(roiImage);
            File file = Configuration.get().createResourceFile(OpenCvVisionProvider.class, "debug_", ".png");
            ImageIO.write(debugImage, "PNG", file);
            logger.debug("Debug image filename {}", file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.pattern.detection.contour.ContourDetectionAlgorithm.java

@Override
public List<? extends Particle> detectAndAssign(ParticleImage image) {

    // take the copy of image that we dont modify the original
    Mat img = new Mat();
    image.getPixels().copyTo(img);/*from  w w  w. ja v  a 2 s.  co m*/
    // blur the image to denoise
    //Imgproc.blur(imagePixels, imagePixels, new Size(3, 3));

    // thresholds the image
    Mat thresholded = new Mat();
    //        Imgproc.threshold(imagePixels, thresholded,
    //                THRESHOLD, MAX, Imgproc.THRESH_TOZERO_INV);

    // thresholding
    Imgproc.adaptiveThreshold(img, thresholded, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
            Imgproc.THRESH_BINARY_INV, 155, 15);
    Highgui.imwrite("1_thresholded.jpg", thresholded);

    Mat edges = new Mat();
    Imgproc.Canny(img, edges, 100, 200);
    Highgui.imwrite("1_canny.jpg", edges);

    // remove small noises
    //        Mat kernel = Mat.ones(new Size(3, 3), CvType.CV_8UC1);
    Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_CROSS, new Size(5, 5));

    Imgproc.morphologyEx(thresholded, thresholded, Imgproc.MORPH_OPEN, kernel);
    Highgui.imwrite("2_opening.jpg", thresholded);

    //        Imgproc.erode(thresholded, thresholded, kernel, ORIGIN, 3);
    //        Highgui.imwrite("3_erode.jpg", thresholded);

    Mat distTransform = new Mat();
    Imgproc.distanceTransform(thresholded, distTransform, Imgproc.CV_DIST_C, 5);
    distTransform.convertTo(distTransform, CvType.CV_8UC1);
    Imgproc.equalizeHist(distTransform, distTransform);
    Highgui.imwrite("4_distance_transform.jpg", distTransform);

    Mat markerMask = Mat.zeros(img.size(), CvType.CV_8UC1);
    double max = Core.minMaxLoc(distTransform).maxVal;
    Imgproc.threshold(distTransform, markerMask, max * 0.9, 255, Imgproc.THRESH_BINARY);
    markerMask.convertTo(markerMask, CvType.CV_8UC1);
    Highgui.imwrite("5_thresholded_distance.jpg", markerMask);

    List<MatOfPoint> contours = new ArrayList<>();
    Imgproc.findContours(markerMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE,
            ORIGIN);

    Mat markers = Mat.zeros(img.size(), CvType.CV_32S);
    //markers.setTo(Scalar.all(0));
    Random rand = new Random();
    for (int idx = 0; idx < contours.size(); idx++) {
        Scalar color = new Scalar(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
        Imgproc.drawContours(markers, contours, idx, color, -1);
    }
    Highgui.imwrite("6_markers.jpg", markers);

    Imgproc.cvtColor(img, img, Imgproc.COLOR_GRAY2RGB);
    img.convertTo(img, CvType.CV_8UC3);
    Imgproc.watershed(img, markers);
    Highgui.imwrite("7_wattershed.jpg", markers);

    // detect contours
    //        List<MatOfPoint> contours = new ArrayList<>();
    Imgproc.findContours(thresholded, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE,
            ORIGIN);

    // create particle from each contour
    List<Particle> particles = new ArrayList<>();
    int i = 0;
    for (MatOfPoint contour : contours) {
        Point cog = calcCog(contour);
        if (!Double.isNaN(cog.x) && !Double.isNaN(cog.y)) {
            System.out.println(cog);
            Particle p = new Particle(cog, contour);
            particles.add(p); // just for reorting reasons
            image.assign(p);
        }
    }

    return particles;
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.CameraCalibration.java

License:Open Source License

/**
 * Draws checkerboard corners on an image.
 * // w ww  .ja v  a  2s.c  o  m
 * @param image the image to process
 * @param addToCalibration if true, add this image to the corner list
 */
public void process(Mat image, boolean addToCalibration) {
    boolean patternFound = Calib3d.findChessboardCorners(image, boardSize, boardCorners,
            Calib3d.CALIB_CB_ADAPTIVE_THRESH | Calib3d.CALIB_CB_NORMALIZE_IMAGE | Calib3d.CALIB_CB_FAST_CHECK);

    if (patternFound) {
        // Refine corner positions to be more accurate
        Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY);
        Imgproc.cornerSubPix(grayImage, boardCorners, new Size(6, 6), new Size(-1, -1),
                new TermCriteria(TermCriteria.EPS + TermCriteria.COUNT, 30, 0.1));

        if (addToCalibration) {
            calibrationCorners.add(boardCorners);
        }

    }

    image.copyTo(boardImage);
    Calib3d.drawChessboardCorners(boardImage, boardSize, boardCorners, patternFound);

    if (!addToCalibration) {
        debugImage("Board", boardImage);
    }

    Imgproc.undistort(image, undistortImage, cameraMatrix, distCoeffs);

    undistortImage.copyTo(image);

    Imgproc.putText(image, "Error: " + error, new Point(20, 20), Core.FONT_HERSHEY_PLAIN, 1.5,
            new Scalar(0, 255, 0));
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.VisionParameters.java

License:Open Source License

public static Scalar getGoalMinThreshold() {
    return new Scalar(VISION_PARAMETERS.getNumber(GOAL_H_MIN_KEY, DEFAULT_GOAL_H_THRESHOLD.getMin()),
            VISION_PARAMETERS.getNumber(GOAL_S_MIN_KEY, DEFAULT_GOAL_S_THRESHOLD.getMin()),
            (int) VISION_PARAMETERS.getNumber(GOAL_V_MIN_KEY, DEFAULT_GOAL_V_THRESHOLD.getMin()));
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.VisionParameters.java

License:Open Source License

public static Scalar getGoalMaxThreshold() {
    return new Scalar(VISION_PARAMETERS.getNumber(GOAL_H_MAX_KEY, DEFAULT_GOAL_H_THRESHOLD.getMax()),
            VISION_PARAMETERS.getNumber(GOAL_S_MAX_KEY, DEFAULT_GOAL_S_THRESHOLD.getMax()),
            (int) VISION_PARAMETERS.getNumber(GOAL_V_MAX_KEY, DEFAULT_GOAL_V_THRESHOLD.getMax()));
}

From source file:org.usfirst.frc.team2084.CMonster2016.vision.VisionParameters.java

License:Open Source License

public static Scalar getBoulderMinThreshold() {
    return new Scalar(VISION_PARAMETERS.getNumber(BOULDER_H_MIN_KEY, DEFAULT_BOULDER_H_THRESHOLD.getMin()),
            VISION_PARAMETERS.getNumber(BOULDER_S_MIN_KEY, DEFAULT_BOULDER_S_THRESHOLD.getMin()),
            (int) VISION_PARAMETERS.getNumber(BOULDER_V_MIN_KEY, DEFAULT_BOULDER_V_THRESHOLD.getMin()));
}