opencv/modules/core
Kavyansh Tyagi 7ca36d9c50
Merge pull request #27408 from KAVYANSHTYAGI:Umat-vector-contructor
Deprecate copyData Parameter in UMat Construction from std::vector and Always Copy Data #27408

Overview

This PR simplifies and modernizes the construction of cv::UMat from std::vector by removing the legacy copyData parameter, always copying the data, and ensuring clearer, safer semantics. This brings UMat in line with current best practices and paves the way for the upcoming OpenCV 5.x series.

What Changed?

1. Header Documentation Update

    Removed confusing or obsolete documentation about copyData and clarified the behavior:

        Old: builds matrix from std::vector with or without copying the data

        New: builds matrix from std::vector. The data is always copied. The copyData parameter is deprecated and will be removed in OpenCV 5.0.

2. Implementation Update

    In UMat::UMat(const std::vector<_Tp>& vec, bool copyData), the copyData parameter:

        Is now ignored and marked as deprecated.

        Marked with CV_UNUSED(copyData) for backward compatibility and to avoid warnings.

    The constructor always copies the data from the input vector, regardless of the value of copyData.

    All branching logic around copyData has been removed. Any code for "not copying" was not implemented and is now dropped.

    This guarantees data safety and predictable behavior.

3. Test Added

    A new test construct_from_vector in test_umat_from_vector.cpp:

        Verifies that UMat copies the vector data, not referencing it.

        Modifies the source vector after construction to confirm that the UMat is unaffected (proving copy, not reference).

        Checks matrix shape, type, and content to ensure correctness.

Why This Change?

1. Safety and Predictability

    Always copying avoids dangling references and hard-to-debug lifetime issues with stack/heap-allocated vectors.

    Removes an undocumented, unimplemented branch (copyData=false).

2. Backward Compatibility

    The constructor signature remains for now, but the copyData parameter is marked as deprecated and ignored.

    Codebases that pass the parameter will still compile and run as before (but always copy).

3. API Clarity and Maintenance

    Documentation now matches the real implementation.

    No misleading expectations about zero-copy.

    Code is cleaner, future-proof, and easier to maintain.

4. Preparation for OpenCV 5.0

    The copyData parameter is deprecated and will be removed in OpenCV 5.x.

    Prepares users and downstream libraries for the planned change.



How This Helps OpenCV Users and Developers

    Guarantees data safety and makes behavior explicit.

    Removes legacy/ambiguous code.

    Provides a clear path to OpenCV 5.x.

    Minimizes future migration pain.

    Ensures all users see the same, reliable behavior (copy semantics).


Refer:#27409
2025-06-09 16:02:38 +03:00
..
3rdparty/SoftFloat Add install component for 3rdparty libraries licenses 2018-03-06 16:32:30 +03:00
cmake/parallel core(parallel): plugins support 2021-02-15 17:07:36 +00:00
doc Merge pull request #26260 from sturkmen72:upd_doc_4_x 2024-10-09 09:09:51 +03:00
include/opencv2 Merge pull request #27408 from KAVYANSHTYAGI:Umat-vector-contructor 2025-06-09 16:02:38 +03:00
misc Merge pull request #25986 from asmorkalov:as/js_for_contrib 2024-08-23 10:49:08 +03:00
perf core(ocl): fix POWN OpenCL implementation 2025-05-01 20:57:23 +00:00
src Fix empty ND-array construction 2025-06-02 10:13:49 +02:00
test Merge pull request #27408 from KAVYANSHTYAGI:Umat-vector-contructor 2025-06-09 16:02:38 +03:00
CMakeLists.txt Merge pull request #26885 from fengyuentau:4x/core/normalize_simd 2025-02-21 13:49:11 +03:00