OpenCV圈出颜色检测

时间:2016-06-27 08:47:51

标签: android opencv

我正在尝试检测相机框架上的红色圆圈,我试图像这里https://solarianprogrammer.com/2015/05/08/detect-red-circles-image-using-opencv/Android OpenCV Color detection那样做,但它只检测蓝色圆圈O_o(截图链接:{{3 }),这里是我的代码:

 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
        Mat lowerRed = new Mat();
        Mat upperRed = new Mat();
        Mat redHueImage = new Mat();
        Mat green;
        Mat blue;
        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_BGR2HSV, 0);
        Core.inRange(HSVMat,new Scalar(0,100,100),new Scalar(10,255,255),lowerRed);
        Core.inRange(HSVMat,new Scalar(160,100,100),new Scalar(179,255,255),upperRed);
        Core.addWeighted(lowerRed,1.0,upperRed,1.0,0.0,redHueImage);
        Imgproc.GaussianBlur(redHueImage, redHueImage, new Size(9, 9), 2, 2);
        double dp = 1.2d;
        double minDist = 100;
        int minRadius = 0;
        int maxRadius = 0;
        double param1 = 100, param2 = 20;
        Mat circles = new Mat();


        Imgproc.HoughCircles(redHueImage, 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);
        }
        lowerRed.release();
        upperRed.release();
        HSVMat.release();

        return rgba;
    }

0 个答案:

没有答案
相关问题