File size: 1,939 Bytes
2649d00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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)
    # Find all contours

    width = 264
    height = 2256
                 
    imgContours = img.copy()
    # imgWrap = 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")  # Adjust path if needed

    while True:
        frame, img = cap.read()
        if not frame:
            break

        pitch_contours = pitch(img)  # Call the placeholder pitch detection

        img_contours = img.copy()
        for cnt in pitch_contours:
            if (
                cv2.contourArea(cnt) > 50000
            ):  # Example area filtering - adjust as needed
                cv2.drawContours(
                    img_contours, cnt, -1, (0, 255, 0), 10
                )  # Draw pitch contours in green

        cv2.imshow("Pitch Detection (Placeholder)", img_contours)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    cap.release()
    cv2.destroyAllWindows()