# Get same results in C++ and python code

**URL:** <https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656>\
**Category:** C++\
**Tags:** python\
**Created:** [June 24, 2023, 1:35pm UTC](https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656 "2023-06-24T13:35:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [June 24, 2023, 1:35pm UTC](https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656/1 "2023-06-24T13:35:27Z")

</div>

Hi,

I translate python code in C++. I check result in python and c++ and sometimes it’s difficult to get same result. example python code

```auto
import numpy as np
import cv2 as cv

xf = np.array([38.4, 40.7])
xi = xf.astype(np.int32)
print(xi)

```

result is

[38 40]

Now in C++

```auto
    Mat xf = (Mat_<float>(2, 1) << 38.4, 40.7);
    Mat xi;
    xf.convertTo(xi, CV_32S);
    cout << "Mat float : " << xf << endl;
    cout << "Mat int : " << xi << endl;

```

and result is

Mat float : [38.400002;  
40.700001]  
Mat int : [38;  
41]

Ok that’s just a difference of 1. But if data is a top point in a rectangle and use it to extract an image for a deep learning model, inference results are really different

This post is only a warning

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 24, 2023, 1:46pm UTC](https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656/2 "2023-06-24T13:46:43Z")

</div>

> [@laurent.berger](#):
>
> `xf.astype(np.int32)`

that truncates. it’s literally a double to int32 cast (double because python float literals are double and np.array respects that). such a cast, by whatever requirements (CPU architecture, whatever, I don’t care), is a truncation. numpy is striving to be very predictable with these things.

> [@laurent.berger](#):
>
> ```auto
> xf.convertTo(xi, CV_32S);
> 
> ```

that does some math in the background. probably even saturating math. OpenCV does “stuff”.

always think about your numerical conversions, decide what behavior you want, and make sure it’s done explicitly.

> [@laurent.berger](#):
>
> This post is only a warning

that sounds ominous

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [June 24, 2023, 2:09pm UTC](https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656/3 "2023-06-24T14:09:11Z")

</div>

> [@crackwitz](#):
>
> that sounds ominous

It is like

```auto
cap.open(samples::findFile("lena.jpg"));
cap >> img;

```

and  
` img=imread(samples::findFile("lena.jpg"));`

img are not equal (on windows)

and again for inference results will not be equal with same data file

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 24, 2023, 2:25pm UTC](https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656/4 "2023-06-24T14:25:29Z")

</div>

> [@laurent.berger](#):
>
> ```auto
> cap.open(samples::findFile("lena.jpg"));
> cap >> img;
> 
> ```

excuse me, why do you try to treat a jpeg image as if it were a video file?

I know you to be generally competent. lapses in mental capacity and communication abilities could indicate a stroke. I’m being serious. please make sure you’re okay.

---

<div class="post-metadata">

**Author:** ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)\
**Post date:** [June 24, 2023, 2:35pm UTC](https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656/5 "2023-06-24T14:35:17Z")

</div>

> [@crackwitz](#):
>
> I’m being serious. please make sure you’re okay.

May be it is a new pandemic

> <https://github.com/opencv/opencv/blob/4.x/samples/dnn/object_detection.cpp#L25C1-L26C1>

> <https://github.com/opencv/opencv/blob/4.x/samples/dnn/object_detection.cpp#L172-L193>

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 24, 2023, 6:04pm UTC](https://forum.opencv.org/t/get-same-results-in-c-and-python-code/13656/6 "2023-06-24T18:04:09Z")

</div>

I am very surprised that VideoCapture tolerates this. the `CV_IMAGES` backend seems to be responsible for this succeeding.

either way, I would not expect results to be _exactly_ the same for `VideoCapture()` and `imread()`
