项目是这样的:我们将首先检测球员,如果有法国队和比利时的比赛,我们将标记检测到的法国和比利时球员。

算法

在我们的例子中,我们将处理法国与比利时之间比赛的精彩视频。

首先,我们将导入必要的库并读取视频。定义一些必要的变量。

#Import libraries

import cv2

import numpy as np

#Reading the video

vidcap = cv2.VideoCapture('cu;)

success,image = vidcap.read()

count = 0

success = True

idx = 0

现在一帧一帧地读视频,我们将帧分为HSV格式。需要将其转换为HSV,使用HSV图像,我们可以检测任何指定的颜色背景,从特定的颜色代码。在这里我们将首先检测绿色的地面,所以另一部分会变成黑色,所以很容易检测我们的球员。此外,HSV图像还将有助于根据球员的国籍进行检测。我们可以通过他们的蓝色球衣发现法国球员,通过他们的红色球衣发现比利时球员。Python代码如下

#Read the video frame by frame

while success:

#converting into hsv image

hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)

#green range

lower_green = np.array([40,40, 40])

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

#blue range

lower_blue = np.array([110,50,50])

upper_blue = np.array([130,255,255])

#Red range

lower_red = np.array([0,31,255])

upper_red = np.array([176,255,255])

#white range

lower_white = np.array([0,0,0])

upper_white = np.array([0,0,255])

我们将定义一个绿色范围的mask 来检测地面。

#Define a mask ranging from lower to uppper

mask = cv2.inRange(hsv, lower_green, upper_green)

#Do masking

res = cv2.bitwise_and(image, image, mask=mask)

#convert to hsv to gray

res_bgr = cv2.cvtColor(res,cv2.COLOR_HSV2BGR)

res_gray = cv2.cvtColor(res,cv2.COLOR_BGR2GRAY)

现在我们所有的图像都是这样的:

现在我们将对这些帧进行形态学闭合操作。关闭操作将填充人群中存在的噪声。因此可以减少错误检测。

#Defining a kernel to do morphological operation in threshold #image to get better output.

kernel = np.ones((13,13),np.uint8)

thresh = cv2.threshold(res_gray,127,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

现在是这样的:

现在我们会在每一帧中找到contours 。在找到contours 后,我们将只检查contours ,那里的高度大于宽度,这些将被检测球员。现在我们将做同样的事情,我们将对被检测的球员进行masking 操作,以检测他们的球衣颜色,如果球衣颜色是蓝色,我们将把文字写为“法兰西”,如果是红色,我们将把文字写为“比利时”。Python实现如下:

for c in contours:

x,y,w,h = cv2.boundingRect(c)

#Detect players

if(h>=)*w):

if(w>15 and h>= 15):

idx = idx+1

player_img = image[y:y+h,x:x+w]

player_hsv = cv2.cvtColor(player_img,cv2.COLOR_BGR2HSV)

#If player has blue jersy

mask1 = cv2.inRange(player_hsv, lower_blue, upper_blue)

res1 = cv2.bitwise_and(player_img, player_img, mask=mask1)

res1 = cv2.cvtColor(res1,cv2.COLOR_HSV2BGR)

res1 = cv2.cvtColor(res1,cv2.COLOR_BGR2GRAY)

nzCount = cv2.countNonZero(res1)

#If player has red jersy

mask2 = cv2.inRange(player_hsv, lower_red, upper_red)

res2 = cv2.bitwise_and(player_img, player_img, mask=mask2)

res2 = cv2.cvtColor(res2,cv2.COLOR_HSV2BGR)

res2 = cv2.cvtColor(res2,cv2.COLOR_BGR2GRAY)

nzCountred = cv2.countNonZero(res2)

if(nzCount >= 20):

#Mark blue jersy players as france

cv2.putText(image, 'France', (x-2, y-2), font, 0.8, (255,0,0), 2, cv2.LINE_AA)

cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),3)

else:

pass

if(nzCountred>=20):

#Mark red jersy players as belgium

cv2.putText(image, 'Belgium', (x-2, y-2), font, 0.8, (0,0,255), 2, cv2.LINE_AA)

cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),3)

else:

pass

现在对于检测足球,我们将使用同样的masking 方法检测白色。

if((h>=1 and w>=1) and (h<=30 and w<=30)):

player_img = image[y:y+h,x:x+w]

player_hsv = cv2.cvtColor(player_img,cv2.COLOR_BGR2HSV)

#white ball detection

mask1 = cv2.inRange(player_hsv, lower_white, upper_white)

res1 = cv2.bitwise_and(player_img, player_img, mask=mask1)

res1 = cv2.cvtColor(res1,cv2.COLOR_HSV2BGR)

res1 = cv2.cvtColor(res1,cv2.COLOR_BGR2GRAY)

nzCount = cv2.countNonZero(res1)

if(nzCount >= 3):

# detect football

cv2.putText(image, 'football', (x-2, y-2), font, 0.8, (0,255,0), 2, cv2.LINE_AA)

cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),3)

相关推荐