def get_the_line_rect(line_coords, img, margin_x=0, margin_y=0):
    """
    Returns the rectangular crop with diagonal being the line with specified 'line_coords' optionally modified by a margin.

    Parameters:
    ---------------------------------------
    line_coords: list
        Has format  [x_start,y_start, x_end,y_end]
    img: np.array
        an image
    margin_x: int
        How should the line be modified in x direction
    margin_y: int
        How should the line be modified in x direction

    Returns:
    ---------------------------------------
    matched_rect: np.array
        the cropped rectangle
    final_coords: list
        coordinates of the line the input image taking into account the margin and img_size

    """
    x_s, y_s, x_e, y_e = line_coords  # get the coordinates of the line
    height_match, width_match, _ = img.shape
    x_s, y_s, x_e, y_e = max(min(x_s - margin_x, x_e - margin_x), 0), max(min(y_s - margin_y, y_e - margin_y), 0), min(
        max(x_s + margin_x, x_e + margin_x), width_match), min(max(y_s + margin_y, y_e + margin_y), height_match)

    matched_rect = img[y_s:y_e, x_s:x_e]
    final_coords = [x_s, y_s, x_e, y_e]
    return matched_rect, final_coords