How it works...

It's convenient to represent binary images—images containing only black and white pixels—using np.uint8 arrays with only 0 and 255 values correspondingly. Both OpenCV and NumPy support all usual binary operators: NOT, AND, OR, and XOR. They are available through aliases, such as ~, &, |, ^, as well as through functions such as cv2.bitwise_not/np.bitwise_not and cv2.bitwise_and/np.bitwise_and.

After running the preceding code, the following output is expected:

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.