如何解决使用 OpenCV Python 从航拍图像中检测网球场
如何从航拍图像中检测和标记网球场?具体来说,根据我迄今为止所做的尝试,我有 3 个子问题:
请参考我的代码和下面显示的两个不同的输出图像。一般的想法是只检测适合网球场形状的白色矩形。迄今为止,我已尝试使用 HoughLines(未成功),现在使用 findContours。
- 参考Image Output1。如何检测仅矩形?在我的代码中,我尝试只保留 4 个边的形状,但这并不总是有效。
- 参考Image Output2。通过检测矩形,我如何才能精确检测网球场形状? IE。矩形内的矩形。目前,我捕捉非网球场的矩形(例如篮球场或停车场)。
- 参考Image Output1 假设我只能检测网球场,我如何只标记一次?
我的代码:
import numpy as np
import cv2
# read image
img = cv2.imread('images/149.jpg')
# define the list of boundaries (keeping only light to white colours)
boundaries = [([80,80,80],[255,255,255])]
# loop over the boundaries and filter out (mask) dark pixels
for (lower,upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower,dtype="uint8")
upper = np.array(upper,dtype="uint8")
# find the colors within the specified boundaries and apply the mask
mask = cv2.inRange(img,lower,upper)
output = cv2.bitwise_and(img,img,mask=mask)
# convert image to grayscale and apply thresholding
gray = cv2.cvtColor(output,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,100,cv2.THRESH_BINARY)
# detect edges
blur = cv2.GaussianBlur(thresh,(5,5),1)
canny = cv2.Canny(blur,10,50)
# find all contours (curves joining continuous pts,having same color or intensity)
contours = cv2.findContours(canny,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)[0]
# only draw contours that meet filter criteria (rectangles of minimum size)
for cnt in contours:
epsilon = 0.05*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
if cv2.contourArea(cnt) > 1000 and len(approx) == 4:
cv2.drawContours(img,[cnt],(0,0),2)
(x,y,w,h) = cv2.boundingRect(approx)
cv2.putText(img,"tennis court",(x,y),cv2.FONT_HERSHEY_SIMPLEX,0.6,(255,2)
# display original image with contours drawn
cv2.imshow('Contours',img)
cv2.waitKey(0)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。