Ask Your Question

Revision history [back]

Corrupt/truncated output of `cv2.bitwise_and` -- fixed in OpenCV 2.4?

Intermittently, the output of "cv2.bitwise_and" (called from the python bindings) is wrong. It looks as if somehow the ANDing process was interrupted before it completed:

source_masked.png

source_masked.png

source.png:

source.png

mask.png:

mask.png

I can reproduce this almost every time on OpenCV 2.3, but not on 2.4 after thousands of runs. I have seen the same problem in the output of "cv2.matchTemplate" (square or triangular regions of output are missing, set to white or black instead). I have reproduced the problem on two different PCs.

My question is: Does anyone know of a fix in 2.4 that would have fixed this issue? I'm worried that maybe it's a race condition, and when I upgraded I changed the conditions slightly such that the race condition is no longer triggered, but is still latently present in 2.4.

I've searched the ticket tracker and the mailing list and the answers site, but didn't find anything relevant. There are several fixed issues relating to matchTemplate but all of them are GPU-related and I'm not using the GPU (as far as I know). I also searched the commit log and saw a commit that fixes "heap corruption in OutputArray::create" but I don't know if it is relevant.

On OpenCV 2.3, I can only reproduce this problem within my application that is taking frames from GStreamer. If I rerun the OpenCV processing on the same frames outside of my application, I can't reproduce the problem even after tens of thousands of runs; so I can't provide a minimal test case. And I can only reproduce it using the python bindings, not with C code.

My code is essentially this:

cv2.imwrite(
    "source.png",
    my_own_function_to_get_image_from_gstreamer())

source_masked = cv2.bitwise_and(
    cv2.imread("source.png"),
    numpy.array([255]),
    mask=cv2.imread("mask.png"))

cv2.imwrite(
    "source_masked.png",
    source_masked)