-
-
Notifications
You must be signed in to change notification settings - Fork 56.4k
Description
Module: core
Source file: modules/core/src/convert.dispatch.cpp
Function: void Mat::convertTo(OutputArray dst, int type_, double alpha, double beta) const
Description
When calling Mat::convertTo on an empty source matrix, the implementation early-exits with:
if (empty())
{
dst.release();
return;
}This results in the output matrix dst being released (emptied) without having its type set to the requested target type (type_). This behavior leads to inconsistent or unexpected downstream behavior, as the resulting empty dst has no type information.
Expected behavior
Even when the input matrix is empty, convertTo should produce an empty output matrix with the requested type (depth and number of channels), preserving consistent metadata semantics.
If the output matrix (dst) is already empty and has the correct type, its existing state - such as the data pointer or ROI linkage - should be preserved to maintain data continuity. This ensures that convertTo does not unnecessarily reallocate or break shallow copies, regions of interest, or external memory bindings.
Suggested fix
Instead of releasing dst on empty source, set dst to an empty matrix with zero size but the requested type_. If dst is already empty and has the requested type_, keep it's state.
For example:
if (empty()) {
if (dst.empty() && dst.type() == type_) {
return;
}
dst.create(0, 0, type_);
return;
}