Finding suitable HSV color

asked 2020-07-20 06:16:19 -0600

Syamim gravatar image

Hi, Im a new learner in OpenCV. Could you please help me in finding the suitble hsv range color for both team and the goalkeeper. I have tested a lot of range, but still does not get a good result. Please help me. Color range that I need is orange(left keeper), green (right keeper), yellow/brown(for left team) and purple/pink(for right team). Could you provide an example. Thanks

import cv2
import numpy as np

cap = cv2.VideoCapture("pitch2.mkv")

kernel_dil = np.ones((0,0), np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
fgbg = cv2.createBackgroundSubtractorMOG2(history=0, varThreshold=100, detectShadows=False)

while True:

#read frame
ret, frame1 = cap.read()

#screen Resize
frame = cv2.resize(frame1,(1364,700),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)

#warpPerspective
width,height = 682,350
#width,height = 1023,525
pts1 = np.float32([[426,90],[940,90],[11,652],[1353,652]])
pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])
matrix = cv2.getPerspectiveTransform(pts1,pts2)
warpOutput = cv2.warpPerspective(frame,matrix,(width,height))

#implement ROI
mask = np.zeros(warpOutput.shape, dtype=np.uint8)
mask.fill(255)
roi_corners = np.array([[(0,340), (682,340), (682,20), (0,20)]], dtype=np.int32)
mask = cv2.fillPoly(mask, roi_corners, 0)
masking = cv2.bitwise_or(warpOutput, mask)

if ret == True:
    fgmask1 = fgbg.apply(masking,mask)
    fgmask2 = cv2.morphologyEx(fgmask1, cv2.MORPH_OPEN, kernel)
    dilation = cv2.dilate(fgmask2, kernel_dil, iterations = 0)
    (contours,hierarchy) = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    XContour = []
    maxX = 0
    xBefore = 0

    XCont = []
    max_x = 0
    xLast = 0

    for pic, contour in enumerate(contours):

        area = cv2.contourArea(contour)
        x,y,w,h = cv2.boundingRect(contour)
        if(area>5): #set to 2 to undetect the ball

            player_img = warpOutput[y:y+h,x:x+6]
            player_hsv = cv2.cvtColor(player_img,cv2.COLOR_BGR2HSV)

            lower_purple = np.array([150,150,100])
            upper_purple = np.array([184,255,255])

            lower_yellow = np.array([70,150,100])
            upper_yellow = np.array([100,255,255])

            lower_green = np.array([70,150,100])
            upper_green = np.array([100,255,255])

            lower_orange = np.array([70,150,100])
            upper_orange = np.array([100,255,255])

            mask_purple = cv2.inRange(player_img, lower_purple, upper_purple)
            output_purple = cv2.bitwise_and(player_hsv, player_img, mask = mask_purple)
            resPurple = cv2.cvtColor(output_purple,cv2.COLOR_HSV2BGR)
            resPurple = cv2.cvtColor(resPurple,cv2.COLOR_BGR2GRAY)
            nzCountPurple = cv2.countNonZero(resPurple)

            mask_yellow = cv2.inRange(player_img, lower_yellow, upper_yellow)
            output_yellow = cv2.bitwise_and(player_img, player_img, mask = mask_yellow)
            resYellow = cv2.cvtColor(output_yellow,cv2.COLOR_HSV2BGR)
            resYellow = cv2.cvtColor(resYellow,cv2.COLOR_BGR2GRAY)
            nzCountYellow = cv2.countNonZero(resYellow)

            mask_green = cv2.inRange(player_img, lower_green, upper_green)
            output_green = cv2.bitwise_and(player_img, player_img, mask = mask_green)
            resGreen = cv2.cvtColor(output_green,cv2.COLOR_HSV2BGR)
            resGreen = cv2.cvtColor(resGreen,cv2.COLOR_BGR2GRAY)
            nzCountGreen = cv2.countNonZero(resGreen)

            mask_orange = cv2.inRange(player_img, lower_orange, upper_orange)
            output_orange = cv2.bitwise_and(player_img, player_img, mask = mask_orange)
            resOrange = cv2.cvtColor(output_orange,cv2.COLOR_HSV2BGR)
            resOrange = cv2.cvtColor(resOrange,cv2.COLOR_BGR2GRAY)
            nzCountOrange = cv2.countNonZero(resOrange)

            if(nzCountPurple>=5):
                    cv2.rectangle(warpOutput, (x,y), (x+w,y+h), (0,255,255), 1)

            elif(nzCountYellow>=5):
                cv2.rectangle(warpOutput, (x,y), (x+w,y+h), (0,0,0), 1)

            elif(nzCountGreen>=5):
                cv2.rectangle(warpOutput, (x,y), (x+w,y+h), (255 ...
(more)
edit retag flag offensive close merge delete