板球追踪问题

时间:2020-05-16 14:17:06

标签: python opencv

一周前,我开始学习Python和深度学习,当时我想帮助我的女儿分析她的板球比赛技巧。我使用网上的以下代码作为起点,因为这是我可以参考的最接近的代码。这是代码的链接。.https://www.analyticsvidhya.com/blog/2020/03/ball-tracking-cricket-computer-vision/

虽然我可以理解并执行代码,直到最后一步,但是我被下面的代码所困扰,并且自最近两天以来无法继续进行,但这是在我尽一切可能进行研究之后。

以下是代码的一部分,代码行x,y,w,h = cv2.boundingRect(countours[i])提供了错误。

!rm -r ball/*

ball_df = pd.DataFrame(columns=['frame','x','y','w','h'])
for idx in range(len(frames)):

    img= cv2.imread('FFrames/' + frames[idx])
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray,(25, 25),0)
    _ , mask = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
    image, contours = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)


    num=20
    cnt=0
    df = pd.DataFrame(columns=['frame','x','y','w','h'])
    for i in range(len(contours)):
        x,y,w,h = cv2.boundingRect(contours[i]) 

        numer=min([w,h])
        denom=max([w,h])
        ratio=numer/denom

        if(x>=num and y>=num):
            xmin=x-num
            ymin=y-num
            xmax=x+w+num
            ymax=y+h+num
        else:
            xmin=x
            ymin=y
            xmax=x+w
            ymax=y+h

        if(ratio>=0.5):    
            #print(cnt,x,y,w,h,ratio)
            df.loc[cnt,'frame'] = frames[idx]
            df.loc[cnt,'x']=x
            df.loc[cnt,'y']=y
            df.loc[cnt,'w']=w
            df.loc[cnt,'h']=h

            cv2.imwrite("patch/"+str(cnt)+".png",img[ymin:ymax,xmin:xmax])
            cnt=cnt+1

错误:

cv2.error:OpenCV(4.2.0) /用户/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:784: 错误:(-215:声明失败)npoints> = 0 &&(深度== CV_32F ||) 'pointSetBoundingRect'中的depth == CV_32S)

1 个答案:

答案 0 :(得分:1)

尝试使用以下内容:

cnt = contours[i]
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])

x, w = leftmost[0], rightmost[0]
y, h = topmost[1], bottommost[1]