def get_roi(shift, dim, gap=(0, 0, 0, 0)):
    """
    find the over-lappying region of the image based on shift
    top-left (0, 0), x(+) to the right, y(+) goes down


    :param shift:
        (shift_x, shift_y) positive integer
    :param dim:
        (w , h) positive integer
    :param gap:
        (left, right, top, bottom) positive integer
    :return:

        ( x1, y1, x2, y2, w, h)

        x1, y1 top-left corner on original frame
        x2, y2 top-left corner on shifted frame
        w      width of ROI
        h      height of ROI
    """
    # dimansion of the image
    w0 = dim[1]
    h0 = dim[0]

    # shift of the image
    sx = int(shift[0])
    sy = int(shift[1])

    # gap of the ROI to the over-lapped area
    gl = abs(int(gap[0]))
    gr = abs(int(gap[1]))
    gt = abs(int(gap[2]))
    gd = abs(int(gap[3]))

    # coordinate and size of ROI
    w = w0 - abs(sx) - gl - gr
    h = h0 - abs(sy) - gt - gd

    w = max(0, w)
    h = max(0, h)

    if sx >= 0:
        x1 = gl + sx
        x2 = gl
    else:
        x1 = gl
        x2 = gl + abs(sx)

    if sy >= 0:
        y1 = gt + sy
        y2 = gt
    else:
        y1 = gt
        y2 = gt + abs(sy)

    output = (x1, y1, x2, y2, w, h)
    return output