Java Code Examples for org.opencv.core.Core.inRange()

The following are Jave code examples for showing how to use inRange() of the org.opencv.core.Core class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: fingerblox   File: ImageProcessing.java   View Source Code Vote up 9 votes
public Mat skinDetection(Mat src) {
        // define the upper and lower boundaries of the HSV pixel
        // intensities to be considered 'skin'
        Scalar lower = new Scalar(0, 48, 80);
        Scalar upper = new Scalar(20, 255, 255);

        // Convert to HSV
        Mat hsvFrame = new Mat(src.rows(), src.cols(), CvType.CV_8U, new Scalar(3));
        Imgproc.cvtColor(src, hsvFrame, Imgproc.COLOR_RGB2HSV, 3);

        // Mask the image for skin colors
        Mat skinMask = new Mat(hsvFrame.rows(), hsvFrame.cols(), CvType.CV_8U, new Scalar(3));
        Core.inRange(hsvFrame, lower, upper, skinMask);
//        currentSkinMask = new Mat(hsvFrame.rows(), hsvFrame.cols(), CvType.CV_8U, new Scalar(3));
//        skinMask.copyTo(currentSkinMask);

        // apply a series of erosions and dilations to the mask
        // using an elliptical kernel
        final Size kernelSize = new Size(11, 11);
        final Point anchor = new Point(-1, -1);
        final int iterations = 2;

        Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize);
        Imgproc.erode(skinMask, skinMask, kernel, anchor, iterations);
        Imgproc.dilate(skinMask, skinMask, kernel, anchor, iterations);

        // blur the mask to help remove noise, then apply the
        // mask to the frame
        final Size ksize = new Size(3, 3);

        Mat skin = new Mat(skinMask.rows(), skinMask.cols(), CvType.CV_8U, new Scalar(3));
        Imgproc.GaussianBlur(skinMask, skinMask, ksize, 0);
        Core.bitwise_and(src, src, skin, skinMask);

        return skin;
    }
 
Example 2
Project: DogeCV   File: HSVColorFilter.java   View Source Code Vote up 6 votes
@Override
public void process(Mat input, Mat mask) {
    Imgproc.cvtColor(input,input,Imgproc.COLOR_RGB2HSV_FULL);
    Imgproc.GaussianBlur(input,input,new Size(3,3),0);

    Scalar lower = new Scalar(perfect.val[0] - range.val[0], perfect.val[1] - range.val[1],perfect.val[2] - range.val[2]);
    Scalar upper = new Scalar(perfect.val[0] + range.val[0], perfect.val[1] + range.val[1],perfect.val[2] + range.val[2]);
    Core.inRange(input,lower,upper,mask);
    input.release();
}
 
Example 3
Project: Auto.js   File: ColorFinder.java   View Source Code Vote up 6 votes
public static Point[] findAllColors(ImageWrapper image, int color, int threshold, Rect rect) {
    Mat bi = new Mat();
    Scalar lowerBound = new Scalar(Color.red(color) - threshold, Color.green(color) - threshold,
            Color.blue(color) - threshold, 255);
    Scalar upperBound = new Scalar(Color.red(color) + threshold, Color.green(color) + threshold,
            Color.blue(color) + threshold, 255);
    if (rect != null) {
        Core.inRange(new Mat(image.getMat(), rect), lowerBound, upperBound, bi);
    } else {
        Core.inRange(image.getMat(), lowerBound, upperBound, bi);
    }
    Mat nonZeroPos = new Mat();
    Core.findNonZero(bi, nonZeroPos);
    if (nonZeroPos.rows() == 0 || nonZeroPos.cols() == 0) {
        return new Point[0];
    }
    Point[] points = new MatOfPoint(nonZeroPos).toArray();
    if (rect != null) {
        for (int i = 0; i < points.length; i++) {
            points[i].x += rect.x;
            points[i].y += rect.y;
        }
    }
    return points;
}
 
