Ask Your Question
0

Detect green laser dot

asked 2019-05-08 09:19:20 -0600

image description

how to detect this green laser dot using android openCV

edit retag flag offensive close merge delete

Comments

Extract the green channel. Threshold to only include high intensity areas (> 240 or so) Do a few rounds of dilation. Find contours and filter based on area and maybe aspect ratio

Chris gravatar imageChris ( 2019-05-08 14:42:43 -0600 )edit

enter code here Imgproc.cvtColor(gray, hsv, Imgproc.COLOR_RGB2HSV); Core.inRange(hsv, new Scalar(45,100, 100), new Scalar(75,255,255), lowerRedRange); Imgproc.threshold(lowerRedRange, bw, 0, 255,Imgproc.THRESH_BINARY); // dilate canny output to remove potential // holes between edge segments Imgproc.dilate(bw, bw, new Mat(), new Point(-1, 1), 1);

// find contours and store them all as a list
List<MatOfPoint> contours = new ArrayList<>();
contourImage = bw.clone();
Imgproc.findContours(
        contourImage,
        contours,
        hierarchyOutputVector,
        Imgproc.RETR_EXTERNAL,
        Imgproc.CHAIN_APPROX_SIMPLE
);
Bhavesh Valani gravatar imageBhavesh Valani ( 2019-05-09 00:29:24 -0600 )edit

enter code here // loop over all found contours for (MatOfPoint cnt : contours) { MatOfPoint2f curve = new MatOfPoint2f(cnt.toArray());

    // approximates a polygonal curve with the specified precision
    Imgproc.approxPolyDP(
            curve,
            approxCurve,
            0.02 * Imgproc.arcLength(curve, true),
            true
    );

    int numberVertices = (int) approxCurve.total();
    double contourArea = Imgproc.contourArea(cnt);

    Log.d(TAG, "vertices:" + numberVertices);
    // ignore to small areas
    if (Math.abs(contourArea) < 100
        // || !Imgproc.isContourConvex(
    ) {
        continue;}
    if (numberVertices >= 4 && numberVertices <= 6) {
    }

    else {// detect}
Bhavesh Valani gravatar imageBhavesh Valani ( 2019-05-09 00:30:00 -0600 )edit

thats above is my code but not getting perfect dot of laser

Bhavesh Valani gravatar imageBhavesh Valani ( 2019-05-09 00:30:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-05-10 10:18:13 -0600

The image below has 4 sections for each step.

  1. green channel using extractChannel(src, dst, 1)

  2. threshold with threshold value 240

  3. dilate with rect kernel of size 3x3 for 1 iteration

  4. find contours and filter by excluding contours with area < 1000 and excluding contours with center y > 300

image description

edit flag offensive delete link more

Comments

Hi chris thank for your valuable answer, after do some research on your steps in android finally i get the same output that you shown above and i am really thanks full for that , i need one more help from you that's

1) How i validate that green laser dot in drop inside tank outer boundaries or out of tank image boundaries ? 2) Tank is .png image inside imageview

Bhavesh Valani gravatar imageBhavesh Valani ( 2019-05-15 01:31:54 -0600 )edit

Hey Bhavesh, I am not sure what the original tank image looks like. Is the background unique somehow? If so, you can take the center x,y of the remaining contour (laser dot) and look up the pixel in the image to check if it is on the tank or on the background. OpenCV Mat has an "at" method you can use to get a single pixel value.

Chris gravatar imageChris ( 2019-05-15 05:17:35 -0600 )edit

Ok , i will explain my situation fist

1) Mobile full screen with fix size of imageview contain TANK image, 2) remaining screen white except tank 3) Imageview is rectangle but tank boundary was not fully fit with rectangle image So when laser dot drops inside TANK image border then should be print LOG otherwise nothing to do

How i can achieve this using openCV

Bhavesh Valani gravatar imageBhavesh Valani ( 2019-05-15 05:33:03 -0600 )edit

Hey chrish i get other all thinks that i want accept one please explain or give me some code for your last two point (3,4).

3) dilate with rect kernel of size 3x3 for 1 iteration
4) find contours and filter by excluding contours with area < 1000 and excluding contours with center y > 300

Bhavesh Valani gravatar imageBhavesh Valani ( 2019-05-18 02:26:40 -0600 )edit

The morphology looks something like:

Size kernelSize(3, 3); Mat kernel = getStructuringElement(MORPH_RECT, kernelSize); morphologyEx(srcImg, dstImg, MORPH_DILATE, kernel, Point(-1, -1), 1);

The contour finding looks like:

vector<vector<point>> contourList; findContours(srcImg, contourList, RETR_LIST, CHAIN_APPROX_NONE);

if (filterList.size() > 0) { for (auto& contour : contourList) { Point2f ctr = ContourFindCenter(contour); double area = contourArea(contour); Rect rect = boundingRect(contour); double ar = double(rect.width) / rect.height; if (area < 1000 && ...

more info here: https://docs.opencv.org/3.4/d4/d73/tu...

Chris gravatar imageChris ( 2019-05-18 07:33:53 -0600 )edit

The morphology looks something like:

Size kernelSize(3, 3);
Mat kernel = getStructuringElement(MORPH_RECT, kernelSize);
morphologyEx(srcImg, dstImg, MORPH_DILATE, kernel, Point(-1, -1), 1);

The contour finding looks like:

vector<vector<Point>> contourList;
findContours(srcImg, contourList, RETR_LIST, CHAIN_APPROX_NONE);

if (filterList.size() > 0) {
    for (auto& contour : contourList) {
        Point2f ctr = ContourFindCenter(contour);
        double area = contourArea(contour);
        Rect rect = boundingRect(contour);
        double ar = double(rect.width) / rect.height;
        if (area < 1000 && .

..

more info here: https://docs.opencv.org/3.4/d4/d73/tu...

Chris gravatar imageChris ( 2019-05-18 07:34:08 -0600 )edit

Please past full code so i will convert it in android,

Bhavesh Valani gravatar imageBhavesh Valani ( 2019-05-18 07:38:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-05-08 09:19:20 -0600

Seen: 1,788 times

Last updated: May 10 '19