Created
September 7, 2022 00:28
-
-
Save saratrajput/c862b202f2ec70e7558d71b8b3dd9931 to your computer and use it in GitHub Desktop.
OpenCV Code Snippets
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 cv2 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# To add a colored mask over a image | |
def add_colored_mask(image, mask): | |
color = (0, 0, 255) # Red. | |
image_colored = np.zeros(image.shape, image.dtype) | |
image_colored[:,:] = color | |
mask_colored = cv2.bitwise_and(image_colored, image_colored, mask=mask) | |
cv2.addWeighted(mask_colored, 1, image, 1, 0, image) | |
# Display OpenCV image in Jupyter Notebook | |
def show_image(image): | |
cv2.imshow("Image", image) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
# Display OpenCV image with matplotlib in Jupyter Notebook | |
def show_image(image): | |
plt.figure() | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
plt.imshow(image) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment