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

The following examples show how to use org.opencv.core.Core#bitwise_or() . 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: StepByStepTestActivity.java    From CVScanner with GNU General Public License v3.0 6 votes vote down vote up
Mat buildSkeleton(Mat img){
    Mat morph = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_CROSS, new Size(3, 3));
    Mat skel = new Mat(img.size(), CvType.CV_8UC1, Scalar.all(0));
    Mat eroded = new Mat();
    Mat temp = new Mat();

    boolean done = false;

    do{
        Imgproc.morphologyEx(img, eroded, Imgproc.MORPH_ERODE, morph);
        Imgproc.morphologyEx(eroded, temp, Imgproc.MORPH_DILATE, morph);
        Core.subtract(img, temp, temp);
        Core.bitwise_or(skel, temp, skel);
        eroded.copyTo(img);

        done = Core.countNonZero(img) == 0;
    }while (!done);

    return skel;
}
 
Example 2
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 3
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);
                }
            }
        }
    }
}