def sample_image(imgGray, sampleRect, maxVal):
    """ Sample image in a given rectangle, represented as [x0,y0,w,h], where x0,y0 is the top-left coordinates of the sampling region """

    y0, x0, w, h = sampleRect

    sampleSum = 0
    for y in range(h):
        for x in range(w):
            sampleSum += imgGray[y0+y][x0+x]

    sample = float(sampleSum) / (w * h * maxVal)

    return sample