【问题标题】:Real-time ellipsoid detection using OpenCV feature tools使用 OpenCV 特征工具进行实时椭球检测
【发布时间】:2020-12-30 04:54:55
【问题描述】:

我正在尝试创建一个程序来从实时摄像头馈送中检测椭圆体形状。目标是识别餐具的边缘(例如,碗或杯子)。在许多 OpenCV 教程和文档页面之后,我尝试了 FAST 角点检测、Canny 边缘(具有最小指定区域的拨号轮廓)、Sobel 边缘和 Hough 圆。即使调整参数,这些似乎都不起作用。

作为参考,这里是每个方法的一个示例场景的单帧的 imgur 相册 https://imgur.com/a/A7d7UxI

countours 的多边形近似依赖于角的数量,但任何具有 4 个以上角的复杂形状似乎都被标记为“圆形或椭圆形”。同样,纯圆检测会产生重复的发现,其中标记了椭圆体的每个切线拟合圆。拐角检测有点失败,因为曲线形状显然没有拐角。

老实说,我很难过。我对 OpenCV 的经验有限,不知道是否有办法检测和标记椭圆体(连续的或破碎的),以便在屏幕上找到它们的边缘位置。任何方向或帮助将不胜感激(我可以自己进行一些挖掘),我最大的领导是试图从 Sobel 检测中识别出最强的可见轮廓,但无法验证它们的曲率或识别这些线条。

虽然没有明确相关,但这里是我用来显示来自相机提要的每一个的 WIP 代码。

import numpy as np
import cv2

def empty():
    pass

def getContours(img,imgContour):
    contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 1000:
            cv2.drawContours(imgContour, cnt, -1, (255, 0, 255), 7)
            peri = cv2.arcLength(cnt, False)
            approx = cv2.approxPolyDP(cnt, 0.02 * peri, False)
            if len(approx) > 5:
                x, y, w, h = cv2.boundingRect(approx)
                cv2.rectangle(imgContour, (x, y), (x + w, y + h), (0, 255, 0), 5)

# Capturing from the video input
# Use VideoCapture(0) for default webcam
# I use a feed from my phone via USB, which is webcam 2 that uses VideoCapture(1)
cap = cv2.VideoCapture(1)

"""Trackbar"""
cv2.namedWindow("Parameters")
cv2.createTrackbar("Threshold1_Canny", "Parameters", 150, 255, empty)
cv2.createTrackbar("Threshold2_Canny", "Parameters", 255, 255, empty)

while True:
    # reading the frame
    ret, frame = cap.read()
    # flipping the frame
    frame = cv2.flip(frame, 1)
    if ret:

        """Image Preprocessing"""
        img = cv2.GaussianBlur(frame, (21, 21), cv2.BORDER_DEFAULT)
        imgContour = img.copy()
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        """FAST Corner Detection"""
        # Initiate FAST object with default values
        fast = cv2.FastFeatureDetector_create()

        # find and draw the keypoints
        kp = fast.detect(img,None)
        img2 = cv2.drawKeypoints(img, kp, None, color=(255,0,0))

        # Disable nonmaxSuppression
        fast.setNonmaxSuppression(0)
        kp = fast.detect(img,None)
        imgFAST = cv2.drawKeypoints(img, kp, None, color=(255,0,0))

        """Canny Edge Detection"""
        threshold1 = cv2.getTrackbarPos("Threshold1_Canny", "Parameters")
        threshold2 = cv2.getTrackbarPos("Threshold2_Canny", "Parameters")
        edges = cv2.Canny(img, threshold1, threshold2)
        kernal = np.ones((5, 5))
        imgDil = cv2.dilate(edges, kernal, iterations=1)
        getContours(imgDil,imgContour)

        """Hough Circles"""
        #cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
        #circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,100,
        #                           param1=50,param2=30,minRadius=60,maxRadius=400)
        #circles = np.uint16(np.around(circles))


        """Sobel Edge Detection"""
        imgSobel = cv2.Sobel(img, cv2.CV_16S, 0, 1, ksize=-1)
        imgSobel = np.absolute(imgSobel)
        imgSobel = np.uint8(imgSobel)

        """Show All Processing"""
        cv2.imshow('Raw', frame)
        cv2.imshow('FAST', imgFAST)
        cv2.imshow('Canny', imgDil)
        cv2.imshow('Sobel', imgSobel)
        #cv2.imshow('Hough', cimg)

        """Key Controls"""
        # getting the input from the keyboard
        k = cv2.waitKey(1) & 0xFF

        # press 'q' to quit
        if k == ord('q'):
            cap.release()
            break

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python opencv object-detection


    【解决方案1】:

    OpenCV 提供了三种不同的函数来将一组点拟合到椭圆:

    这些函数对点列表(即轮廓)进行操作,因此您可以将这些函数中的任何一个与轮廓一起使用。我建议的方法是:

    • 二值化图像
    • 检测二值图像中的轮廓
    • 只保留外部轮廓(即碗有很多椭圆,但实际上您只需要最外面的)
    • 对于每个剩余轮廓:
      • 拟合椭圆
      • 将检测到的轮廓与椭圆的轮廓进行比较(例如使用matchShapes())。
      • 判断轮廓是否为椭圆的阈值

    此过程仅依赖于一个主要参数(比较阈值),该参数可以根据标记数据决定,因此作为一种算法并不算太糟糕。但是,这里的预处理步骤(Canny 或其他二值化)可能有很多变量,并且在给定光照等情况下可能会发生很大变化。

    一般而言,此类依赖图像处理的经典算法在受限环境(已知光照、固定摄像机位置等)中效果最佳,因为您实际上很可能可以找到效果良好的预处理参数(而且通常不是这样)在这些工业环境中很难提供几个滑块来动态更改阈值)。然而,对于不受约束的环境,例如在任意灯光下挥动手机指向任何颜色的物体,ML 可能会更好地为您服务,用您感兴趣的特定类训练物体或掩码检测器。

    【讨论】:

    • 谢谢,我会调查这些,并尝试约束环境以获得更一致的设置。
    猜你喜欢
    • 1970-01-01
    • 2012-06-14
    • 2014-03-13
    • 2012-12-24
    • 2013-03-01
    • 1970-01-01
    • 2017-08-08
    • 2016-10-16
    • 1970-01-01
    相关资源
    最近更新 更多