There's easier way to get rectangle from an image in Python. Since cv2
operates on NumPy arrays, you can use normal slicing (note, that i
corresponds to y
and j
- to x
, not the other way):
rect = image[i:i+h, j:j+w]
And taking mean is even simpler:
rect.mean()