import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('apple.jpg',0) ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY) ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV) ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC) ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO) ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV) titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV'] images = [img, thresh1, thresh2, thresh3, thresh4, thresh5] cv2.imshow(titles[0],images[0]) cv2.imshow(titles[1],images[1]) cv2.imshow(titles[2],images[2]) cv2.imshow(titles[3],images[3]) cv2.imshow(titles[4],images[4]) cv2.imshow(titles[5],images[5]) if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()