Files
opencv-MIRROR/modules/core
pranjal9091 2868492769 core: forbid trace and setIdentity for non-2D matrices (#30785)
* core: support 1D Mat in trace and setIdentity for non-float types

In OpenCV 5.x, 1D Mat support was introduced and cv::trace and
cv::setIdentity were updated with CV_Assert(m.dims <= 2). However,
both functions previously relied on m.diag() as the general fallback
path for non-float types (e.g. integer and multi-channel types).
Since Mat::diag strictly asserts dims == 2, calling cv::trace or
cv::setIdentity on a 1D non-float Mat resulted in an assertion failure
in Mat::diag.

Fix this by using m.col(0) when m.dims <= 1 (mirroring the behavior of
1xN 2D matrices), and gracefully handling empty matrices. Also add unit
tests covering 1D Mat across integral, floating point, and multi-channel
types for both cv::trace and cv::setIdentity.

* core: handle 0-D Mat in trace and setIdentity for non-float types

Properly handle 0-D matrices (scalars) in cv::trace using cv::sum(m)
and in cv::setIdentity using m = s without invoking m.col(0).
Also add unit tests for 0-D Mat covering integral, floating-point,
and multi-channel types.

* core: forbid trace and setIdentity for non-2D matrices
2026-11-26 18:00:46 +05:30
..