Ask Your Question

Revision history [back]

This was an invalid use of the mask parameter. The mask parameter to bitwise_and means that the operation only changes those destination array pixels p for which mask(p) != 0. Since we're not specifying dst (destination), bitwise_and allocates a new output array, but doesn't initialize it properly, so anything in the output image in the area corresponding to the 0 pixels in the mask contain garbage, rather than 0. See http://code.opencv.org/issues/1748#note-2

The code above should be written as:

source_masked = cv2.bitwise_and(
    cv2.imread("source.png"),
    cv2.imread("mask.png"))

Presumably the reason I didn't see this behaviour in OpenCV 2.4.5 is because of http://code.opencv.org/issues/1286