Example 4
Project: RobotIGS   File: ColorBlobDetector.java   View Source Code Vote up 5 votes
/**
 * Process an rgba image. The results can be drawn on retrieved later.
 * This method does not modify the image.
 *
 * @param rgbaImage An RGBA image matrix
 */
public void process(Mat rgbaImage) {
    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
    Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

    Imgproc.cvtColor(mPyrDownMat, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);

    //Test whether we need two inRange operations (only if the hue crosses over 255)
    if (upperBound.getScalar().val[0] <= 255) {
        Core.inRange(mHsvMat, lowerBound.getScalar(), upperBound.getScalar(), mMask);
    } else {
        //We need two operations - we're going to OR the masks together
        Scalar lower = lowerBound.getScalar().clone();
        Scalar upper = upperBound.getScalar().clone();
        while (upper.val[0] > 255)
            upper.val[0] -= 255;
        double tmp = lower.val[0];
        lower.val[0] = 0;
        //Mask 1 - from 0 to n
        Core.inRange(mHsvMat, lower, upper, mMaskOne);
        //Mask 2 - from 255-n to 255
        lower.val[0] = tmp;
        upper.val[0] = 255;

        Core.inRange(mHsvMat, lower, upper, mMask);
        //OR the two masks
        Core.bitwise_or(mMaskOne, mMask, mMask);
    }

    //Dilate (blur) the mask to decrease processing power
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

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

    Imgproc.findContours(mDilatedMask, contourListTemp, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    // Filter contours by area and resize to fit the original image size
    contours.clear();
    for (MatOfPoint c : contourListTemp) {
        Core.multiply(c, new Scalar(4, 4), c);
        contours.add(new Contour(c));
    }
}
 
Example 5
Project: Robot-Vision-API   File: RGBFilter.java   View Source Code Vote up 5 votes
@Override
public Mat filter(Mat image) {
    Mat out = new Mat();
    Scalar lower = new Scalar(blue.getLow(), green.getLow(), red.getLow());
    Scalar upper = new Scalar(blue.getHigh(), green.getHigh(), red.getHigh());
    Core.inRange(image, lower, upper, out);
    return out;
}
 
Example 6
Project: Robot-Vision-API   File: HSVFilter.java   View Source Code Vote up 5 votes
@Override
public Mat filter(Mat image) {
    Mat hsvImg = new Mat();
    Imgproc.cvtColor(image, hsvImg, Imgproc.COLOR_BGR2HSV);
    Scalar lower = new Scalar(hue.getLow(), saturation.getLow(), value.getLow());
    Scalar upper = new Scalar(hue.getHigh(), saturation.getHigh(), value.getHigh());
    Core.inRange(hsvImg, lower, upper, hsvImg);
    return hsvImg;
}
 
Example 7
Project: Robot-Vision-API   File: BrightnessFilter.java   View Source Code Vote up 5 votes
@Override
public Mat filter(Mat image) {
    Mat out = new Mat();
    Imgproc.cvtColor(image, out, Imgproc.COLOR_BGR2GRAY);
    Scalar lower = new Scalar(brightness.getLow());
    Scalar upper = new Scalar(brightness.getHigh());
    Core.inRange(out, lower, upper, out);
    return out;
}
 
Example 8
Project: Team9261-2017-2018   File: NewCryptobox.java   View Source Code Vote up 5 votes
public Mat getRedMask(Mat input){
    Scalar lower1 = new Scalar(0,150,100);
    Scalar upper1 = new Scalar(20,255,255);
    Scalar lower2 = new Scalar(140,100,100);
    Scalar upper2 = new Scalar(179,255,255);
    Core.inRange(input,lower1,upper1,mask1);
    Core.inRange(input,lower2,upper2,mask2);
    Core.addWeighted(mask1,1.0, mask2,1.0, 0.0, mask);
    return mask;
}
 
Example 9
Project: FTC2016   File: ColorBlobDetector.java   View Source Code Vote up 4 votes
/**
 * Process an rgba image. The results can be drawn on retrieved later.
 * This method does not modify the image.
 *
 * @param rgbaImage An RGBA image matrix
 */
public void process(Mat rgbaImage) {
    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
    Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

    Imgproc.cvtColor(mPyrDownMat, mHsvMat, Imgproc.COLOR_RGB2HSV_FULL);

    //Test whether we need two inRange operations (only if the hue crosses over 255)
    if (upperBound.getScalar().val[0] <= 255) {
        Core.inRange(mHsvMat, lowerBound.getScalar(), upperBound.getScalar(), mMask);
    } else {
        //We need two operations - we're going to OR the masks together
        Scalar lower = lowerBound.getScalar().clone();
        Scalar upper = upperBound.getScalar().clone();
        while (upper.val[0] > 255)
            upper.val[0] -= 255;
        double tmp = lower.val[0];
        lower.val[0] = 0;
        //Mask 1 - from 0 to n
        Core.inRange(mHsvMat, lower, upper, mMaskOne);
        //Mask 2 - from 255-n to 255
        lower.val[0] = tmp;
        upper.val[0] = 255;

        Core.inRange(mHsvMat, lower, upper, mMask);
        //OR the two masks
        Core.bitwise_or(mMaskOne, mMask, mMask);
    }

    //Dilate (blur) the mask to decrease processing power
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

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

    Imgproc.findContours(mDilatedMask, contourListTemp, mHierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    // Filter contours by area and resize to fit the original image size
    contours.clear();
    for (MatOfPoint c : contourListTemp) {
        Core.multiply(c, new Scalar(4, 4), c);
        contours.add(new Contour(c));
    }
}
 
Example 10
Project: FTC2016   File: BeaconUtils.java   View Source Code Vote up 4 votes
public static int getBeaconConfig(Image img, VuforiaTrackableDefaultListener beacon, CameraCalibration camCal) {

        OpenGLMatrix pose = beacon.getRawPose();

        if (pose != null && img != null && img.getPixels() != null) {

            Matrix34F rawPose = new Matrix34F();
            float[] poseData = Arrays.copyOfRange(pose.transposed().getData(), 0, 12);

            rawPose.setData(poseData);

            float[][] corners = new float[4][2];

            corners[0] = Tool.projectPoint(camCal, rawPose, new Vec3F(-127, 276, 0)).getData(); //upper left of beacon
            corners[1] = Tool.projectPoint(camCal, rawPose, new Vec3F(127, 276, 0)).getData(); //upper right of beacon
            corners[2] = Tool.projectPoint(camCal, rawPose, new Vec3F(127, -92, 0)).getData(); //lower right of beacon
            corners[3] = Tool.projectPoint(camCal, rawPose, new Vec3F(-127, -92, 0)).getData(); //lower left of beacon

            //getting camera image...
            Bitmap bm = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.RGB_565);
            bm.copyPixelsFromBuffer(img.getPixels());

            //turning the corner pixel coordinates into a proper bounding box
            Mat crop = bitmapToMat(bm, CvType.CV_8UC3);
            float x = Math.min(Math.min(corners[1][0], corners[3][0]), Math.min(corners[0][0], corners[2][0]));
            float y = Math.min(Math.min(corners[1][1], corners[3][1]), Math.min(corners[0][1], corners[2][1]));
            float width = Math.max(Math.abs(corners[0][0] - corners[2][0]), Math.abs(corners[1][0] - corners[3][0]));
            float height = Math.max(Math.abs(corners[0][1] - corners[2][1]), Math.abs(corners[1][1] - corners[3][1]));


            //make sure our bounding box doesn't go outside of the image
            //OpenCV doesn't like that...
            x = Math.max(x, 0);
            y = Math.max(y, 0);
            width = (x + width > crop.cols())? crop.cols() - x : width;
            height = (y + height > crop.rows())? crop.rows() - y : height;

            //cropping bounding box out of camera image
            final Mat cropped = new Mat(crop, new Rect((int) x, (int) y, (int) width, (int) height));

            //filtering out non-beacon-blue colours in HSV colour space
            Imgproc.cvtColor(cropped, cropped, Imgproc.COLOR_RGB2HSV_FULL);

            //get filtered mask
            //if pixel is within acceptable blue-beacon-colour range, it's changed to white.
            //Otherwise, it's turned to black
            Mat mask = new Mat();

            Core.inRange(cropped, BEACON_BLUE_LOW, BEACON_BLUE_HIGH, mask);
            Moments mmnts = Imgproc.moments(mask, true);

            //calculating centroid of the resulting binary mask via image moments
            Log.i("CentroidX", "" + ((mmnts.get_m10() / mmnts.get_m00())));
            Log.i("CentroidY", "" + ((mmnts.get_m01() / mmnts.get_m00())));

            //checking if blue either takes up the majority of the image (which means the beacon is all blue)
            //or if there's barely any blue in the image (which means the beacon is all red or off)
//            if (mmnts.get_m00() / mask.total() > 0.8) {
//                return VortexUtils.BEACON_ALL_BLUE;
//            } else if (mmnts.get_m00() / mask.total() < 0.1) {
//                return VortexUtils.BEACON_NO_BLUE;
//            }//elseif

            //Note: for some reason, we end up with a image that is rotated 90 degrees
            //if centroid is in the bottom half of the image, the blue beacon is on the left
            //if the centroid is in the top half, the blue beacon is on the right
            if ((mmnts.get_m01() / mmnts.get_m00()) < cropped.rows() / 2) {
                return BEACON_RED_BLUE;
            } else {
                return BEACON_BLUE_RED;
            }
        }

        return NOT_VISIBLE;
    }
 
Example 11
Project: Team9261-2017-2018   File: NewCryptobox.java   View Source Code Vote up 4 votes
public Mat getBlueMask(Mat input){
    Scalar lower = new Scalar(90, 135, 25);
    Scalar upper = new Scalar(130, 250, 150);
    Core.inRange(input,lower,upper,mask);
    return mask;
}
 
Example 12
Project: 2017   File: GripPipeline.java   View Source Code Vote up 3 votes
/**
 * Segment an image based on color ranges.
 * 
 * @param input
 *            The image on which to perform the RGB threshold.
 * @param red
 *            The min and max red.
 * @param green
 *            The min and max green.
 * @param blue
 *            The min and max blue.
 * @param output
 *            The image in which to store the output.
 */
private void rgbThreshold (Mat input, double[] red, double[] green,
        double[] blue,
        Mat out)
{
    Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2RGB);
    Core.inRange(out, new Scalar(red[0], green[0], blue[0]),
            new Scalar(red[1], green[1], blue[1]), out);
}
 
