Compute region area in an image

import numpy as np M = np.zeros((400, 300)) # pretend it's an image def area(matrix): return len(matrix.ravel()) Ax, Ay = 20, 30 # left, top Cx, Cy = 200, 160 # right, bottom area1 = (Cx - Ax) * (Cy - Ay) area2 = area(M[Ax:Cx, Ay:Cy]) # From four points (clockwise ordered) # Measuring areas from (0,0) to each (x,y) coordinate of each point Bx, By = Cx, Ay Dx, Dy = Ax, Cy area3 = area(M[:Cx, :Cy]) - (area(M[:Bx, :By]) + area(M[:Dx, :Dy]) - area(M[:Ax, :Ay])) print(area1, area2, area3) # 23400 23400 23400