Skip to content

Commit

Permalink
Merge pull request opencv#4159 from alalek:vector_processing
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Jul 6, 2015
2 parents 8bede85 + 885114e commit 40fcc9d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/core/include/opencv2/core/mat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class CV_EXPORTS _InputArray
bool isMatVector() const;
bool isUMatVector() const;
bool isMatx() const;
bool isVector() const;

~_InputArray();

Expand Down
1 change: 1 addition & 0 deletions modules/core/include/opencv2/core/mat.inl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ inline bool _InputArray::isUMat() const { return kind() == _InputArray::UMAT; }
inline bool _InputArray::isMatVector() const { return kind() == _InputArray::STD_VECTOR_MAT; }
inline bool _InputArray::isUMatVector() const { return kind() == _InputArray::STD_VECTOR_UMAT; }
inline bool _InputArray::isMatx() const { return kind() == _InputArray::MATX; }
inline bool _InputArray::isVector() const { return kind() == _InputArray::STD_VECTOR || kind() == _InputArray::STD_BOOL_VECTOR; }

////////////////////////////////////////////////////////////////////////////////////////

Expand Down
5 changes: 5 additions & 0 deletions modules/core/src/copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ void Mat::copyTo( OutputArray _dst ) const

if( rows > 0 && cols > 0 )
{
// For some cases (with vector) dst.size != src.size, so force to column-based form
// It prevents memory corruption in case of column-based src
if (_dst.isVector())
dst = dst.reshape(0, (int)dst.total());

const uchar* sptr = data;
uchar* dptr = dst.data;

Expand Down
82 changes: 82 additions & 0 deletions modules/core/test/test_mat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1318,3 +1318,85 @@ TEST(Core_SparseMat, footprint)
ASSERT_LE((int)m.hdr->nodeSize, 32);
ASSERT_LE(dataSize1, threshold);
}


// Can't fix without duty hacks or broken user code (PR #4159)
TEST(Core_Mat_vector, DISABLED_OutputArray_create_getMat)
{
cv::Mat_<uchar> src_base(5, 1);
std::vector<uchar> dst8;

src_base << 1, 2, 3, 4, 5;

Mat src(src_base);
OutputArray _dst(dst8);
{
_dst.create(src.rows, src.cols, src.type());
Mat dst = _dst.getMat();
EXPECT_EQ(src.dims, dst.dims);
EXPECT_EQ(src.cols, dst.cols);
EXPECT_EQ(src.rows, dst.rows);
}
}

TEST(Core_Mat_vector, copyTo_roi_column)
{
cv::Mat_<uchar> src_base(5, 2);
std::vector<uchar> dst1;

src_base << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;

Mat src_full(src_base);
Mat src(src_full.col(0));
#if 0 // Can't fix without duty hacks or broken user code (PR #4159)
OutputArray _dst(dst1);
{
_dst.create(src.rows, src.cols, src.type());
Mat dst = _dst.getMat();
EXPECT_EQ(src.dims, dst.dims);
EXPECT_EQ(src.cols, dst.cols);
EXPECT_EQ(src.rows, dst.rows);
}
#endif

std::vector<uchar> dst2;
src.copyTo(dst2);
std::cout << "src = " << src << std::endl;
std::cout << "dst = " << Mat(dst2) << std::endl;
EXPECT_EQ((size_t)5, dst2.size());
EXPECT_EQ(1, (int)dst2[0]);
EXPECT_EQ(3, (int)dst2[1]);
EXPECT_EQ(5, (int)dst2[2]);
EXPECT_EQ(7, (int)dst2[3]);
EXPECT_EQ(9, (int)dst2[4]);
}

TEST(Core_Mat_vector, copyTo_roi_row)
{
cv::Mat_<uchar> src_base(2, 5);
std::vector<uchar> dst1;

src_base << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;

Mat src_full(src_base);
Mat src(src_full.row(0));
OutputArray _dst(dst1);
{
_dst.create(src.rows, src.cols, src.type());
Mat dst = _dst.getMat();
EXPECT_EQ(src.dims, dst.dims);
EXPECT_EQ(src.cols, dst.cols);
EXPECT_EQ(src.rows, dst.rows);
}

std::vector<uchar> dst2;
src.copyTo(dst2);
std::cout << "src = " << src << std::endl;
std::cout << "dst = " << Mat(dst2) << std::endl;
EXPECT_EQ((size_t)5, dst2.size());
EXPECT_EQ(1, (int)dst2[0]);
EXPECT_EQ(2, (int)dst2[1]);
EXPECT_EQ(3, (int)dst2[2]);
EXPECT_EQ(4, (int)dst2[3]);
EXPECT_EQ(5, (int)dst2[4]);
}

0 comments on commit 40fcc9d

Please sign in to comment.