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

Code before:
    //Flatten the Union Find tree and relabel the components
    template<typename LabelT>
    inline static
    LabelT flattenL(LabelT *P, LabelT length){
        LabelT k = 1;
        for (LabelT i = 1; i < length; ++i){
            if (P[i] < i){
                P[i] = P[P[i]];
            }
            else{
                P[i] = k; k = k + 1;
            }
        }
        return k;
    }

    template<typename LabelT>
    inline static
    void checkLabelTypeOverflow(const LabelT numLabels) {
        constexpr LabelT maxLabelTypeValue = std::numeric_limits<LabelT>::max();
        if (numLabels == maxLabelTypeValue) {
            CV_Error(
                cv::Error::StsError,
                "Total number of labels overflowed label type. Try using CV_32S instead of CV_16U as ltype");
        }
    }

    template<typename LabelT>
    inline static
    void flattenLParallel(LabelT *P, const int start, const int nElem, LabelT& k){
        for (int i = start; i < start + nElem; ++i){
            if (P[i] < i){//node that point to root
                P[i] = P[P[i]];
            }
            else{ //for root node
                P[i] = k;
                checkLabelTypeOverflow(k);
                k = k + 1;
            }
        }
    }

    template <typename LT> static inline
    LT stripeFirstLabel4Connectivity(int y, int w)
    {
        CV_DbgAssert((y & 1) == 0);
        return (LT(y) * LT(w) /*+ 1*/) / 2 + 1;
    }

    template <typename LT> static inline
    LT stripeFirstLabel8Connectivity(int y, int w)

Review comment (suggestion):
You can use `CV_CheckLE` instead of new function everywhere: https://docs.opencv.org/4.x/d1/d26/check_8hpp.html#a7b8e786a57dcee88dc0cae752adab921

Code after:
    //Flatten the Union Find tree and relabel the components
    template<typename LabelT>
    inline static
    LabelT flattenL(LabelT *P, LabelT length){
        LabelT k = 1;
        for (LabelT i = 1; i < length; ++i){
            if (P[i] < i){
                P[i] = P[P[i]];
            }
            else{
                P[i] = k; k = k + 1;
            }
        }
        return k;
    }

    template<typename LabelT>
    inline static
    void checkLabelTypeOverflowBeforeIncrement(const LabelT numLabels) {
        constexpr LabelT maxLabelTypeValue = std::numeric_limits<LabelT>::max();
        CV_CheckLT(
            numLabels,
            maxLabelTypeValue,
            "Total number of labels overflowed label type. Try using CV_32S instead of CV_16U as ltype");
    }

    template<typename LabelT>
    inline static
    void flattenLParallel(LabelT *P, const int start, const int nElem, LabelT& k){
        for (int i = start; i < start + nElem; ++i){
            if (P[i] < i){//node that point to root
                P[i] = P[P[i]];
            }
            else{ //for root node
                P[i] = k;
                checkLabelTypeOverflowBeforeIncrement(k);
                k = k + 1;
            }
        }
    }

    template <typename LT> static inline
    LT stripeFirstLabel4Connectivity(int y, int w)
    {
        CV_DbgAssert((y & 1) == 0);
        return (LT(y) * LT(w) /*+ 1*/) / 2 + 1;
    }

    template <typename LT> static inline
    LT stripeFirstLabel8Connectivity(int y, int w)
    {