在视频中找到红色圆圈

时间:2015-11-21 19:32:10

标签: python c++ opencv

我想用Python(或C ++)中的OpenCV跟踪网络摄像头Feed中可乐瓶的移动上限。我试图搜索框架中的所有红色,然后我使用某种HOUGH TRANSFORM来搜索圆圈。 我找不到圆的正确半径并修复它所以它不会改变每一帧。处理时间不是那么重要我不想要实时检测但我确实想要一个精确的红圈检测。

这是我到目前为止所做的:

import cv2
import numpy as np
import cv2.cv as cv
cap = cv2.VideoCapture(0)
while(1):

    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV 
    lower_red = np.array([160,140,50]) 
    upper_red = np.array([180,255,255])

    imgThreshHigh = cv2.inRange(hsv, lower_red, upper_red)

    imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    thresh = 18
    edges = cv2.Canny(imgray,thresh,thresh*3)

    circles = cv2.HoughCircles(imgThreshHigh, cv.CV_HOUGH_GRADIENT, 1, 500, 25, 75, 5, 15)
    maxRadius= 0
    xc = 0.00
    yc = 0.00
    found = False 
    if circles is not None:
        found = True
        for i in circles[0,:3]:
            if i[2] < 100:
                if i[2] > maxRadius:
                    maxRadius = i[2]
                    if maxRadius > 1.0:
                        # draw the outer circle
                        cv2.circle(frame,(i[0],i[1]),maxRadius,(0,0,255),2)
                        # draw the center of the circle
                        cv2.circle(frame,(i[0],i[1]),1,(0,0,255),3)
                        xc = i[0]
                        yc = i[1] 
    if found: 
        print "ball detected at position:",xc, ",", yc, " with radius:", maxRadius
    else: 
        print "no ball" 
    cv2.imshow('frame',frame)
    cv2.imshow('edges',edges)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break
cv2.destroyAllWindows()

我认为HOUGH TRANSFORM不适用于此。所以我想使用边缘。 如何使用(X-Xc)^2 + (Y-Yc)^2 =R^2之类的等式和轮廓来查找圆圈?

如果Hough Transform有改进,如果你与我分享,我会很感激

轮廓:

contours,hierarchy=cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

我的示例图片:---&gt;这不是我的例子Image.this是我想在视频中找到的对象。

enter image description here

1 个答案:

答案 0 :(得分:0)

对于你必须找到合适范围的颜色,对于人眼来说,瓶盖是红色的,但是对于相机可能是橙色或类似的东西。也受到光线的影响,因此相机可能会变白。哈哈,我在艰难的道路上学到了这一点。您可以使用轨迹栏和hsv值来执行代码,以获得对象的确切范围。

现在,你在寻找中心(x,y)吗?您可以尝试使用 cv2.moments()

Here is an example

您的代码是这样的

import cv2
import numpy as np
import cv2.cv as cv
cap = cv2.VideoCapture(0)

coords = []
while(1):

    # Take each frame
    _, frame = cap.read()

    # Convert BGR to HSV

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    # define range of blue color in HSV 
    lower_red = np.array([160,140,50]) 
    upper_red = np.array([180,255,255])

    imgThreshHigh = cv2.inRange(hsv, lower_red, upper_red)
    thresh = imgThreshHigh.copy()

    countours,_ = cv2.findContours(thresh, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

    for cnt in countours:
        area = cv2.contourArea(cnt)
        if area > max_area:
        max_area = area
        best_cnt = cnt

    M = cv2.moments(best_cnt)
    cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
    coord = cx, cy #This are your coordinates for the circle
    # area = moments['m00'] save the object area
    #perimeter = cv2.arcLength(best_cnt,True) is the object perimeter

    #Save the coords every frame on a list
    #Here you can make more conditions if you don't want repeated coordinates
    points.append(coord) 

    cv2.imshow('frame',frame)
    cv2.imshow('Object',thresh)
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break
cv2.destroyAllWindows()

有些时候你没有你提到的raidius hough变换因为每次计算物体的相对中心。

以下是我用来了解哪些是确切值的代码,希望它可以帮助您HSV detection code