Skip to content

Commit ec97c38

Browse files
authored
Merge pull request opencv#24535 from dkurt:ipp_distransform_update
Handle huge images in IPP distanceTransform opencv#24535 ### Pull Request Readiness Checklist * Do not use IPP for huge Mat (reproduced with opencv#23895 (comment) on `DIST_MASK_5`) I have observed two types of errors on the reproducer from the issue: 1. When `temp` is not allocated: ``` Thread 1 "app" received signal SIGSEGV, Segmentation fault. 0x00007ffff65dc755 in icv_l9_ownDistanceTransform_5x5_8u32f_C1R_21B_g9e9 () from /home/dkurtaev/opencv_install/bin/../lib/libopencv_imgproc.so.408 (gdb) bt #0 0x00007ffff65dc755 in icv_l9_ownDistanceTransform_5x5_8u32f_C1R_21B_g9e9 () from /home/dkurtaev/opencv_install/bin/../lib/libopencv_imgproc.so.408 #1 0x00007ffff659e8df in icv_l9_ippiDistanceTransform_5x5_8u32f_C1R () from /home/dkurtaev/opencv_install/bin/../lib/libopencv_imgproc.so.408 opencv#2 0x00007ffff5c390f0 in cv::distanceTransform (_src=..., _dst=..., _labels=..., distType=2, maskSize=5, labelType=1) at /home/dkurtaev/opencv/modules/imgproc/src/distransform.cpp:854 opencv#3 0x00007ffff5c396ef in cv::distanceTransform (_src=..., _dst=..., distanceType=2, maskSize=5, dstType=5) at /home/dkurtaev/opencv/modules/imgproc/src/distransform.cpp:903 opencv#4 0x000055555555669e in main (argc=1, argv=0x7fffffffdef8) at /home/dkurtaev/main.cpp:18 ``` 2. When we keep `temp` allocated every time: ``` OpenCV(4.8.0-dev) Error: Assertion failed (udata < (uchar*)ptr && ((uchar*)ptr - udata) <= (ptrdiff_t)(sizeof(void*)+64)) in fastFree, file /home/dkurtaev/opencv/modules/core/src/alloc.cpp, line 191 terminate called after throwing an instance of 'cv::Exception' what(): OpenCV(4.8.0-dev) /home/dkurtaev/opencv/modules/core/src/alloc.cpp:191: error: (-215:Assertion failed) udata < (uchar*)ptr && ((uchar*)ptr - udata) <= (ptrdiff_t)(sizeof(void*)+64) in function 'fastFree' ``` * Try enable IPP for 3x3 (see opencv#15904) * Reduce memory footprint with IPP 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. - [x] The feature is well documented and sample code can be built with the project CMake
1 parent 4a69877 commit ec97c38

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

modules/imgproc/src/distransform.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -817,14 +817,15 @@ void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labe
817817
Size size = src.size();
818818

819819
int border = maskSize == CV_DIST_MASK_3 ? 1 : 2;
820-
Mat temp( size.height + border*2, size.width + border*2, CV_32SC1 );
820+
Mat temp;
821821

822822
if( !need_labels )
823823
{
824824
if( maskSize == CV_DIST_MASK_3 )
825825
{
826-
#if defined (HAVE_IPP) && (IPP_VERSION_X100 >= 700) && 0 // disabled: https://github.com/opencv/opencv/issues/15904
827-
CV_IPP_CHECK()
826+
#if defined (HAVE_IPP) && (IPP_VERSION_X100 >= 700)
827+
bool has_int_overflow = (int64)src.cols * src.rows >= INT_MAX;
828+
if (!has_int_overflow && CV_IPP_CHECK_COND)
828829
{
829830
IppiSize roi = { src.cols, src.rows };
830831
if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_3x3_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, _mask) >= 0)
@@ -836,12 +837,14 @@ void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labe
836837
}
837838
#endif
838839

840+
temp.create(size.height + border*2, size.width + border*2, CV_32SC1);
839841
distanceTransform_3x3(src, temp, dst, _mask);
840842
}
841843
else
842844
{
843845
#if defined (HAVE_IPP) && (IPP_VERSION_X100 >= 700)
844-
CV_IPP_CHECK()
846+
bool has_int_overflow = (int64)src.cols * src.rows >= INT_MAX;
847+
if (!has_int_overflow && CV_IPP_CHECK_COND)
845848
{
846849
IppiSize roi = { src.cols, src.rows };
847850
if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_5x5_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, _mask) >= 0)
@@ -853,6 +856,7 @@ void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labe
853856
}
854857
#endif
855858

859+
temp.create(size.height + border*2, size.width + border*2, CV_32SC1);
856860
distanceTransform_5x5(src, temp, dst, _mask);
857861
}
858862
}
@@ -879,7 +883,8 @@ void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labe
879883
}
880884
}
881885

882-
distanceTransformEx_5x5( src, temp, dst, labels, _mask );
886+
temp.create(size.height + border*2, size.width + border*2, CV_32SC1);
887+
distanceTransformEx_5x5( src, temp, dst, labels, _mask );
883888
}
884889
}
885890

0 commit comments

Comments
 (0)