Skip to content

Instantly share code, notes, and snippets.

@femioladeji
Last active July 10, 2022 00:35
Show Gist options
  • Save femioladeji/b4e084cb14a098db1a859ca5206c258c to your computer and use it in GitHub Desktop.
Save femioladeji/b4e084cb14a098db1a859ca5206c258c to your computer and use it in GitHub Desktop.
Read the car image and convert it to binary
from skimage.io import imread
from skimage.filters import threshold_otsu
import matplotlib.pyplot as plt
car_image = imread("car.jpg", as_gray=True)
# it should be a 2 dimensional array
print(car_image.shape)
# the next line is not compulsory however, a grey scale pixel
# in skimage ranges between 0 & 1. multiplying it with 255
# will make it range between 0 & 255 (something we can relate better with
gray_car_image = car_image * 255
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(gray_car_image, cmap="gray")
threshold_value = threshold_otsu(gray_car_image)
binary_car_image = gray_car_image > threshold_value
ax2.imshow(binary_car_image, cmap="gray")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment