Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
- Mat lowerRed = new Mat();
- Mat upperRed = new Mat();
- Mat redHueImage = new Mat();
- Mat green = new Mat();
- Mat greenHueImage = new Mat();
- Mat blue = new Mat();
- Mat blueHueImage = new Mat();
- Mat orange;
- Mat rgba = inputFrame.rgba();
- //Core.flip(rgba, rgba, 1);
- Mat HSVMat = new Mat();
- Imgproc.medianBlur(rgba,rgba,3);
- Imgproc.cvtColor(rgba, HSVMat, Imgproc.COLOR_RGB2HSV, 0);
- //detect red cirlces
- Core.inRange(HSVMat,new Scalar(0,70,100),new Scalar(10,255,255),lowerRed);
- Core.inRange(HSVMat,new Scalar(160,70,50),new Scalar(180,255,255),upperRed);
- Core.addWeighted(lowerRed,1.0,upperRed,1.0,0.0,redHueImage);
- //add green circles to red
- Core.inRange(HSVMat,new Scalar(50,100,100),new Scalar(70,255,255),green);
- Core.addWeighted(redHueImage,1.0,green,1.0,0.0,greenHueImage);
- //add blue circles to red and green
- Core.inRange(HSVMat,new Scalar(240,100,100),new Scalar(260,255,255),blue);
- Core.addWeighted(greenHueImage,1.0,blue,1.0,0.0,blueHueImage);
- Imgproc.GaussianBlur(blueHueImage, blueHueImage, new Size(9, 9), 2, 2);
- double dp = 1.2d;
- double minDist = 100;
- int minRadius = 30;
- int maxRadius = 100;
- double param1 = 70, param2 = 72;
- Mat circles = new Mat();
- Imgproc.HoughCircles(blueHueImage, circles, Imgproc.HOUGH_GRADIENT, dp, redHueImage.rows()/8, param1, param2, minRadius, maxRadius);
- int numCircles = (circles.rows() == 0) ? 0 : circles.cols();
- for (int i = 0; i < numCircles; i++) {
- double[] circleCoordinates = circles.get(0, i);
- int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];
- Point center = new Point(x, y);
- int radius = (int) circleCoordinates[2];
- Imgproc.circle(rgba, center, radius, new Scalar(0, 255, 0), 4);
- }
- circles.release();
- lowerRed.release();
- upperRed.release();
- HSVMat.release();
- redHueImage.release();
- return rgba;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement