1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #28242 from vpisarev:fix_input_array_std_vector

modified Input/OutputArray methods to handle 'std::vector<T>' or 'std::vector<std::vector<T>>' properly #28242

This is port of #26408 with some further improvements (all switch-by-vector-type statements are consolidated in a single macro)

### 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
- [ ] The PR is proposed to the proper branch
- [ ] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Vadim Pisarevsky
2025-12-23 14:25:07 +03:00
committed by GitHub
parent a27397a654
commit eaccbe24b2
3 changed files with 235 additions and 175 deletions
+60
View File
@@ -2778,4 +2778,64 @@ TEST(Mat, copyAt_regression27298)
}
}
template<typename _Tp, int cn> static void make_vector(std::vector<cv::Vec<_Tp, cn> >& v, int n)
{
v.clear();
v.resize(n);
_Tp* data = &v[0][0];
for (int i = 0; i < n; i++) {
for (int j = 0; j < cn; j++) {
int k = j % 4;
int val = (k == 0 ? 1 : k == 1 ? -1 : k == 2 ? (i+1) : -(i+1))*(i+1);
data[i*cn + j] = (_Tp)val;
}
}
}
TEST(Core_InputOutputArray, std_vector_vector)
{
std::vector<Vec3s> vv0_s, vv1_s;
std::vector<std::vector<short> > cn_s;
make_vector(vv0_s, 100);
split(vv0_s, cn_s);
merge(cn_s, vv1_s);
double err0 = cvtest::norm(vv0_s, vv1_s, NORM_INF);
EXPECT_EQ(0, err0);
_InputArray iarr_s(cn_s);
_OutputArray oarr_s(cn_s);
EXPECT_EQ(3u, iarr_s.total(-1));
size_t newsize_s = vv0_s.size()*2;
oarr_s.create(Size((int)newsize_s, 1), CV_16S, 2);
EXPECT_EQ(newsize_s, cn_s[2].size());
cn_s[1].clear();
EXPECT_EQ(true, oarr_s.empty(1));
std::vector<Vec4d> vv0_d, vv1_d;
std::vector<std::vector<double> > cn_d;
make_vector(vv0_d, 1000);
split(vv0_d, cn_d);
merge(cn_d, vv1_d);
double err1 = cvtest::norm(vv0_d, vv1_d, NORM_INF);
EXPECT_EQ(0., err1);
_InputArray iarr_d(cn_d);
_OutputArray oarr_d(cn_d);
EXPECT_EQ(4u, iarr_d.total(-1));
size_t newsize_d = vv0_d.size()*3;
oarr_d.create(Size((int)newsize_d, 1), CV_64F, 3);
EXPECT_EQ(newsize_d, cn_d[3].size());
cn_d[1].clear();
EXPECT_EQ(true, oarr_d.empty(1));
Mat m2 = oarr_d.getMat(2);
double err2 = cvtest::norm(m2, Mat(cn_d[2]).t(), NORM_INF);
EXPECT_EQ(m2.ptr<double>(), &cn_d[2][0]);
EXPECT_EQ(0., err2);
}
}} // namespace