在Android中使用OpenCV检测任何颜色的激光点

时间:2017-05-16 09:56:16

标签: android c++ opencv feature-detection

我正在尝试检测任何颜色激光的激光点。我从这里做了一些参考代码OpenCV Android Track laser dot

该代码完全适用于Only RED颜色检测,我想要任何颜色的激光点检测。

我是OpenCV的新手。

这是我迄今所做的事情:

Mat originalFrame= new Mat();
        Mat frame = new Mat();
        cvf.rgba().copyTo(originalFrame);
        cvf.rgba().copyTo(frame);
        Mat frameH;
        Mat frameV;
        Mat frameS;
        mRgba = cvf.rgba();
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        //   Mat frameS;
        // Convert it to HSV
        Imgproc.cvtColor(frame, frame, Imgproc.COLOR_RGB2HSV);
        // Split the frame into individual components (separate images for H, S,
       // and V)

        mChannels.clear();
        Core.split(frame, mChannels); // Split channels: 0-H, 1-S, 2-V
        frameH = mChannels.get(0);
        frameS = mChannels.get(1);
        frameV = mChannels.get(2);

        // Apply a threshold to each component
        Imgproc.threshold(frameH, frameH, 155, 160, Imgproc.THRESH_BINARY);
       // Imgproc.threshold(frameS, frameS, 0, 100, Imgproc.THRESH_BINARY);
        Imgproc.threshold(frameV, frameV, 250, 256, Imgproc.THRESH_BINARY);
        // Perform an AND operation
        Core.bitwise_and(frameH, frameV, frame);
     //
        //   Core.bitwise_and(frame,frameS,frame);


        Imgproc.findContours(frame, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
        hierarchy.release();
        for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
        {
            // Minimum size allowed for consideration
            MatOfPoint2f approxCurve = new MatOfPoint2f();
            MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(contourIdx).toArray() );
            //Processing on mMOP2f1 which is in type MatOfPoint2f
            double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
            Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

            //Convert back to MatOfPoint
            MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

             // Get bounding rect of contour
            Rect rect = Imgproc.boundingRect(points);

            Imgproc.rectangle(originalFrame, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 0, 255), 3);



        }

1 个答案:

答案 0 :(得分:0)

您发布的代码执行两个阈值操作。一个在色调上,一个在价值上。然后将结果与AND进行对比。由于它限制色调的方式,效果是它正在寻找一个明亮的红色(ish)点。

我的第一个解决方案是寻找一个亮点(所以只需看一下色调框架)。您也可以尝试寻找高饱和度(除了激光点可能使传感器过载,并导致明显不饱和的像素)。

要选择合适的阈值,您必须尝试各种图像。