Repository: opencv/opencv
File: modules/imgproc/src/distransform.cpp (C++)

Code before:
#endif
            {
                IppStatus status;
                IppiSize roi = { src.cols, src.rows };
                Ipp8u *pBuffer;
                int bufSize=0;

                status = ippiTrueDistanceTransformGetBufferSize_8u32f_C1R(roi, &bufSize);
                if (status>=0)
                {
                    pBuffer = (Ipp8u *)CV_IPP_MALLOC( bufSize );
                    status = CV_INSTRUMENT_FUN_IPP(ippiTrueDistanceTransform_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, pBuffer);
                    ippFree( pBuffer );
                    if (status>=0)
                    {
                        static const float correctionDiff = 1.0f / (1 << 12);
                        for (int i = 0; i < dst.rows; ++i)
                        {
                            float* row = dst.ptr<float>(i);
                            for (int j = 0; j < dst.cols; ++j)
                            {
                                float rounded = cvRound(row[j]);
                                if (fabs(row[j] - rounded) <= correctionDiff)
                                    row[j] = rounded;
                            }
                        }
                        CV_IMPL_ADD(CV_IMPL_IPP);
                        return;
                    }
                    setIppErrorStatus();
                }
            }
        }
#endif

        trueDistTrans( src, dst );
        return;
    }

    CV_Assert( distType == cv::DIST_C || distType == cv::DIST_L1 || distType == cv::DIST_L2 );

    getDistanceTransformMask( (distType == cv::DIST_C ? 0 :
        distType == cv::DIST_L1 ? 1 : 2) + maskSize*10, _mask );

    Size size = src.size();

    int border = maskSize == cv::DIST_MASK_3 ? 1 : 2;
    Mat temp;

    if( !need_labels )
    {

Review comment (bug):
I recommend you to add comment with link to the original bug and couple of words why the loop is needed. The reason of "clipping" is not obvious.

Code after:
#endif
            {
                IppStatus status;
                IppiSize roi = { src.cols, src.rows };
                Ipp8u *pBuffer;
                int bufSize=0;

                status = ippiTrueDistanceTransformGetBufferSize_8u32f_C1R(roi, &bufSize);
                if (status>=0)
                {
                    pBuffer = (Ipp8u *)CV_IPP_MALLOC( bufSize );
                    status = CV_INSTRUMENT_FUN_IPP(ippiTrueDistanceTransform_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, pBuffer);
                    ippFree( pBuffer );
                    if (status>=0)
                    {
                        // https://github.com/opencv/opencv/issues/24082
                        // There is probably a rounding issue that leads to non-deterministic behavior
                        // between runs on positions closer to zeros by x-axis in straight direction.
                        // As a workaround, we detect the distances that expected to be exact
                        // number of pixels and round manually.
                        static const float correctionDiff = 1.0f / (1 << 11);
                        for (int i = 0; i < dst.rows; ++i)
                        {
                            float* row = dst.ptr<float>(i);
                            for (int j = 0; j < dst.cols; ++j)
                            {
                                float rounded = static_cast<float>(cvRound(row[j]));
                                if (fabs(row[j] - rounded) <= correctionDiff)
                                    row[j] = rounded;
                            }
                        }
                        CV_IMPL_ADD(CV_IMPL_IPP);
                        return;
                    }
                    setIppErrorStatus();
                }
            }
        }
#endif

        trueDistTrans( src, dst );
        return;
    }

    CV_Assert( distType == cv::DIST_C || distType == cv::DIST_L1 || distType == cv::DIST_L2 );

    getDistanceTransformMask( (distType == cv::DIST_C ? 0 :
        distType == cv::DIST_L1 ? 1 : 2) + maskSize*10, _mask );

    Size size = src.size();