List of usage examples for org.opencv.core Scalar Scalar
public Scalar(double v0, double v1)
From source file:eu.fpetersen.robobrain.behavior.followobject.ColorBlobDetector.java
License:Open Source License
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 maxArea = 0;/*from www . j a v a 2s. c o m*/ Iterator<MatOfPoint> each = contours.iterator(); Mat biggestContour = null; while (each.hasNext()) { MatOfPoint wrapper = each.next(); double area = Imgproc.contourArea(wrapper); if (area > maxArea) { maxArea = area; biggestContour = wrapper.clone(); } } if (biggestContour != null) { Core.multiply(biggestContour, new Scalar(4, 4), biggestContour); Moments mo = Imgproc.moments(biggestContour); centroidOfMaxArea = new Point(mo.get_m10() / mo.get_m00(), mo.get_m01() / mo.get_m00()); } else { centroidOfMaxArea = null; } // 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); } } Imgproc.drawContours(rgbaImage, mContours, -1, CONTOUR_COLOR); }
From source file:in.fabinpaul.sixthsense.ColorBlobDetector.java
License:Apache License
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;/*from www . java 2s. c o m*/ } // 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); } } }
From source file:LetsStart.GUI.java
public GUI(String windowName, Mat newImage) { super();//from ww w. j av a 2 s .c om this.windowName = windowName; this.image = newImage; //this.a = newImage; //new Mat (100,100, CvType.CV_16UC3 ,new Scalar (new double[]{1,1,1})); originalImage = newImage.clone(); color = new Scalar(10, 10); processOperation(); updateView(); }
From source file:org.lasarobotics.vision.detection.ColorBlobDetector.java
License:Open Source License
/** * 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 *//*w w w.j a v a 2 s.c o m*/ 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)); } }
From source file:org.vinesrobotics.bot.utils.opencv.ColorBlobDetector.java
License:Open Source License
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); // Filter contours by area and resize to fit the original image size 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;//from ww w. ja v a 2 s . c o m } 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); } } Imgproc.drawContours(rgbaImage, mContours, -1, mBaseColor); each = mContours.iterator(); ArrayList<Point> centers = new ArrayList<>(); while (each.hasNext()) { MatOfPoint wrapper = each.next(); Moments moments = Imgproc.moments(wrapper); centers.add(new Point(moments.m10 / moments.m00, moments.m01 / moments.m00)); } colorCenterPoints = centers; Point avg = new Point(0, 0); for (Point p : centers) avg.set(new double[] { avg.x + p.x, avg.y + p.y }); avg.set(new double[] { avg.x / centers.size(), avg.y / centers.size() }); centerOfAll = avg; }