mirror of
https://github.com/opencv/opencv.git
synced 2026-11-25 19:07:03 -06:00
code cleanup
This commit is contained in:
@@ -133,10 +133,6 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
|
||||
|
||||
if( type < 0 )
|
||||
{
|
||||
// 64-bit integers used to be force-cast to CV_32S here, which silently
|
||||
// truncated any value outside the int32 range. They now map to
|
||||
// CV_64S/CV_64U in numpyTypeToCvDepth(), so reaching this point means
|
||||
// the dtype genuinely has no cv::Mat equivalent.
|
||||
const std::string dtype_name = getArrayTypeName(oarr);
|
||||
failmsg("%s data type = %s is not supported", info.name,
|
||||
dtype_name.c_str());
|
||||
@@ -304,15 +300,12 @@ bool pyopencv_to(PyObject* o, Mat& m, const ArgInfo& info)
|
||||
template<>
|
||||
PyObject* pyopencv_from(const cv::Mat& m)
|
||||
{
|
||||
// NumPy has no bfloat16 dtype, so CV_16BF is widened to float32 (lossless).
|
||||
// The values must actually be converted, not just relabelled: cvDepthToNumpyType()
|
||||
// reports NPY_FLOAT for CV_16BF, and handing a 2-byte-per-element buffer to the
|
||||
// NumPy allocator under a 4-byte dtype would misinterpret the payload.
|
||||
// NumPy has no bfloat16 dtype: widen CV_16BF to float32 (lossless).
|
||||
if( m.depth() == CV_16BF )
|
||||
{
|
||||
cv::Mat m32f;
|
||||
ERRWRAP2(m.convertTo(m32f, CV_32F));
|
||||
return pyopencv_from(m32f); // m32f is CV_32F, so this recurses at most once
|
||||
return pyopencv_from(m32f);
|
||||
}
|
||||
if( m.empty() )
|
||||
{
|
||||
|
||||
@@ -34,10 +34,8 @@ UMatData* NumpyAllocator::allocate(int dims0, const int* sizes, int type, void*
|
||||
|
||||
int depth = CV_MAT_DEPTH(type);
|
||||
int cn = CV_MAT_CN(type);
|
||||
// cvDepthToNumpyType() widens CV_16BF to NPY_FLOAT for export, which is only
|
||||
// valid when the values are converted (see pyopencv_from). Backing a CV_16BF
|
||||
// Mat with a float32 buffer here would instead pair a 2-byte element step with
|
||||
// 4-byte NumPy strides and silently corrupt the data, so refuse it outright.
|
||||
// Backing a CV_16BF Mat with the float32 buffer cvDepthToNumpyType() asks for
|
||||
// would pair a 2-byte element step with 4-byte NumPy strides and corrupt the data.
|
||||
if( depth == CV_16BF )
|
||||
CV_Error(Error::StsNotImplemented,
|
||||
"CV_16BF (bfloat16) arrays cannot be allocated through the NumPy allocator: "
|
||||
|
||||
@@ -21,14 +21,10 @@ int cvDepthToNumpyType(int depth)
|
||||
case CV_32F: return NPY_FLOAT;
|
||||
case CV_64F: return NPY_DOUBLE;
|
||||
case CV_16F: return NPY_HALF;
|
||||
// NumPy has no bfloat16 dtype, so CV_16BF is exported as float32 (a lossless
|
||||
// widening). pyopencv_from() performs the value conversion; without it the
|
||||
// 2-byte payload would be reinterpreted as 4-byte elements.
|
||||
// NumPy has no bfloat16 dtype; pyopencv_from() converts the values to float32.
|
||||
case CV_16BF: return NPY_FLOAT;
|
||||
case CV_Bool: return NPY_BOOL;
|
||||
default:
|
||||
// Deliberately an error rather than a fallback: silently mapping an
|
||||
// unknown depth to some default dtype mislabels the payload.
|
||||
CV_Error(cv::Error::StsNotImplemented,
|
||||
cv::format("Mat depth %d has no corresponding NumPy dtype", depth));
|
||||
}
|
||||
@@ -36,7 +32,7 @@ int cvDepthToNumpyType(int depth)
|
||||
|
||||
int numpyTypeToCvDepth(int typenum)
|
||||
{
|
||||
// Only canonical NPY_* values may appear as case labels: the fixed-width
|
||||
// Only canonical NPY_* values may be used as case labels: the fixed-width
|
||||
// aliases (NPY_INT32, NPY_INT64, ...) expand to these and would collide.
|
||||
switch (typenum)
|
||||
{
|
||||
@@ -48,8 +44,7 @@ int numpyTypeToCvDepth(int typenum)
|
||||
case NPY_INT: return CV_32S;
|
||||
case NPY_ULONGLONG: return CV_64U;
|
||||
case NPY_LONGLONG: return CV_64S;
|
||||
// 'long' is 64-bit on LP64 (Linux/macOS) but 32-bit on LLP64 (Windows),
|
||||
// so this must be decided by size rather than by name.
|
||||
// 'long' is 64-bit on LP64 but 32-bit on LLP64, so decide by size, not by name.
|
||||
case NPY_ULONG: return NPY_SIZEOF_LONG == 8 ? CV_64U : CV_32U;
|
||||
case NPY_LONG: return NPY_SIZEOF_LONG == 8 ? CV_64S : CV_32S;
|
||||
case NPY_HALF: return CV_16F;
|
||||
|
||||
Reference in New Issue
Block a user