def extract_red_channel(input_im, bayer_pattern='grbg'):
    """
    Extract and return the red channel from a Bayer image.

    :param input_im: The input Bayer image.
    :param bayer_pattern: The Bayer pattern of the image, either 'rggb' or 'bggr'.
    :return: The extracted channel, of the same type as the image.
    """
    d = {'rggb':(0,0), 'bggr':(1,1), 'grbg': (0,1), 'girg':(1,0)}
    assert bayer_pattern in d, 'Invalid Bayer pattern \'{}\''.format(bayer_pattern)

    red_idx = d[bayer_pattern][0]
    red_idy = d[bayer_pattern][1]
    im = input_im[red_idx::2, red_idy::2, ...]

    return im