def mean_gray_integral(integral, x0, y0, x1, y1):
    """
        Compute mean gray of
        a grayscale image by
        computing the cumulative
        sum using the opencv integral
        function and then dividing by
        the number of pixels
    """
    # Height and width
    # of the patch
    height = x1 - x0 + 1
    width = y1 - y0 + 1

    # Retrieve four integral
    # point for cumulative sum
    # computation
    top_left = integral[x0, y0]
    top_right = integral[x0, y1]
    bottom_left = integral[x1, y0]
    bottom_right = integral[x1, y1]

    # Compute cumulative sum
    cumulative_sum = bottom_right + top_left - bottom_left - top_right

    # Return mean gray
    return cumulative_sum // (height * width)