Skip to content

Instantly share code, notes, and snippets.

@LeiZheng
Created July 30, 2019 03:55
# shift the image x = -100, y = -100 since the pic generated has 100, 100 offset.
rows, cols, d3 = rink.shape
M = np.float32([[1, 0, -offset], [0, 1, -offset]])
transformedImage = cv2.warpAffine(image, M, (cols, rows))
# resize the rink image to match with play image.
transformedRink = cv2.resize(rink, None, fx=300.0 * 2 / rows, fy=610.0 * 2 / cols, interpolation=cv2.INTER_CUBIC)
# covert the transformRink to black/white mask
rinkMask = cv2.cvtColor(transformedRink, cv2.COLOR_BGR2GRAY)
h, rinkMask = cv2.threshold(rinkMask, 10, 255, cv2.THRESH_BINARY)
cv2.imshow("rinkMask", rinkMask)
# use this mask to truncate the play rink image
rows, cols = rinkMask.shape
imageMask = cv2.bitwise_and(cv2.cvtColor(rinkMask, cv2.COLOR_GRAY2BGR), transformedImage[0:rows, 0:cols])
# convert the truncated play image (only have the rink part) to black/white mask
imageMask = cv2.cvtColor(imageMask, cv2.COLOR_BGR2GRAY)
h, imageMask = cv2.threshold(imageMask, 10, 255, cv2.THRESH_BINARY)
cv2.imshow("imageMask", imageMask)
# create the red mask to merge with the normalized rink image.
cropImage = transformedImage[0:rows, 0:cols]
cropImage[imageMask != 0] = [0, 0, 255]
cropImage[imageMask == 0] = [0, 0, 0]
cv2.imshow("cropImage", cropImage)
highlightRink = cv2.addWeighted(cropImage, 0.1, transformedRink, 0.9, 0)
cv2.imshow("highlightRink", highlightRink)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment