|
import cv2
|
|
import numpy as np
|
|
|
|
|
|
def pitch(img):
|
|
"""
|
|
Detects the cricket pitch in a given image frame using color-based segmentation and edge detection.
|
|
|
|
Args:
|
|
img: The input image frame (BGR format).
|
|
|
|
Returns:
|
|
contours: A list of contours detected in the color-masked and edge-processed image,
|
|
presumed to be the pitch. Returns an empty list if no contours are found.
|
|
"""
|
|
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
|
imgBlur= cv2.GaussianBlur(imgGray, (5, 5), 1)
|
|
imgThreshold = cv2.Canny(imgBlur, 190, 167)
|
|
kernel = np.ones((5, 5))
|
|
imgDial = cv2.dilate(imgThreshold, kernel, iterations = 2)
|
|
imgThreshold = cv2.erode(imgDial, kernel, iterations = 2)
|
|
|
|
|
|
lower = np.array([190, 167, 99])
|
|
upper = np.array([255, 255, 184 ])
|
|
|
|
|
|
mask = cv2.inRange(imgGray, lower, upper)
|
|
|
|
|
|
width = 264
|
|
height = 2256
|
|
|
|
imgContours = img.copy()
|
|
|
|
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
return contours
|
|
|
|
|
|
if __name__ == "__main__":
|
|
cap = cv2.VideoCapture(r"lbw.mp4")
|
|
|
|
while True:
|
|
frame, img = cap.read()
|
|
if not frame:
|
|
break
|
|
|
|
pitch_contours = pitch(img)
|
|
|
|
img_contours = img.copy()
|
|
for cnt in pitch_contours:
|
|
if (
|
|
cv2.contourArea(cnt) > 50000
|
|
):
|
|
cv2.drawContours(
|
|
img_contours, cnt, -1, (0, 255, 0), 10
|
|
)
|
|
|
|
cv2.imshow("Pitch Detection (Placeholder)", img_contours)
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord("q"):
|
|
break
|
|
|
|
cap.release()
|
|
cv2.destroyAllWindows()
|
|
|