Skip to content

Commit

Permalink
Merge pull request opencv#8244 from sovrasov:adjust_roi_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Feb 24, 2017
2 parents c624d82 + 14451f3 commit eee638f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
9 changes: 7 additions & 2 deletions modules/core/src/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -997,8 +997,13 @@ Mat& Mat::adjustROI( int dtop, int dbottom, int dleft, int dright )
Size wholeSize; Point ofs;
size_t esz = elemSize();
locateROI( wholeSize, ofs );
int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
int row1 = std::min(std::max(ofs.y - dtop, 0), wholeSize.height), row2 = std::max(0, std::min(ofs.y + rows + dbottom, wholeSize.height));
int col1 = std::min(std::max(ofs.x - dleft, 0), wholeSize.width), col2 = std::max(0, std::min(ofs.x + cols + dright, wholeSize.width));
if(row1 > row2)
std::swap(row1, row2);
if(col1 > col2)
std::swap(col1, col2);

data += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;
rows = row2 - row1; cols = col2 - col1;
size.p[0] = rows; size.p[1] = cols;
Expand Down
9 changes: 7 additions & 2 deletions modules/core/src/umatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,13 @@ UMat& UMat::adjustROI( int dtop, int dbottom, int dleft, int dright )
Size wholeSize; Point ofs;
size_t esz = elemSize();
locateROI( wholeSize, ofs );
int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
int row1 = std::min(std::max(ofs.y - dtop, 0), wholeSize.height), row2 = std::max(0, std::min(ofs.y + rows + dbottom, wholeSize.height));
int col1 = std::min(std::max(ofs.x - dleft, 0), wholeSize.width), col2 = std::max(0, std::min(ofs.x + cols + dright, wholeSize.width));
if(row1 > row2)
std::swap(row1, row2);
if(col1 > col2)
std::swap(col1, col2);

offset += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;
rows = row2 - row1; cols = col2 - col1;
size.p[0] = rows; size.p[1] = cols;
Expand Down
14 changes: 14 additions & 0 deletions modules/core/test/test_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1240,3 +1240,17 @@ class CV_SparseMatTest : public cvtest::BaseTest
};

TEST(Core_SparseMat, iterations) { CV_SparseMatTest test; test.safe_run(); }

TEST(MatTestRoi, adjustRoiOverflow)
{
Mat m(15, 10, CV_32S);
Mat roi(m, cv::Range(2, 10), cv::Range(3,6));
int rowsInROI = roi.rows;
roi.adjustROI(1, 0, 0, 0);

ASSERT_EQ(roi.rows, rowsInROI + 1);

roi.adjustROI(-m.rows, -m.rows, 0, 0);

ASSERT_EQ(roi.rows, m.rows);
}
14 changes: 14 additions & 0 deletions modules/core/test/test_umat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,20 @@ TEST_P(UMatTestRoi, adjustRoi)

INSTANTIATE_TEST_CASE_P(UMat, UMatTestRoi, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, UMAT_TEST_SIZES ));

TEST(UMatTestRoi, adjustRoiOverflow)
{
UMat m(15, 10, CV_32S);
UMat roi(m, cv::Range(2, 10), cv::Range(3,6));
int rowsInROI = roi.rows;
roi.adjustROI(1, 0, 0, 0);

ASSERT_EQ(roi.rows, rowsInROI + 1);

roi.adjustROI(-m.rows, -m.rows, 0, 0);

ASSERT_EQ(roi.rows, m.rows);
}

/////////////////////////////////////////////////////////////// Size ////////////////////////////////////////////////////////////////////

PARAM_TEST_CASE(UMatTestSizeOperations, int, int, Size, bool)
Expand Down

0 comments on commit eee638f

Please sign in to comment.