figure 1
Let's do more adventure following yesterday's work. Highlight the rink to show the active play area as following:
figure 2
The red highlight area is exactly the active play area from figure 2.
In order to get it, firstly resize the rink image to match with play image. so we can use for pixel to pixel mapping.
create black/white mask from resized rink image:
figure 3
truncate the play image suing figure 3 and then create active red area
figure 4
Finally weightly add figure 4 to the normalized to generate figure 1.Here is source code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Comments
Post a Comment