一周前我开始学习 Python 和深度学习,因为我想帮助我女儿分析她的板球比赛技巧。我使用网络上的以下代码作为我的起点,因为这是我可以引用的最接近的代码.. 这是代码的链接.. https://www.analyticsvidhya.com/blog/2020/03/ball-tracking-cricket-computer-vision/
虽然我能够理解并执行代码直到最后一步,但是我被代码的以下部分卡住了,并且自过去 2 天以来我无法继续,这是在我完成所有可能的研究之后。
以下是部分代码,代码行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) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/shapedescr.cpp:784: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'
最佳答案
尝试使用以下内容:
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]
关于python - 板球跟踪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61838207/