# Differentiable image values of a triangle

**URL:** <https://discourse.julialang.org/t/differentiable-image-values-of-a-triangle/105821>\
**Category:** Signal and Image Processing\
**Tags:** question, geometry, autodiff, image-processing\
**Created:** [November 5, 2023, 3:39pm UTC](https://discourse.julialang.org/t/differentiable-image-values-of-a-triangle/105821 "2023-11-05T15:39:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_Mitura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_mitura/32/19496_2.png) [@Jakub\_Mitura](https://discourse.julialang.org/u/Jakub_Mitura)\
**Post date:** [November 5, 2023, 3:39pm UTC](https://discourse.julialang.org/t/differentiable-image-values-of-a-triangle/105821/1 "2023-11-05T15:39:56Z")

</div>

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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [November 5, 2023, 6:49pm UTC](https://discourse.julialang.org/t/differentiable-image-values-of-a-triangle/105821/2 "2023-11-05T18:49:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jakub\_Mitura](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakub_mitura/32/19496_2.png) [@Jakub\_Mitura](https://discourse.julialang.org/u/Jakub_Mitura)\
**Post date:** [November 5, 2023, 7:22pm UTC](https://discourse.julialang.org/t/differentiable-image-values-of-a-triangle/105821/3 "2023-11-05T19:22:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 5, 2023, 8:43pm UTC](https://discourse.julialang.org/t/differentiable-image-values-of-a-triangle/105821/4 "2023-11-05T20:43:37Z")

</div>

> [@Jakub\_Mitura](#):
>
> even if it would be it is in if

That’s your smallest problem.

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

```
