Skip to content

Instantly share code, notes, and snippets.

@acharles7
Created August 2, 2019 19:16
Show Gist options
  • Save acharles7/aaf48ae9a969d20fcd1efb131bd314cc to your computer and use it in GitHub Desktop.
Save acharles7/aaf48ae9a969d20fcd1efb131bd314cc to your computer and use it in GitHub Desktop.
The pixel values falling below or above that threshold can be classified accordingly as an object or the background is known as Threshold Segmentation
from skimage.color import rgb2gray
import matplotlib.pyplot as plt
img = plt.imread('cat.jpg')
gray = rgb2gray(img)
gray_r = gray.reshape(gray.shape[0]*gray.shape[1])
global_threshold = gray_r.mean()
for i in range(gray_r.shape[0]):
if gray_r[i] > global_threshold:
gray_r[i] = 1
else:
gray_r[i] = 0
gray = gray_r.reshape(gray.shape[0], gray.shape[1])
plt.imshow(gray, cmap='gray')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment