Skip to content

Instantly share code, notes, and snippets.

@abdul-rehman-2050
Created April 6, 2016 11:27
Show Gist options
  • Save abdul-rehman-2050/297538c634d9c8ba93f05406658214e1 to your computer and use it in GitHub Desktop.
Save abdul-rehman-2050/297538c634d9c8ba93f05406658214e1 to your computer and use it in GitHub Desktop.
matplotlib example to display binary images as a plot
import cv2
from matplotlib import pyplot as plt
cnt=1
if __name__ == '__main__':
video_capture = cv2.VideoCapture(0)
while cnt<5:
# Capture frame-by-frame
ret, frame = video_capture.read()
#convert to grayscale image
img = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
#binarization
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]
for i in xrange(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()
cnt=cnt+1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment