0
0
mirror of https://github.com/opencv/opencv.git synced 2026-01-18 17:21:42 +01:00

Merge pull request #27972 from asmorkalov:as/dst_mat_type

Force empty output type where it's defined in API #27972

The PR replaces:
- https://github.com/opencv/opencv/pull/27936
- https://github.com/opencv/opencv/pull/21059

Empty matrix has undefined type, so user code should not relay on the output type, if it's empty. The PR introduces some exceptions:
- copyTo documentation defines, that the method re-create output buffer and set it's type.
- convertTo has output type as parameter and output type is defined and expected.
This commit is contained in:
Alexander Smorkalov
2025-11-10 08:00:17 +03:00
committed by GitHub
parent 386be68f93
commit 64bbaa9f41
6 changed files with 33 additions and 0 deletions

View File

@@ -1113,6 +1113,10 @@ void cv::add( InputArray src1, InputArray src2, OutputArray dst,
if (src1.empty() && src2.empty())
{
dst.release();
if (dtype >= 0)
{
dst.create(0, 0, dtype);
}
return;
}
@@ -1140,6 +1144,10 @@ void cv::subtract( InputArray _src1, InputArray _src2, OutputArray _dst,
if (_src1.empty() && _src2.empty())
{
_dst.release();
if (dtype >= 0)
{
_dst.create(0, 0, dtype);
}
return;
}
@@ -1345,6 +1353,10 @@ void cv::addWeighted( InputArray src1, double alpha, InputArray src2,
if (src1.empty() && src2.empty())
{
dst.release();
if (dtype >= 0)
{
dst.create(0, 0, dtype);
}
return;
}

View File

@@ -252,6 +252,7 @@ void Mat::convertTo(OutputArray dst, int type_, double alpha, double beta) const
if (empty())
{
dst.release();
dst.create(size(), type_ >= 0 ? type_ : type());
return;
}
@@ -318,6 +319,7 @@ void UMat::convertTo(OutputArray dst, int type_, double alpha, double beta) cons
if (empty())
{
dst.release();
dst.create(size(), type_ >= 0 ? type_ : type());
return;
}

View File

@@ -447,6 +447,7 @@ void Mat::copyTo( OutputArray _dst ) const
if( empty() )
{
_dst.release();
_dst.create(size(), type());
return;
}

View File

@@ -1153,6 +1153,7 @@ void UMat::copyTo(OutputArray _dst) const
if( empty() )
{
_dst.release();
_dst.create(size(), type());
return;
}

View File

@@ -1385,6 +1385,15 @@ TEST(Core_Mat, push_back)
}
}
TEST(Core_Mat, copyToConvertTo_Empty)
{
cv::Mat A(0, 0, CV_16SC2), B, C;
A.copyTo(B);
ASSERT_EQ(A.type(), B.type());
A.convertTo(C, CV_32SC2);
ASSERT_EQ(C.type(), CV_32SC2);
}
TEST(Core_Mat, copyNx1ToVector)
{
cv::Mat_<uchar> src(5, 1);

View File

@@ -1199,6 +1199,14 @@ TEST(UMat, async_cleanup_without_call_chain_warning)
}
}
TEST(UMat, copyToConvertTo_Empty)
{
cv::UMat A(0, 0, CV_16SC2), B, C;
A.copyTo(B);
ASSERT_EQ(A.type(), B.type());
A.convertTo(C, CV_32SC2);
ASSERT_EQ(C.type(), CV_32SC2);
}
///////////// oclCleanupCallback threadsafe check (#5062) /////////////////////