Skip to content

Python Scalar is different with C++ #23764

@zihaomu

Description

@zihaomu

Hi,
I suspect the following code has different behavior in C++ and Python.

Scalar a = 1.0;

For Python, the Scalar a = 1.0 code will only replace the first 0.0 to 1.0.
Do something like a[0] = 1.0.
For C++, it will do the Scalar a = Scalar(1.0).

Original link: https://forum.opencv.org/t/image2blobparams-c-and-python/13437?u=zihao_mu
My code in python :

paramTF2cedn = cv.dnn.Image2BlobParams()
paramTF2cedn.datalayout = cv.dnn.DNN_LAYOUT_NHWC;
paramTF2cedn.ddepth = cv.CV_32F;
paramTF2cedn.mean = (128, 128, 128)
paramTF2cedn.scalefactor = 1 / 128.
paramTF2cedn.size = (224, 224)
paramTF2cedn.swapRB = False;
paramTF2cedn.paddingmode = cv.dnn.DNN_PMODE_NULL
print("paramTF2cedn.scalefactor = ", paramTF2cedn.scalefactor)

result is
paramTF2cedn.scalefactor = (0.0078125, 1.0, 1.0, 1.0)

in C++

Image2BlobParams paramTF2cedn;
paramTF2cedn.datalayout = DNN_LAYOUT_NHWC;
paramTF2cedn.ddepth = CV_32F;
paramTF2cedn.mean = (128, 128, 128);
paramTF2cedn.scalefactor = 1 / 128.;
paramTF2cedn.size = Size(224, 224);
paramTF2cedn.swapRB = false;
paramTF2cedn.paddingmode = DNN_PMODE_NULL;
cout << "paramTF2cedn.scalefactor = " << paramTF2cedn.scalefactor << endl;

result is
paramTF2cedn.scalefactor = [0.0078125, 0, 0, 0]

Detailed description

It is about opencv-python bing.

Steps to reproduce

install lastest opencv-python

pip install opencv-python-rolling==4.7.0.20230603.

param = cv.dnn.Image2BlobParams()
param.mean = np.array([0.2, 0.3, 0.4]) # [0.2, 0.3, 0.4, 0.0]
# then redefine it.
param.mean = 1.0 # it is [1.0, 0.3, 0.4, 0.0]  <<--

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions