Differentiable image values of a triangle

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)

Hi @Jakub_Mitura!
Can you maybe share an example code that does what you want to get the conversation started? I have no idea of the right algorithm to apply here (sounds like a job for computation geometry), but once you provide it, we can help you modify it to make it differentiable.
Just to be clear, you want the derivatives of every triangle variance with respect to triangle coordinates ? or with respect to pixelwise intensity values? What are the dimensions of the input and output?

Thanks i will provide simple julia code tomorrow to main answer, for now i will optimize the coordinates positions and procedure is like in pseudocode just a mean for now is enough to show

Image #100 by 100 image

function is_in_triangle(i,j , Triangle_points )

non differentiable code

Return bool result
end

Sum_p =0
Triangle_points # 3 coordinates of a triangle
For i in 1:100 j in 1:100
If(is_in_triangle(i,j, Triangle_points ))
Sum_p= Sum_p +Image[i,j]
End#if
End#for

Return Sup_p/100×100

Problematic is is_triangle function that in my implementation is not differentiable and even if it would be it is in if

That’s your smallest problem.

Sum_p += is_in_triangle(i, j, Triangle_points) * image[i, j]