Internal preprocessing of inputs in Python version of cv2

Not being able to apply two categories, here is the Python version of my question. What does Python OpenCV secretly do with inputs to the functions (GaussianBlur and Laplacian for sure, maybe others)? I suspect upgrading to CV_64F type in GaussianBlur, but I can’t find similar for the Laplacian. And I ask because identical calls in Python and C++ give different outputs.

can you add code to reproduce the problem, please ?
(without, this question is quite useless …)

Not really. It’s a question in hope that someone really knows what’s happening internally in Python OpenCV calls. Not that someone will debug the program. But why not:

#grayPython is an UINT8 result of cv2.GaussianBlur()
laplacedPython = cv2.Laplacian(blurPython, cv2.CV_64F, ksize=5)

//Here is more fiddling to have the same image type before Laplacian.
cv::GaussianBlur(imageCpp, blur0Cpp, 15, 0);
blur0Cpp.convertTo(blurCpp, CV_8U);
cv::Mat laplacedCpp;
cv::Laplacian(blurCpp, laplacedCpp, CV_64F, 5);

Anyway - laplacedPython and laplacedCpp differ. Inputs on the debug are reported as identical.

please read the comment, and then the line of code. where is grayPython? is it relevant? why do you pass CV_64F? do you want the result to be fp64? did you not just say that you’re dissatisfied by data types?

you have an issue and you want us to help, so we expect a “Minimal Reproducible Example” that demonstrates the issue. you can’t just claim something without giving us any way to reproduce those claims.

the most likely explanation for your observation is that you did something wrong. do figure out what you did, and if that’s wrong or not, we need a MRE. no way around it. there’s no point in arguing.

Of course it’s relevant. Everywhere else there is “blur” in this context, this “gray” is just my obvious mistake editing the code.

Because that’s the output type I need.

No. I did not say anything like this.

I am not able to disclose the exact image that I’m observing this behaviour on so it will be not possible to directly reproduce the problem. This is why I’m looking for general help of someone who really knows internal details of Laplacian and GaussianBlur implementation used in Python vs. used in C++ and can tell the differences.

Input data to the functions as “black boxes” are identical, as much as they can be identical between Python and C++. Outputs from the “black box” treated OpenCV functions are different. There is nothing to debug here, I’m looking for someone who knows what’s inside the “black boxes”.

By the way. Can you please unblock my main question in C++ category? This, as I’ve stated at the very beginning, is a secondary one because you do not give possibility to assign two categories to one topic. How else can I efficiently seek for help in a problem that appears between two different languages?

there is no python implementation (or preprocessing), it’s only a thin, generated wrapper around the c++ code. and again, if you get different results, most likely you’re doing something different there, that’s why we ask you for a MRE

then use a different picture that reproduces the issue. the particular picture file is very unlikely to be the cause. more likely it’s the code you use.

we are asking for an MRE because we have reasons. your being difficult makes me suspect you might be a troll.

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