Created
March 16, 2019 23:52
-
-
Save ChongyeWang/e4ac36fad95d882b3851e8ba894154e3 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# Load our image as greyscale | |
image = cv2.imread('images/gradient.jpg',0) | |
cv2.imshow('Original', image) | |
# Values below 127 goes to 0 (black, everything above goes to 255 (white) | |
ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) | |
cv2.imshow('1 Threshold Binary', thresh1) | |
# Values below 127 go to 255 and values above 127 go to 0 (reverse of above) | |
ret,thresh2 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV) | |
cv2.imshow('2 Threshold Binary Inverse', thresh2) | |
# Values above 127 are truncated (held) at 127 (the 255 argument is unused) | |
ret,thresh3 = cv2.threshold(image, 127, 255, cv2.THRESH_TRUNC) | |
cv2.imshow('3 THRESH TRUNC', thresh3) | |
# Values below 127 go to 0, above 127 are unchanged | |
ret,thresh4 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO) | |
cv2.imshow('4 THRESH TOZERO', thresh4) | |
# Resever of above, below 127 is unchanged, above 127 goes to 0 | |
ret,thresh5 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO_INV) | |
cv2.imshow('5 THRESH TOZERO INV', thresh5) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
import cv2 | |
import numpy as np | |
# Load our new image | |
image = cv2.imread('images/Origin_of_Species.jpg', 0) | |
cv2.imshow('Original', image) | |
cv2.waitKey(0) | |
# Values below 127 goes to 0 (black, everything above goes to 255 (white) | |
ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) | |
cv2.imshow('Threshold Binary', thresh1) | |
cv2.waitKey(0) | |
# It's good practice to blur images as it removes noise | |
image = cv2.GaussianBlur(image, (3, 3), 0) | |
# Using adaptiveThreshold | |
thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, | |
cv2.THRESH_BINARY, 3, 5) | |
cv2.imshow("Adaptive Mean Thresholding", thresh) | |
cv2.waitKey(0) | |
_, th2 = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) | |
cv2.imshow("Otsu's Thresholding", thresh) | |
cv2.waitKey(0) | |
# Otsu's thresholding after Gaussian filtering | |
blur = cv2.GaussianBlur(image, (5,5), 0) | |
_, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) | |
cv2.imshow("Guassian Otsu's Thresholding", thresh) | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment