0

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



        }
Community
  • 1
  • 1
Vishal Thakkar
  • 652
  • 4
  • 18
  • if you start your code, will you know the color of the laser you are looking for? For example do you want to be able to set some configuration to "now we want to detect blue laser instead of red laser", or do you want to detect ANY laser in any image? First case should be very easy, second case will be tricky and probably error-prone. Are there any other assumptions (e.g. scene/task?) – Micka May 16 '17 at 11:43
  • hey @Micka, the color of the laser can be any like red,green,blue,purple any... and i want to detect it in live camera frame, not from image. can you please help me for this? Thanks. – Vishal Thakkar May 16 '17 at 11:53
  • 1
    live video are images, too. I wanted to know whether the user can/may limit the possible laser colors during runtime, or whether you want to detect all the laser colors at the same time (which will be much harder). For example the user could change the setup to "now I want to detect a purple laser and nothing else, because I know that the laser used by the presenter is purple and there won't be any other lasers." Since we don't know your application we can't tell whether these kind of assumptions are valid or not. – Micka May 16 '17 at 12:07
  • yes @Micka laser color can be only one at a time, i want to detect only one laser light color dot at a time but color of light can not be fixed, it can be any. so what should we do? – Vishal Thakkar May 16 '17 at 12:40
  • hard problem, I would try things like thresholding high valued regions in the HSV value channel and finding blobs in the hue channel. You'll probably need some trial & error approaches and some experience in computer vision. – Micka May 16 '17 at 12:45
  • if you say, that your code works well for red lasers, you can just try to detect other colors at the same time. Just add additional code from this line: `Imgproc.threshold(frameH, frameH, 155, 160, Imgproc.THRESH_BINARY);` to detect different hue ranges to other result masks and in the end combine the results. – Micka May 16 '17 at 13:05
  • okay @Micka Thank you so much and can you please tell me value for green, blue and purple color in above line.. thanks again. – Vishal Thakkar May 16 '17 at 13:21
  • have a look at https://upload.wikimedia.org/wikipedia/commons/a/ad/HueScale.svg - but in openCV you'll have to divide the values by 2 (because openCV wants to fit the hue range in a byte). So green laser should be somwhere around 60 +/- x and blue should be around 120 +/x x in your code. – Micka May 16 '17 at 13:33
  • Hi @Micka Thank you. can you please help me for this question? http://stackoverflow.com/questions/44017224/detect-values-of-rgb-from-cameraframe-using-opencv-in-android – Vishal Thakkar May 18 '17 at 07:07

2 Answers2

3

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

0

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.

  • Hi, Thanks, can you please give me idea about how can i give that threshold value for Hue and Saturation or can you please give me example? – Vishal Thakkar May 16 '17 at 10:24