I have a 2D gray and white image and set of coordinates of triangles (generally all of the image is triangluted) is there a differentiable stable way to get mean and variance of the part of the image enclosed by each triangle?

I then want then to update triangle verticies positions to reduce variance of the image intensity values in each triangle

Example non differentiable code

```
function area(x1, y1, x2, y2, x3, y3)
return abs((x1 * (y2 - y3) + x2 * (y3 - y1)
+ x3 * (y1 - y2)) / 2.0)
end #area
# A function to check whether point P(x, y)
# lies inside the triangle formed by
# A(x1, y1), B(x2, y2) and C(x3, y3)
function isInside(x1, y1, x2, y2, x3, y3, x, y)
# Calculate area of triangle ABC
A = area(x1, y1, x2, y2, x3, y3)
# Calculate area of triangle PBC
A1 = area(x, y, x2, y2, x3, y3)
# Calculate area of triangle PAC
A2 = area(x1, y1, x, y, x3, y3)
# Calculate area of triangle PAB
A3 = area(x1, y1, x2, y2, x, y)
# Check if sum of A1, A2 and A3
# is same as A
if(A == A1 + A2 + A3)
return true
else
return false
end#if
end#isInside
function is_in_triangle(i,j, Triangle_points)
# return isInside(Triangle_points[1,1],Triangle_points[1,2] , Triangle_points[2,1], Triangle_points[2,2], Triangle_points[3,1], Triangle_points[3,3], i, j)
return isInside(Triangle_points[1][1],Triangle_points[1][2] , Triangle_points[2][1], Triangle_points[2][2], Triangle_points[3][1], Triangle_points[3][2], i, j)
end#is_in_triangle
function mainn()
Sum_p =0
Triangle_points =[[0.0,0.0],[0.0,10.0],[10.0,0.0]]# coordinates of a triangle
Image= range(1,1000*1000,step=1)
Image=reshape(Image,(1000,1000))
for i in 1:100, j in 1:100
Sum_p= Sum_p +(Image[i,j]*is_in_triangle(i,j, Triangle_points ))
end#for
print(Sum_p)
return Sum_p/(1000*1000)
end #main
rr=mainn()
print(rr)
```