Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
  2. Mat lowerRed = new Mat();
  3. Mat upperRed = new Mat();
  4. Mat redHueImage = new Mat();
  5. Mat green = new Mat();
  6. Mat greenHueImage = new Mat();
  7. Mat blue = new Mat();
  8. Mat blueHueImage = new Mat();
  9. Mat orange;
  10.  
  11. Mat rgba = inputFrame.rgba();
  12. //Core.flip(rgba, rgba, 1);
  13. Mat HSVMat = new Mat();
  14. Imgproc.medianBlur(rgba,rgba,3);
  15. Imgproc.cvtColor(rgba, HSVMat, Imgproc.COLOR_RGB2HSV, 0);
  16. //detect red cirlces
  17. Core.inRange(HSVMat,new Scalar(0,70,100),new Scalar(10,255,255),lowerRed);
  18. Core.inRange(HSVMat,new Scalar(160,70,50),new Scalar(180,255,255),upperRed);
  19. Core.addWeighted(lowerRed,1.0,upperRed,1.0,0.0,redHueImage);
  20. //add green circles to red
  21. Core.inRange(HSVMat,new Scalar(50,100,100),new Scalar(70,255,255),green);
  22. Core.addWeighted(redHueImage,1.0,green,1.0,0.0,greenHueImage);
  23. //add blue circles to red and green
  24. Core.inRange(HSVMat,new Scalar(240,100,100),new Scalar(260,255,255),blue);
  25. Core.addWeighted(greenHueImage,1.0,blue,1.0,0.0,blueHueImage);
  26.  
  27. Imgproc.GaussianBlur(blueHueImage, blueHueImage, new Size(9, 9), 2, 2);
  28. double dp = 1.2d;
  29. double minDist = 100;
  30. int minRadius = 30;
  31. int maxRadius = 100;
  32. double param1 = 70, param2 = 72;
  33. Mat circles = new Mat();
  34.  
  35.  
  36. Imgproc.HoughCircles(blueHueImage, circles, Imgproc.HOUGH_GRADIENT, dp, redHueImage.rows()/8, param1, param2, minRadius, maxRadius);
  37. int numCircles = (circles.rows() == 0) ? 0 : circles.cols();
  38. for (int i = 0; i < numCircles; i++) {
  39. double[] circleCoordinates = circles.get(0, i);
  40. int x = (int) circleCoordinates[0], y = (int) circleCoordinates[1];
  41. Point center = new Point(x, y);
  42. int radius = (int) circleCoordinates[2];
  43. Imgproc.circle(rgba, center, radius, new Scalar(0, 255, 0), 4);
  44. }
  45. circles.release();
  46. lowerRed.release();
  47. upperRed.release();
  48. HSVMat.release();
  49. redHueImage.release();
  50.  
  51. return rgba;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement