# Buggy code
def custom(img, vertices):
    """Create an custom polygon ROI.

    Inputs:
    img           = An RGB or grayscale image to plot the ROI on in debug mode.
    vertices      = List of vertices of the desired polygon ROI

    Outputs:
    roi_contour   = An ROI set of points (contour).
    roi_hierarchy = The hierarchy of ROI contour(s).

    :param img: numpy.ndarray
    :param vertices: list
    :return roi_contour: list
    :return roi_hierarchy: numpy.ndarray
    """
    # Autoincrement the device counter
    params.device += 1

    # Get the height and width of the reference image
    height, width = np.shape(img)[:2]

    roi_contour = [np.array(vertices, dtype=np.int32)]
    roi_hierarchy = np.array([[[-1, -1, -1, -1]]], dtype=np.int32)

    if params.debug is not None:
        _draw_roi(img=img, roi_contour=roi_contour)

    return roi_contour, roi_hierarchy


# Traceback (RuntimeError)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-143-af286afe5707> in <module>
4                              spacing=(0, 1150),
5                              ncols=1,
----> 6                              nrows=2)

~\Miniconda3\envs\test-environment\lib\site-packages\plantcv-3.6.1+51.g90ef8d0.dirty-py3.7.egg\plantcv\plantcv\roi\roi_methods.py in multi(img, coord, radius, spacing, nrows, ncols)
262                 x = coord[0] + j * spacing[0]
263                 # Create a chip ROI
--> 264                 rois.append(circle(img=img, x=x, y=y, r=radius))
265                 # Draw the circle on the binary image
266                 cv2.circle(bin_img, (x, y), radius, 255, -1)

~\Miniconda3\envs\test-environment\lib\site-packages\plantcv-3.6.1+51.g90ef8d0.dirty-py3.7.egg\plantcv\plantcv\roi\roi_methods.py in circle(img, x, y, r)
120     # Check whether the ROI is correctly bounded inside the image
121     if x - r < 0 or x + r > width or y - r < 0 or y + r > height:
--> 122         fatal_error("The ROI extends outside of the image!")
123
124     # Initialize a binary image of the circle

~\Miniconda3\envs\test-environment\lib\site-packages\plantcv-3.6.1+51.g90ef8d0.dirty-py3.7.egg\plantcv\plantcv\fatal_error.py in fatal_error(error)
12     """
13
---> 14     raise RuntimeError(error)

RuntimeError: The ROI extends outside of the image!

# Fixed code
def custom(img, vertices):
    """Create an custom polygon ROI.

    Inputs:
    img           = An RGB or grayscale image to plot the ROI on in debug mode.
    vertices      = List of vertices of the desired polygon ROI

    Outputs:
    roi_contour   = An ROI set of points (contour).
    roi_hierarchy = The hierarchy of ROI contour(s).

    :param img: numpy.ndarray
    :param vertices: list
    :return roi_contour: list
    :return roi_hierarchy: numpy.ndarray
    """
    # Autoincrement the device counter
    params.device += 1

    # Get the height and width of the reference image
    height, width = np.shape(img)[:2]

    roi_contour = [np.array(vertices, dtype=np.int32)]
    roi_hierarchy = np.array([[[-1, -1, -1, -1]]], dtype=np.int32)

    if params.debug is not None:
        _draw_roi(img=img, roi_contour=roi_contour)

    # Check that the ROI doesn't go off the screen
    for i in vertices:
        (x, y) = i
        if x < 0 or x > width or y < 0 or y > height:
            fatal_error("An ROI extends outside of the image!")

    return roi_contour, roi_hierarchy