Internal preprocessing of inputs in Python version of cv2

Well, we’ve solved the problem ourselves. For future generations: The so called “thin wrapper” around Python calls in fact does quite a serious operation, it converts the integer image type to floating point (we didn’t bother to verify if 32, or 64) and then, if necessary, back to integer. Which is NOT THE SAME as doing the operation on integers alone. Think about building the gaussian kernel on integers vs. on floating points.
What is more, the floating point → integer conversion on Windows machines used in C++ can have differences from the conversion used in Python, namely some xxx.5 values are rounded DOWN. Be it a matter of better precision, or some FPU failure - doesn’t matter, it just happens.
So, to sum up, if you need to port the OpenCV algorithm between Python and C++ and identical results are important, don’t be surprised and think about the data types because in Python they do things outside your view.

1 Like