Vishal Thakkar
Vishal Thakkar

Reputation: 682

Detect laser light dot of any colour using OpenCV in Android

I am trying to detect laser light dot of any colour of laser.and i have done some reference code from here OpenCV Android Track laser dot

That code is running perfectly for Only RED colour detection and i want any colour of laser dot detection.

I am new in OpenCV.

Here's what i have done till now :

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);



        }

Upvotes: 0

Views: 2156

Answers (2)

Angelo Gabriel Abrita
Angelo Gabriel Abrita

Reputation: 379

This is old question but i have findy my solution using core.InRange

Follow my alternative version

@Override
public void onCameraViewStarted(int width, int height) {

    mat1 = new Mat(height, width, CvType.CV_16UC4);
    mat2 = new Mat(height, width, CvType.CV_16UC4);

}

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    Mat src = inputFrame.rgba();

     Imgproc.cvtColor(inputFrame.rgba(), mat1, Imgproc.COLOR_BGR2HSV);
     //rangeLow and RangeHight is Scalar
     Core.inRange(mat1, rangeLow, rangeHight, mat2);
     Core.MinMaxLocResult mmG = Core.minMaxLoc(mat2);
     Core.rotate(src, src, Core.ROTATE_90_CLOCKWISE);

     Imgproc.circle(src,mmG.maxLoc,30,new Scalar(0,255,0), 5, Imgproc.LINE_AA);

    return src;
}

enter image description here

Upvotes: 3

The code you posted carries out two thresholding operations. One on the hue and one on the value. It then ANDs the results together. Because of the way it thresholds the hue, the effect is that it is looking for a bright red(ish) spot.

My first solution would be to look for just a bright spot (so just look on the hue frame). You might also try looking for high saturations (except that a laser spot may well overload the sensors, and result in an apparently unsaturated pixel).

To select the appropriate threshold values, you will have to experiment with various images.

Upvotes: 0

Related Questions