Example 13
Project: 2017   File: KilroyVision.java   View Source Code Vote up 3 votes
/**
 * Segment an image based on color ranges.
 * 
 * @param input
 *            The image on which to perform the RGB threshold.
 * @param red
 *            The min and max red.
 * @param green
 *            The min and max green.
 * @param blue
 *            The min and max blue.
 * @param output
 *            The image in which to store the output.
 */
private void rgbThreshold (Mat input, double[] red, double[] green,
        double[] blue,
        Mat out)
{
    Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2RGB);
    Core.inRange(out, new Scalar(red[0], green[0], blue[0]),
            new Scalar(red[1], green[1], blue[1]), out);
}
 
Example 14
Project: Ftc2018RelicRecovery   File: GripPipeline.java   View Source Code Vote up 3 votes
/**
 * Segment an image based on hue, saturation, and luminance ranges.
 *
 * @param input The image on which to perform the HSL threshold.
 * @param hue The min and max hue
 * @param sat The min and max saturation
 * @param lum The min and max luminance
 * @param out The image in which to store the output.
 */
private void hslThreshold(Mat input, double[] hue, double[] sat, double[] lum,
	Mat out) {
	Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2HLS);
	Core.inRange(out, new Scalar(hue[0], lum[0], sat[0]),
		new Scalar(hue[1], lum[1], sat[1]), out);
}
 
