這是我的圖片,我想檢測紅球:

但是,我根本無法讓代碼正常作業。我嘗試過使用不同的 param1 和 param2 值、更大的 dp 值,甚至重新縮放影像。
對此(甚至是檢測球的替代方法)的任何幫助將不勝感激。
代碼:
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)
uj5u.com熱心網友回復:
這是我的嘗試。這個想法是找到球,假設是場景中最飽和的物體之一。這應該覆寫所有明亮的物體,與它們的顏色無關。
我不使用霍夫的圓圈,因為它有點難以引數化,而且它通常不能很好地擴展到其他影像。相反,我只是檢測二進制影像上的斑點并計算斑點圓度,假設我正在尋找的東西接近一個圓(并且它的圓度應該接近1.0)。
這是代碼:
# 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)
讓我們看看我們只尋找高飽和度值的 HSV 掩碼:
沒關系,感興趣的物件在那里,但是面具很吵。讓我們嘗試一些形態學來定義更多的那些斑點:
# Apply Dilate Erode:
kernel = np.ones((3, 3), np.uint8)
binaryMask = cv2.morphologyEx(binaryMask, cv2.MORPH_DILATE, kernel, iterations=1)
這是過濾后的影像:
現在,讓我檢測輪廓并計算輪廓屬性以過濾噪聲。我會將感興趣的 blob 存盤在一個名為的串列中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)
我設定了一個圓度和最小面積測驗來過濾嘈雜的斑點。detectedCircles所有相關的 blob 都以擬合圓的形式存盤在串列中。讓我們看看結果:
看起來不錯。感興趣的斑點被藍色圓圈包圍,噪聲被紅色圓圈包圍。現在,讓我們為球嘗試另一種顏色。我創建了一個帶有藍色球而不是紅色球的影像版本,結果如??下:
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511472.html
