[Python] The result obtained from the OpenCV-Python cv.add() function differs from the...

Discussão em 'Python' iniciado por Stack, Agosto 4, 2025.

  1. Stack

    Stack Membro Participativo

    During my learning process of OpenCV-Python, I encountered an issue. My environment uses Python version 3.13, NumPy version 2.2.6, and OpenCV-Python version 4.12.

    In the "Arithmetic Operations on Images" section of the official documentation, it mentions the difference between OpenCV addition and NumPy addition: OpenCV addition is a saturated operation while NumPy addition is a modulo operation. Official documentation page

    However, the actual results I obtained during computation differ from the values provided in the documentation.

    My code:

    import cv2 as cv
    import numpy as np

    x = np.uint8([250])
    y = np.uint8([10])

    print(f"x+y = {x+y}")
    print(f"cv.add(x,y) = {cv.add(x,y)}")


    Official documentation result:

    [4]
    [[255]]


    My result:

    [4]
    [[260.]
    [0.]
    [0.]
    [0.]]


    I checked cv.add(x,y).dtype and found it returns float64 instead of uint8.

    After attempting data type conversion and retrieving the first value with cv.add(x,y).astype(np.uint8)[0], the result is [4], which matches the NumPy addition result. How can I obtain the correct value from the cv.add() algorithm?

    Continue reading...

Compartilhe esta Página