Created
November 8, 2024 04:41
-
-
Save timrprobocom/88ce0b423a609730f1827cdc0ab69230 to your computer and use it in GitHub Desktop.
Example translating matlab to Python.
This file contains hidden or 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 | |
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