Combining Mahotas and OpenCV
Just like OpenCV, Mahotas uses NumPy arrays to store and process images. We can also combine OpenCV and Mahotas. Let's see an example of this, as follows:
import cv2 import numpy as np import mahotas as mh cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) T_otsu = mh.otsu(frame) output = frame > T_otsu output = output.astype(np.uint8) * 255 cv2.imshow('Output', output) if cv2.waitKey(1) == 27: break cv2.destroyAllWindows() cap.release()
In the preceding program, we converted a live frame into a grayscale version. Then, we applied a Mahotas implementation of Otsu's binarization, which converted the frame from the live video feed into a Boolean binary image. We need to convert this to the np.uint8
type and multiply it by 255
(all of which takes the form of ones in binary 8-bit) so that we can use it with cv2.imshow()
. The output is...