简体   繁体   中英

OpenCV Hough Circles Transform not detecting cricket ball

This is the picture I have, and I want to detect the red ball:

在此处输入图像描述

However, I simply cannot get the code to work. I've tried experimenting with different param1 and param2 values, larger dp values, and even rescaling the image.

Any help on this (or even an alternate method for detecting the ball) would be much appreciated.

CODE:

frame = cv.imread("cricket_ball.png")

# Convert frame to grayscale
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)


# cv.HoughCircles returns a 3-element floating-point vector (x,y,radius) for each circle detected
circles = cv.HoughCircles(gray,cv.HOUGH_GRADIENT,1,minDist=100, minRadius=2.5,maxRadius=10) # Cricket ball on videos are approximately 10 pixels in diameter.

print(circles)

# Ensure at least one circle was found
if circles is not None:
    # Converts (x,y,radius to integers)
    circles = np.uint8(np.around(circles))

    for i in circles[0,:]:
        cv.circle(frame, (i[0],i[1]), i[2], (0,255,0), 20) # Produce circle outline

cv.imshow("Ball", frame)
    
cv.waitKey(0)

Here's my attempt. The idea is to find the ball assuming is (one) of the most saturated objects in the scene. This should cover all bright objects, independent of their color.

I don't use Hough's circles because its a little bit difficult to parametrize and it often doesn't scale well to other image. Instead, I just detect blobs on a binary image and calculate blob circularity , assuming the thing I'm looking for is close to a circle (and its circularity should be close to 1.0 ).

This is the code:

# imports:
import cv2
import numpy as np

# image path
path = "D://opencvImages//"
fileName = "fv8w3.png"

# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)
# Deep copy for results:
inputImageCopy = inputImage.copy()

# Convert the image to the HSV color space:
hsvImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2HSV)

# Set the HSV values:
lowRange = np.array([0, 120, 0])
uppRange = np.array([179, 255, 255])

# Create the HSV mask
binaryMask = cv2.inRange(hsvImage, lowRange, uppRange)

Let's check out what kind of HSV mask we get looking only for high Saturation values:

It's all right, the object of interest is there, but the mask is noisy. Let's try some morphology to define a little bit more those blobs:

# Apply Dilate + Erode:
kernel = np.ones((3, 3), np.uint8)
binaryMask = cv2.morphologyEx(binaryMask, cv2.MORPH_DILATE, kernel, iterations=1)

This is the filtered image:

Now, let me detect contours and compute contour properties to filter the noise. I'll store the blobs of interest in a list called detectedCircles :

# Find the circle blobs on the binary mask:
contours, hierarchy = cv2.findContours(binaryMask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Store the circles here:
detectedCircles = []

# Alright, just look for the outer bounding boxes:
for i, c in enumerate(contours):

    # Get blob area:
    blobArea = cv2.contourArea(c)
    print(blobArea)

    # Get blob perimeter:
    blobPerimeter = cv2.arcLength(c, True)
    print(blobPerimeter)

    # Compute circulariity
    blobCircularity = (4 * 3.1416 * blobArea)/(blobPerimeter**2)
    print(blobCircularity)

    # Set min circularuty:
    minCircularity = 0.8

    # Set min Area
    minArea = 35

    # Approximate the contour to a circle:
    (x, y), radius = cv2.minEnclosingCircle(c)

    # Compute the center and radius:
    center = (int(x), int(y))
    radius = int(radius)

    # Set Red color (unfiltered blob)
    color = (0, 0, 255)

    # Process only big blobs:
    if blobCircularity > minCircularity and blobArea > minArea:

        # Set Blue color (filtered blob)
        color = (255, 0, 0)

        # Store the center and radius:
        detectedCircles.append([center, radius])

    # Draw the circles:
    cv2.circle(inputImageCopy, center, radius, color, 2)

    cv2.imshow("Circles", inputImageCopy)
    cv2.waitKey(0)

I've set a circularity and minimum area test to filter the noisy blobs. All the relevant blobs are stored in the detectedCircles list as fitted circles. Let's see the result:

Looks good. The blob of interested is enclosed by a blue circle and the noise with a red one. Now, let's try another color for the ball. I created a version of the image with a blue ball instead of a red one , this is the result:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM