1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-28 06:43:01 +04:00

Merge pull request #28968 from Prasadayus:Identity_function_fix_for_0D

Fixes the issue for Identity functions for 0D #28968

Closes https://github.com/opencv/opencv/issues/25763

### Pull Request Readiness Checklist

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:
Prasad Ayush Kumar
2026-05-12 17:13:46 +05:30
committed by GitHub
parent 2ad46b860b
commit 223b110c30
5 changed files with 47 additions and 26 deletions
+18 -18
View File
@@ -129,18 +129,7 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
bool needcopy = false, needcast = false;
int typenum = PyArray_TYPE(oarr), new_typenum = typenum;
int type = typenum == NPY_UBYTE ? CV_8U :
typenum == NPY_BYTE ? CV_8S :
typenum == NPY_USHORT ? CV_16U :
typenum == NPY_SHORT ? CV_16S :
typenum == NPY_INT ? CV_32S :
typenum == NPY_UINT32 ? CV_32U :
typenum == NPY_INT32 ? CV_32S :
typenum == NPY_HALF ? CV_16F :
typenum == NPY_FLOAT ? CV_32F :
typenum == NPY_DOUBLE ? CV_64F :
typenum == NPY_BOOL ? CV_Bool :
-1;
int type = numpyTypeToCvDepth(typenum);
if( type < 0 )
{
@@ -296,12 +285,14 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
return filled;
}
// handle degenerate case
// FIXIT: Don't force 1D for Scalars
if( ndims == 0) {
size[ndims] = 1;
step[ndims] = elemsize;
ndims++;
// 0D (scalar) tensor: allocate a proper scalar Mat and copy the value.
if (ndims == 0)
{
m.fit(0, nullptr, type);
memcpy(m.data, PyArray_DATA(oarr), CV_ELEM_SIZE(type));
if (needcopy)
Py_DECREF(o);
return true;
}
#if 1
@@ -326,6 +317,15 @@ PyObject* pyopencv_from(const cv::Mat& m)
{
if( !m.data )
Py_RETURN_NONE;
// 0D (scalar) Mat: return a true 0D numpy array.
if( m.dims == 0 )
{
int typenum = cvDepthToNumpyType(CV_MAT_DEPTH(m.type()));
PyObject* o = PyArray_SimpleNew(0, nullptr, typenum);
if( o )
memcpy(PyArray_DATA((PyArrayObject*)o), m.data, CV_ELEM_SIZE(m.type()));
return o;
}
cv::Mat temp, *p = (cv::Mat*)&m;
if(!p->u || p->allocator != &GetNumpyAllocator())
{