From 726a0959dfdf8b328c9763c4978f6781cf858029 Mon Sep 17 00:00:00 2001 From: NIKHIL Date: Thu, 13 Aug 2026 20:27:50 +0530 Subject: [PATCH] python: validate high-channel 3D ndarrays on 4.x --- modules/python/src2/cv2_convert.cpp | 2 +- modules/python/test/test_mat.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/modules/python/src2/cv2_convert.cpp b/modules/python/src2/cv2_convert.cpp index edcef0bb59..65a8858707 100644 --- a/modules/python/src2/cv2_convert.cpp +++ b/modules/python/src2/cv2_convert.cpp @@ -173,7 +173,7 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info) CV_LOG_DEBUG(NULL, "Incoming ndarray '" << info.name << "': ndims=" << ndims << " _sizes=" << pycv_dumpArray(_sizes, ndims) << " _strides=" << pycv_dumpArray(_strides, ndims)); - bool ismultichannel = ndims == 3 && _sizes[2] <= CV_CN_MAX && !info.nd_mat; + bool ismultichannel = ndims == 3 && !info.nd_mat; if (pyopencv_Mat_TypePtr && PyObject_TypeCheck(o, pyopencv_Mat_TypePtr)) { bool wrapChannels = false; diff --git a/modules/python/test/test_mat.py b/modules/python/test/test_mat.py index 73efdf3fb8..3329fd5ec2 100644 --- a/modules/python/test/test_mat.py +++ b/modules/python/test/test_mat.py @@ -89,6 +89,19 @@ try: print(res1) + def test_ndarray_channel_limit_is_reported(self): + map_x, map_y = np.meshgrid( + np.arange(8, dtype=np.float32), + np.arange(8, dtype=np.float32), + ) + data = np.broadcast_to( + np.arange(513, dtype=np.uint8), + (8, 8, 513), + ).copy() + + with self.assertRaisesRegex(cv.error, "unable to wrap channels"): + cv.remap(data, map_x, map_y, cv.INTER_NEAREST) + def test_mat_wrap_channels_zero(self): # Passing a 0-channel array must raise cv.error, not segfault. data = np.zeros((100, 100, 0), dtype=np.uint8)