2. Common Operations When Working with Images
Activity 2.01: Masking Using Binary Images
Solution:
- Create a new notebook –
Activity2.01.ipynb
. We will be writing our code in this notebook. - Import the required libraries:
# Import modules import cv2 import numpy as np import matplotlib.pyplot as plt %matplotlib inline
- Read the image of the disk and convert it to grayscale.
Note
Before proceeding, be sure to change the path to the image (highlighted) based on where the image is saved in your system.
The code for this is as follows:
img = cv2.imread("../data/recording.jpg") img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- Display the grayscale image using Matplotlib:
plt.imshow(img, cmap='gray') plt.show()
The output is as follows. The X and Y axes refer to the width and height of the image, respectively:
- Threshold this image with a threshold of 150 and a maximum value of 255:
# Set threshold and maximum value thresh = 150...