Skip to content

Instantly share code, notes, and snippets.

@timrprobocom
Created November 8, 2024 04:41
Show Gist options
  • Save timrprobocom/88ce0b423a609730f1827cdc0ab69230 to your computer and use it in GitHub Desktop.
Save timrprobocom/88ce0b423a609730f1827cdc0ab69230 to your computer and use it in GitHub Desktop.
Example translating matlab to Python.
import cv2
import numpy as np
img1 = cv2.imread('NewCuyama.jpg')
img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
# img is now a numpy array.
# Find the mean of all non-zero pixels.
avg = np.mean( img[img.nonzero()] )
print(img.shape)
print("Average", avg)
cv2.imshow('original',img)
cv2.waitKey(0)
# Set those below average to zero.
img[img <= avg] = 0
cv2.imshow('zero all below mean',img)
cv2.waitKey(0)
# Set pixels under 21% of mean to zero.
img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img[img <= 0.21*avg] = 0
cv2.imshow('zero below 21% of mean',img)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment