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

The following examples show how to use org.opencv.core.Core#inRange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HSVColorFilter.java    From DogeCV with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Process a image and return a mask
 * @param input - Input image to process
 * @param mask - Output mask
 */
@Override
public void process(Mat input, Mat mask) {
    // Copy the input to working mat
    workingMat = input.clone();
    // Convert the input to HSV color space
    Imgproc.cvtColor(workingMat,workingMat,Imgproc.COLOR_RGB2HSV_FULL);

    // Blur the imgae
    Imgproc.GaussianBlur(workingMat,workingMat,new Size(5,5),0);

    // Run a inRange mask using the color and range
    Scalar lower = new Scalar(perfect.val[0] - (range.val[0]/2), perfect.val[1] - (range.val[1]/2),perfect.val[2] - (range.val[2]/2));
    Scalar upper = new Scalar(perfect.val[0] + (range.val[0]/2), perfect.val[1] + (range.val[1]/2),perfect.val[2] + (range.val[2]/2));
    Core.inRange(workingMat,lower,upper,mask);
}
 
Example 2
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 6 votes vote down vote up
public void skinSegmentation_WithThreshold()
{ 
    //-----code for skin detection--using hsv color method
    
    orgMask= new Mat(matrix2_grabcut.size(),CvType.CV_8UC3);
    orgMask.setTo(new Scalar(0,0,0));
    
    Mat hsvImage = new Mat();
    Imgproc.cvtColor(matrix2_grabcut, hsvImage, Imgproc.COLOR_BGR2HSV);
    Core.inRange(hsvImage, new Scalar(3,30,50),new Scalar(33,255,255),orgMask);
    
    Imgcodecs.imwrite(resultDirectory + maskOutput, orgMask);
    
    newMask = Imgcodecs.imread(resultDirectory + maskOutput);
    //code for getting rgb skin mask from hsv skin mask
    mask_rgb = new Mat(newMask.size(),CvType.CV_8SC3);
    Imgproc.cvtColor(newMask, mask_rgb, Imgproc.COLOR_HSV2RGB);    
    Imgcodecs.imwrite(resultDirectory+maskRgbOutput, mask_rgb);
    
    //getting only skin image with red background
    matrix3_skindetection= new Mat(sourceImage.size(), sourceImage.type());
    matrix3_skindetection.setTo(new Scalar(0,0,255));
    sourceImage.copyTo(matrix3_skindetection,orgMask);
    
    Imgcodecs.imwrite(resultDirectory+skinDetectionOutput,matrix3_skindetection);
}
 
Example 3
Source File: OpenCVoperation.java    From Human-hair-detection with Apache License 2.0 6 votes vote down vote up
public void skinSegmentation()
{
    matrix3_skindetection = new Mat(matrix2_grabcut.size(),matrix2_grabcut.type()); 
    matrix3_skindetection.setTo(new Scalar(0,0,255));
    Mat skinMask = new Mat();
    Mat hsvMatrix = new Mat();
    
    Scalar lower = new Scalar(0,48,80);
    Scalar upper = new Scalar(20,255,255);
    
    Imgproc.cvtColor(matrix2_grabcut, hsvMatrix, Imgproc.COLOR_BGR2HSV);
    Core.inRange(hsvMatrix, lower, upper, skinMask);
    
    Mat kernel =Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE,new Size(11,11));
    Imgproc.erode(skinMask, skinMask, kernel);
    Imgproc.dilate(skinMask, skinMask, kernel);  
    
    Imgproc.GaussianBlur(skinMask,skinMask, new Size(3,3), 0);
    
    Core.bitwise_and(matrix2_grabcut, matrix2_grabcut, matrix3_skindetection,skinMask);
    Imgcodecs.imwrite(resultDirectory+skinDetectionOutput , matrix3_skindetection);
}
 
Example 4
Source File: CbColorFilter.java    From DogeCV with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void process(Mat input, Mat mask) {
    Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YCrCb);

    // Blur it
    Imgproc.GaussianBlur(input,input,new Size(5,5),0);
    // Run in range check
    Core.inRange(input, lower, upper, mask);
    input.release();
}
 
Example 5
Source File: HSVRangeFilter.java    From DogeCV with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Process a image and return a mask
 * @param input - Input image to process
 * @param mask - Output mask
 */
@Override
public void process(Mat input, Mat mask) {
    // Convert the input to HSV
    Imgproc.cvtColor(input,input,Imgproc.COLOR_RGB2HSV_FULL);

    // Blur it
    Imgproc.GaussianBlur(input,input,new Size(5,5),0);
    // Run in range check
    Core.inRange(input,lower,upper,mask);
    input.release();
}
 
Example 6
Source File: GrayscaleFilter.java    From DogeCV with GNU General Public License v3.0 5 votes vote down vote up
public void process(Mat input, Mat mask) {
    // Convert the input to grayscale
    Imgproc.cvtColor(input,input,Imgproc.COLOR_RGB2GRAY);

    // Blur it
    Imgproc.GaussianBlur(input,input,new Size(5,5),0);
    // Run in range check
    Core.inRange(input, lower, upper, mask);
    input.release();
}
 
Example 7
Source File: ColorBlobDetector.java    From FaceT with Mozilla Public License 2.0 5 votes vote down vote up
public void process(Mat rgbaImage) {
    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
    Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

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

    Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

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

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

    // Find max contour area
    double maxArea = 0;
    Iterator<MatOfPoint> each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint wrapper = each.next();
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea)
            maxArea = area;
    }

    // Filter contours by area and resize to fit the original image size
    mContours.clear();
    each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint contour = each.next();
        if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
            Core.multiply(contour, new Scalar(4,4), contour);
            mContours.add(contour);
        }
    }
}
 
Example 8
Source File: ColorBlobDetector.java    From OpenCV-AndroidSamples with MIT License 5 votes vote down vote up
public void process(Mat rgbaImage) {
    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
    Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

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

    Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

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

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

    // Find max contour area
    double maxArea = 0;
    Iterator<MatOfPoint> each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint wrapper = each.next();
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea)
            maxArea = area;
    }

    // Filter contours by area and resize to fit the original image size
    mContours.clear();
    each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint contour = each.next();
        if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
            Core.multiply(contour, new Scalar(4,4), contour);
            mContours.add(contour);
        }
    }
}
 
Example 9
Source File: ColorBlobDetector.java    From hand_finger_recognition_android with MIT License 5 votes vote down vote up
public void process(Mat rgbaImage) {
    Imgproc.pyrDown(rgbaImage, mPyrDownMat);
	Imgproc.pyrDown(mPyrDownMat, mPyrDownMat);

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

    Core.inRange(mHsvMat, mLowerBound, mUpperBound, mMask);
    Imgproc.dilate(mMask, mDilatedMask, new Mat());

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

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

    // Find max contour area
    double maxArea = 0;
    Iterator<MatOfPoint> each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint wrapper = each.next();
        double area = Imgproc.contourArea(wrapper);
        if (area > maxArea)
            maxArea = area;
    }

    // Filter contours by area and resize to fit the original image size
    mContours.clear();
    each = contours.iterator();
    while (each.hasNext()) {
        MatOfPoint contour = each.next();
        if (Imgproc.contourArea(contour) > mMinContourArea*maxArea) {
            Core.multiply(contour, new Scalar(4,4), contour);
            mContours.add(contour);
        }
    }
}
 
Example 10
Source File: LeviColorFilter.java    From DogeCV with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Process a image and return a mask
 * @param input - Input image to process
 * @param mask - Output mask
 */
@Override
public void process(Mat input, Mat mask) {
    channels = new ArrayList<>();

    switch(color){
        case RED:
            if(threshold == -1){
                threshold = 164;
            }

            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
            break;
        case BLUE:
            if(threshold == -1){
                threshold = 145;
            }

            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YUV);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY);
            break;
        case WHITE:
            if(threshold == -1) {
                threshold = 150;
            }

            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2Lab);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            Core.inRange(channels.get(0), new Scalar(threshold, 150, 40), new Scalar(255, 150, 150), mask);
            break;
        case YELLOW:
            if(threshold == -1){
                threshold = 70;
            }
            
            Mat lab = new Mat(input.size(), 0);
            Imgproc.cvtColor(input, lab, Imgproc.COLOR_RGB2Lab);
            Mat temp = new Mat();
            Core.inRange(input, new Scalar(0,0,0), new Scalar(255,255,164), temp);
            Mat mask2 = new Mat(input.size(), 0);
            temp.copyTo(mask2);
            input.copyTo(input, mask2);
            mask2.release();
            temp.release();
            lab.release();
            
            Imgproc.cvtColor(input, input, Imgproc.COLOR_RGB2YUV);
            Imgproc.GaussianBlur(input,input,new Size(3,3),0);
            Core.split(input, channels);
            if(channels.size() > 0){
                Imgproc.threshold(channels.get(1), mask, threshold, 255, Imgproc.THRESH_BINARY_INV);
            }

            break;
    }

    for(int i=0;i<channels.size();i++){
        channels.get(i).release();
    }

    input.release();

}
 
Example 11
Source File: ColorBlobDetector.java    From FTCVision with MIT License 4 votes vote down vote up
/**
 * 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 12
Source File: ResistorImageProcessor.java    From ResistorScanner with MIT License 4 votes vote down vote up
private void findLocations(Mat searchMat)
{
    _locationValues.clear();
    SparseIntArray areas = new SparseIntArray(4);

    for(int i = 0; i < NUM_CODES; i++)
    {
        Mat mask = new Mat();
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        if(i == 2)
        {
            // combine the two ranges for red
            Core.inRange(searchMat, LOWER_RED1, UPPER_RED1, mask);
            Mat rmask2 = new Mat();
            Core.inRange(searchMat, LOWER_RED2, UPPER_RED2, rmask2);
            Core.bitwise_or(mask, rmask2, mask);
        }
        else
            Core.inRange(searchMat, COLOR_BOUNDS[i][0], COLOR_BOUNDS[i][1], mask);

        Imgproc.findContours(mask, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
        for (int contIdx = 0; contIdx < contours.size(); contIdx++)
        {
            int area;
            if ((area = (int)Imgproc.contourArea(contours.get(contIdx))) > 20)
            {
                Moments M = Imgproc.moments(contours.get(contIdx));
                int cx = (int) (M.get_m10() / M.get_m00());

                // if a colour band is split into multiple contours
                // we take the largest and consider only its centroid
                boolean shouldStoreLocation = true;
                for(int locIdx = 0; locIdx < _locationValues.size(); locIdx++)
                {
                    if(Math.abs(_locationValues.keyAt(locIdx) - cx) < 10)
                    {
                        if (areas.get(_locationValues.keyAt(locIdx)) > area)
                        {
                            shouldStoreLocation = false;
                            break;
                        }
                        else
                        {
                            _locationValues.delete(_locationValues.keyAt(locIdx));
                            areas.delete(_locationValues.keyAt(locIdx));
                        }
                    }
                }

                if(shouldStoreLocation)
                {
                    areas.put(cx, area);
                    _locationValues.put(cx, i);
                }
            }
        }
    }
}
 
Example 13
Source File: AAVActivity.java    From AAV with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
	synchronized (inputFrame) {

		_rgbaImage = inputFrame.rgba();

		if (android.os.Build.MODEL.equalsIgnoreCase("Nexus 5X")) {
			Core.flip(_rgbaImage, _rgbaImage, -1);
		}

		double current_contour;

		// In contrast to the C++ interface, Android API captures images in the RGBA format.
		// Also, in HSV space, only the hue determines which color it is. Saturation determines
		// how 'white' the color is, and Value determines how 'dark' the color is.
		Imgproc.cvtColor(_rgbaImage, _hsvMat, Imgproc.COLOR_RGB2HSV_FULL);

		Core.inRange(_hsvMat, _lowerThreshold, _upperThreshold, _processedMat);

		// Imgproc.dilate(_processedMat, _dilatedMat, new Mat());
		Imgproc.erode(_processedMat, _dilatedMat, new Mat());
		Imgproc.findContours(_dilatedMat, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
		MatOfPoint2f points = new MatOfPoint2f();
		_contourArea = 7;
		for (int i = 0, n = contours.size(); i < n; i++) {
			current_contour = Imgproc.contourArea(contours.get(i));
			if (current_contour > _contourArea) {
				_contourArea = current_contour;
				contours.get(i).convertTo(points, CvType.CV_32FC2); // contours.get(x) is a single MatOfPoint, but to use minEnclosingCircle we need to pass a MatOfPoint2f so we need to do a
				// conversion
			}
		}
		if (!points.empty() && _contourArea > MIN_CONTOUR_AREA) {
			Imgproc.minEnclosingCircle(points, _centerPoint, null);
			// Core.circle(_rgbaImage, _centerPoint, 3, new Scalar(255, 0, 0), Core.FILLED);
			if (_showContourEnable)
				Core.circle(_rgbaImage, _centerPoint, (int) Math.round(Math.sqrt(_contourArea / Math.PI)), new Scalar(255, 0, 0), 3, 8, 0);// Core.FILLED);
		}
		contours.clear();
	}
	return _rgbaImage;
}
 
Example 14
Source File: GripPipeline.java    From FtcSamples with MIT License 3 votes vote down vote up
/**
 * 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);
}