【发布时间】: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