Reputation: 143
My goal is to try and isolate the court in the below frame and to outline it:
I'm using OpenCV for Python and here are my results after taking the following steps:
And here is the result from my Canny edge detector:
As you can see, my Canny detector performed very poorly and there is a lot of noise in my mask. I tried certain techniques including erosion and dilation but they didn't help too much.
What else can I do to make sure when I pass the mask along to the Hough Line Transformer, it will actually be able to detect the edges of the court?
Here is some code for reference:
img = cv2.imread('imgs/bulls.jpg')
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
court_color = np.uint8([[[160,221,248]]])
hsv_court_color = cv2.cvtColor(court_color, cv2.COLOR_BGR2HSV)
hue = hsv_court_color[0][0][0]
# define range of blue color in HSV
lower_color = np.array([hue - 10,10,10])
upper_color = np.array([hue + 10,255,255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv_img, lower_color, upper_color)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)), plt.title('Original Image'), plt.show()
plt.imshow(mask, cmap='Greys'), plt.title('Mask'), plt.savefig('imgs/mask.jpg'), plt.show()
# Erosion
kernel = np.ones((2,2),np.uint8)
erosions2 = cv2.erode(mask,kernel,iterations = 5)
# Dilation
dilation = cv2.dilate(mask,kernel,iterations = 3)
# Opening
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
# Closing
closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
EDIT: I am attempting to replicate this research: web.stanford.edu/class/ee368/Project_Spring_1415/Reports/…. I want to isolate the court by detecting the straight lines that outline it so that I can eventually use homography to find coordinates of players on the court.
Upvotes: 2
Views: 1060
Reputation: 3408
Detecting Hough lines on the image is your best bet in this case, because court colors can change from place to place and also the camera settings. Detecting the lines, and some further processing using uniform color patches should allow you to segment the court region with some accuracy.
Upvotes: 1