import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load our image as greyscale
image = cv2.imread('./images/gradient.jpg')
new_image = np.copy(image)
new_image[new_image < 127] = 0
new_image[new_image >= 127] = 255
plt.figure(figsize=[10,5])
plt.subplot(121);plt.imshow(image, cmap='gray');plt.title("Original");
plt.subplot(122);plt.imshow(new_image, cmap='gray');plt.title("Output");
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load our image as greyscale
image = cv2.imread('./images/Lusin-1.jpg', 0)
new_image = np.copy(image)
new_image[new_image < 127] = 0
new_image[new_image >= 127] = 255
plt.figure(figsize=[10,5])
plt.subplot(121);plt.imshow(image, cmap='gray');plt.title("Original");
plt.subplot(122);plt.imshow(new_image, cmap='gray');plt.title("Output");
threshold(src, thresh, maxval, type[, dst])
maxval
: maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load our image as greyscale
image = cv2.imread('images/gradient.jpg',0)
# Values below 127 goes to 0 (black, everything above goes to 255 (white)
ret,new_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
print(ret)
plt.figure(figsize=[10,5])
plt.subplot(121);plt.imshow(image, cmap='gray');plt.title("Original");
plt.subplot(122);plt.imshow(new_image, cmap='gray');plt.title("Output");
127.0
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load our image as greyscale
image = cv2.imread('images/gradient.jpg',0)
# Values below 127 goes to 0 (black, everything above goes to 255 (white)
ret1,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# Values below 127 go to 255 and values above 127 go to 0 (reverse of above)
ret2,thresh2 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV)
# Values above 127 are truncated (held) at 127 (the 255 argument is unused)
ret3,thresh3 = cv2.threshold(image, 127, 255, cv2.THRESH_TRUNC)
# Values below 127 go to 0, above 127 are unchanged
ret4,thresh4 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO)
# Resever of above, below 127 is unchanged, above 127 goes to 0
ret5,thresh5 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO_INV)
plt.figure(figsize=[9,6])
plt.subplot(231);plt.imshow(thresh1, cmap='gray');plt.title("1 Threshold Binary");
plt.subplot(232);plt.imshow(thresh2, cmap='gray');plt.title("2 Threshold Binary Inverse");
plt.subplot(233);plt.imshow(thresh3, cmap='gray');plt.title("3 THRESH TRUNC");
plt.subplot(234);plt.imshow(thresh4, cmap='gray');plt.title("4 THRESH TOZERO");
plt.subplot(235);plt.imshow(thresh5, cmap='gray');plt.title("5 THRESH TOZERO INV");
plt.subplot(236);plt.imshow(image, cmap='gray');plt.title("Original");
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load our image as greyscale
image = cv2.imread('images/plate.jpg',0)
cv2.imshow('Original', image)
# Values below 127 goes to 0 (black, everything above goes to 255 (white)
ret1,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# Values below 127 go to 255 and values above 127 go to 0 (reverse of above)
ret2,thresh2 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV)
# Values above 127 are truncated (held) at 127 (the 255 argument is unused)
ret3,thresh3 = cv2.threshold(image, 127, 255, cv2.THRESH_TRUNC)
# Values below 127 go to 0, above 127 are unchanged
ret4,thresh4 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO)
# Resever of above, below 127 is unchanged, above 127 goes to 0
ret5,thresh5 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO_INV)
plt.figure(figsize=[10,5])
plt.subplot(231);plt.imshow(thresh1, cmap='gray');plt.title("1 Threshold Binary");
plt.subplot(232);plt.imshow(thresh2, cmap='gray');plt.title("2 Threshold Binary Inverse");
plt.subplot(233);plt.imshow(thresh3, cmap='gray');plt.title("3 THRESH TRUNC");
plt.subplot(234);plt.imshow(thresh4, cmap='gray');plt.title("4 THRESH TOZERO");
plt.subplot(235);plt.imshow(thresh5, cmap='gray');plt.title("5 THRESH TOZERO INV");
plt.subplot(236);plt.imshow(image, cmap='gray');plt.title("Original");
import cv2
import matplotlib.pyplot as plt
max_value = 255
max_type = 4
max_binary_value = 255
trackbar_type = 'Type: \n 0: Binary \n 1: Binary Inverted \n 2: Truncate \n 3: To Zero \n 4: To Zero Inverted'
trackbar_value = 'Value'
window_name = 'Threshold Demo'
def Threshold_Demo(val):
global dst, threshold_value, threshold_type
#0: Binary
#1: Binary Inverted
#2: Threshold Truncated
#3: Threshold to Zero
#4: Threshold to Zero Inverted
threshold_type = cv2.getTrackbarPos(trackbar_type, window_name)
threshold_value = cv2.getTrackbarPos(trackbar_value, window_name)
_, dst = cv2.threshold(src_gray, threshold_value, max_binary_value, threshold_type )
cv2.imshow(window_name, dst)
src = cv2.imread("images/plate.jpg" )
# Convert the image to Gray
src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
cv2.namedWindow(window_name,cv2.WINDOW_NORMAL)
cv2.resizeWindow(window_name, 400, 300)# Resize window to specified dimensions
cv2.createTrackbar(trackbar_type, window_name , 3, max_type, Threshold_Demo)
# Create Trackbar to choose Threshold value
cv2.createTrackbar(trackbar_value, window_name , 0, max_value, Threshold_Demo)
# Call the function to initialize
Threshold_Demo(0)
# Wait until user finishes program
cv2.waitKey()
cv2.destroyAllWindows()
thresh_type= ["Binary","Binary Inverted","Threshold Truncated","Threshold to Zero","Threshold to Zero Inverted"]
plt.imshow(dst, cmap='gray');plt.title("threshold: {} with type: {}".format(threshold_value, thresh_type[threshold_type]))
plt.show()