1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge pull request #27779 from dkurt:dlpack_5.x_types

Add 5.x types for DLPack. Keep uint32/int64/uint64 data type for conversion to Numpy. More types support for GpuMat::convertTo #27779

### Pull Request Readiness Checklist

**Merge with contrib**: https://github.com/opencv/opencv_contrib/pull/4000

related: https://github.com/opencv/opencv/pull/27581

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Dmitry Kurtaev
2025-09-25 10:14:46 +03:00
committed by GitHub
parent 9d232fe7f2
commit 4159c0ad98
7 changed files with 144 additions and 24 deletions
+15
View File
@@ -546,11 +546,26 @@ static bool init_body(PyObject * m)
PUBLISH(CV_16SC2);
PUBLISH(CV_16SC3);
PUBLISH(CV_16SC4);
PUBLISH(CV_32U);
PUBLISH(CV_32UC1);
PUBLISH(CV_32UC2);
PUBLISH(CV_32UC3);
PUBLISH(CV_32UC4);
PUBLISH(CV_32S);
PUBLISH(CV_32SC1);
PUBLISH(CV_32SC2);
PUBLISH(CV_32SC3);
PUBLISH(CV_32SC4);
PUBLISH(CV_64U);
PUBLISH(CV_64UC1);
PUBLISH(CV_64UC2);
PUBLISH(CV_64UC3);
PUBLISH(CV_64UC4);
PUBLISH(CV_64S);
PUBLISH(CV_64SC1);
PUBLISH(CV_64SC2);
PUBLISH(CV_64SC3);
PUBLISH(CV_64SC4);
PUBLISH(CV_32F);
PUBLISH(CV_32FC1);
PUBLISH(CV_32FC2);
+3 -2
View File
@@ -36,8 +36,9 @@ UMatData* NumpyAllocator::allocate(int dims0, const int* sizes, int type, void*
const int f = (int)(sizeof(size_t)/8);
int typenum = depth == CV_8U ? NPY_UBYTE : depth == CV_8S ? NPY_BYTE :
depth == CV_16U ? NPY_USHORT : depth == CV_16S ? NPY_SHORT :
depth == CV_32S ? NPY_INT : depth == CV_32F ? NPY_FLOAT :
depth == CV_64F ? NPY_DOUBLE : depth == CV_16F ? NPY_HALF : depth == CV_Bool ? NPY_BOOL : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
depth == CV_32U ? NPY_UINT32 : depth == CV_32S ? NPY_INT32 : depth == CV_64S ? NPY_INT64 :
depth == CV_32F ? NPY_FLOAT : depth == CV_64F ? NPY_DOUBLE : depth == CV_16F ? NPY_HALF :
depth == CV_Bool ? NPY_BOOL : f*NPY_ULONGLONG + (f^1)*NPY_UINT;
int i, dims = dims0;
cv::AutoBuffer<npy_intp> _sizes(dims + 1);
for( i = 0; i < dims; i++ )
+13 -1
View File
@@ -146,13 +146,25 @@ class cuda_test(NewOpenCVTests):
self.assertEqual(True, hasattr(cv.cuda, 'nonLocalMeans'))
def test_dlpack_GpuMat(self):
for dtype in [np.int8, np.uint8, np.int16, np.uint16, np.float16, np.int32, np.float32, np.float64]:
for dtype in [np.int8, np.uint8, np.int16, np.uint16, np.float16, np.int32, np.float32, np.float64, np.int64, np.uint32, np.uint64, np.bool_]:
for channels in [2, 3, 5]:
ref = (np.random.random((64, 128, channels)) * 255).astype(dtype)
src = cv.cuda_GpuMat()
src.upload(ref)
# workaround int64/uint64 conversion to int32/uint32
if dtype == np.int64:
print("skip because of https://github.com/opencv/opencv/issues/27671")
continue
src = src.convertTo(cv.CV_64S)
elif dtype == np.uint64:
print("skip because of https://github.com/opencv/opencv/issues/27671")
continue
src = src.convertTo(cv.CV_64U)
dst = cv.cuda_GpuMat.from_dlpack(src)
test = dst.download()
self.assertEqual(ref.dtype, test.dtype)
equal = np.array_equal(ref, test)
if not equal:
print(f"Failed test with dtype {dtype} and {channels} channels")