Skip to content

Commit 7d168ff

Browse files
authored
Merge pull request #3983 from mitresthen:warn-warpaffine-overlap
Add assert to ensure using non-overlapping memory regions #3983 This pr addresses this issue: opencv/opencv#27429 where the user did not realize that the warpaffine function requires non-overlapping src and dst memory regions. The code now compares the input memory regions and asserts that they do not overlap. There is also a test for this functionality. ### 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 - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] 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
1 parent f49f0ae commit 7d168ff

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

modules/cudawarping/include/opencv2/cudawarping.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ CV_EXPORTS_W void resize(InputArray src, OutputArray dst, Size dsize, double fx=
118118
@param src Source image. CV_8U , CV_16U , CV_32S , or CV_32F depth and 1, 3, or 4 channels are
119119
supported.
120120
@param dst Destination image with the same type as src . The size is dsize .
121+
**In-place operation (src == dst) is not supported and will result in an error.**
121122
@param M *2x3* Mat or UMat transformation matrix.
122123
@param dsize Size of the destination image.
123124
@param flags Combination of interpolation methods (see resize) and the optional flag
@@ -127,6 +128,7 @@ INTER_NEAREST , INTER_LINEAR , and INTER_CUBIC interpolation methods are support
127128
@param borderValue
128129
@param stream Stream for the asynchronous version.
129130
131+
@note In-place operation is not supported. If src and dst refer to the same data, the behavior is undefined.
130132
@sa warpAffine
131133
*/
132134
CV_EXPORTS void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags = INTER_LINEAR,

modules/cudawarping/src/warp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ void cv::cuda::warpAffine(InputArray _src, OutputArray _dst, InputArray _M, Size
208208
_dst.create(dsize, src.type());
209209
GpuMat dst = _dst.getGpuMat();
210210

211+
CV_Assert( src.data != dst.data && "In-place operation not supported for cv::cuda::warpAffine" );
212+
211213
Size wholeSize;
212214
Point ofs;
213215
src.locateROI(wholeSize, ofs);

modules/cudawarping/test/test_warp_affine.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,36 @@ CUDA_TEST_P(WarpAffine, Accuracy)
222222
EXPECT_MAT_NEAR(dst_gold, dst, src.depth() == CV_32F ? 1e-1 : 1.0);
223223
}
224224

225+
CUDA_TEST_P(WarpAffine, OverlapDetection)
226+
{
227+
cv::Mat src = randomMat(size, type);
228+
ASSERT_FALSE(src.empty());
229+
cv::cuda::GpuMat gpuSrc;
230+
gpuSrc.upload(src);
231+
232+
cv::Mat M = cv::Mat::eye(2, 3, CV_64FC1);
233+
int flags = interpolation;
234+
if (inverse)
235+
flags |= cv::WARP_INVERSE_MAP;
236+
237+
{
238+
cv::cuda::GpuMat gpuDst(gpuSrc, cv::Rect(0, 0, size.width, size.height));
239+
240+
EXPECT_THROW(
241+
cv::cuda::warpAffine(gpuSrc, gpuDst, M, size, flags, borderType, cv::Scalar::all(0)),
242+
cv::Exception);
243+
}
244+
245+
{
246+
cv::cuda::GpuMat gpuDst(size, gpuSrc.type());
247+
ASSERT_NE(gpuSrc.data, gpuDst.data); // Confirm they are distinct
248+
249+
EXPECT_NO_THROW({
250+
cv::cuda::warpAffine(gpuSrc, gpuDst, M, size, flags, borderType, cv::Scalar::all(0));
251+
});
252+
}
253+
}
254+
225255
INSTANTIATE_TEST_CASE_P(CUDA_Warping, WarpAffine, testing::Combine(
226256
ALL_DEVICES,
227257
DIFFERENT_SIZES,

0 commit comments

Comments
 (0)