Created
January 15, 2019 22:03
-
-
Save moniquelive/d4d20af1b50d855c0bb01b80fd9a18a6 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
import cv2 | |
def mask_it(img, points): | |
height, width, depth = img.shape | |
mask = np.zeros( (height, width), np.uint8) | |
cv2.fillPoly(mask, pts = np.int32([points]), color=(255,255,255)) | |
return cv2.bitwise_and(img,img, mask=mask) | |
# | |
# LOAD IMAGE | |
# | |
import urllib.request | |
req = urllib.request.urlopen('https://docs.opencv.org/2.4-beta/_static/opencv-logo-white.png') | |
arr = np.asarray(bytearray(req.read()), dtype=np.uint8) | |
img = cv2.imdecode(arr, -1) # 'Load it as it is' | |
# | |
# DRAW RECTANGLE | |
# | |
height, width, depth = img.shape | |
top = height / 2 - 150 | |
left = width / 2 - 200 | |
bottom = height / 2 - 50 | |
right = width / 2 + 200 | |
contours = np.array( [ | |
[left, top], | |
[left, bottom], | |
[right, bottom], | |
[right, top] ] ) | |
masked = mask_it(img, contours) | |
# | |
# DISPLAY | |
# | |
cv2.imshow("original", img) | |
cv2.imshow("masked", masked) | |
cv2.waitKey() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment