如何使用OpenCV查找亮绿色的激光点?

时间:2019-05-08 15:22:59

标签: android image opencv

在android openCV中有用于检测绿色明亮激光点的代码,但是它可以检测所有绿色,我只想检测明亮激光,无论我做过什么,我都会张贴它。

如果有任何链接请让我知道

<code>enter image description here</code>

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

    // 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 {// circle detection}

1 个答案:

答案 0 :(得分:1)

您的颜色范围不够严格。在下图中,您可以看到我使用的值。圆点周围的圆圈实际上最容易分离。点较难,因为它包含非常白色的颜色,即饱和度低的颜色。但是背景也一样,请改用戒指。
如果要特别点,可以使用环的内部轮廓。

注意:inRange返回二进制掩码,因此代码中的这一行不执行任何操作:
Imgproc.threshold(lowerRedRange, bw, 0, 255,Imgproc.THRESH_BINARY);

enter image description here

enter image description here

更新:请求注释中的代码。
带有滑块的图片是一个Python脚本,您可以在GitHub上找到
带有最终结果的详细图片代码:

import cv2
import numpy as np  
# load image
img = cv2.imread("BPcph.jpg")
# convert to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 
# set lower and upper color limits
lower_val = np.array([58,204,219])
upper_val = np.array([101,255,255])
# Threshold the HSV image 
mask = cv2.inRange(hsv, lower_val, upper_val)
# remove noise
kernel =  np.ones((5,5),np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
# find contours in mask
im2, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw contours
for cnt in contours:
    cv2.drawContours(img,[cnt],0,(0,0,255),2)
#show image
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()