Example 15
Project: FRC-6416-frc2017   File: GripPipeline.java   View Source Code Vote up 3 votes
/**
 * Segment an image based on hue, saturation, and value ranges.
 *
 * @param input The image on which to perform the HSL threshold.
 * @param hue The min and max hue
 * @param sat The min and max saturation
 * @param val The min and max value
 * @param output The image in which to store the output.
 */
private void hsvThreshold(Mat input, double[] hue, double[] sat, double[] val,
    Mat out) {
	Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2HSV);
	Core.inRange(out, new Scalar(hue[0], sat[0], val[0]),
		new Scalar(hue[1], sat[1], val[1]), out);
}
 
Example 16
Project: DemonVision   File: DefaultPipeline.java   View Source Code Vote up 3 votes
/**
 * Segment an image based on color ranges.
 * @param input The image on which to perform the RGB threshold.
 * @param red The min and max red.
 * @param green The min and max green.
 * @param blue The min and max blue.
 * @param output The image in which to store the output.
 */
private void rgbThreshold(Mat input, double[] red, double[] green, double[] blue,
	Mat out) {
	Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2RGB);
	Core.inRange(out, new Scalar(red[0], green[0], blue[0]),
		new Scalar(red[1], green[1], blue[1]), out);
}
 
Example 17
Project: FlashLib   File: CvProcessing.java   View Source Code Vote up 3 votes
/**
 * Filters mat data by colors. Data within the color boundary now represents a contour.  
 * 
 * @param mat the mat data
 * @param threshold the resulting binary image
 * @param min1 min boundary 1: min hue/red
 * @param max1 max boundary 1: max hue/red
 * @param min2 min boundary 2: min saturation/green
 * @param max2 max boundary 2: max saturation/green
 * @param min3 min boundary 3: min value/blue
 * @param max3 max boundary 3: max value/blue
 * @return the binary image
 * @see Core#inRange(Mat, Scalar, Scalar, Mat)
 */
public static Mat filterMatColors(Mat mat, Mat threshold, int min1, int max1, int min2, int max2, int min3, int max3){
	Core.inRange(mat, 
			new Scalar(min1, min2, min3), 
			new Scalar(max1, max2, max3), 
			threshold);
	return threshold;
}
 
Example 18
Project: frc-2017   File: Vision.java   View Source Code Vote up 2 votes
/**
 * Segment an image based on hue, saturation, and value ranges.
 *
 * @param input
 *            The image on which to perform the HSL threshold.
 * @param hue
 *            The min and max hue
 * @param sat
 *            The min and max saturation
 * @param val
 *            The min and max value
 * @param output
 *            The image in which to store the output.
 */
private void hsvThreshold(Mat input, double[] hue, double[] sat, double[] val, Mat out) {
	Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2HSV);
	Core.inRange(out, new Scalar(hue[0], sat[0], val[0]), new Scalar(hue[1], sat[1], val[1]), out);
}
 
Example 19
Project: STEAMworks   File: GripPipelineWithBlur.java   View Source Code Vote up 2 votes
/**
 * Segment an image based on hue, saturation, and luminance ranges.
 *
 * @param input
 *          The image on which to perform the HSL threshold.
 * @param hue
 *          The min and max hue
 * @param sat
 *          The min and max saturation
 * @param lum
 *          The min and max luminance
 * @param output
 *          The image in which to store the output.
 */
private void hslThreshold(Mat input, double[] hue, double[] sat, double[] lum, Mat out) {
  Imgproc.cvtColor(input, out, Imgproc.COLOR_BGR2HLS);
  Core.inRange(out, new Scalar(hue[0], lum[0], sat[0]), new Scalar(hue[1], lum[1], sat[1]), out);
}