Skip to main content
Inspiring
December 3, 2021
Question

Calculate geometric center of mask

  • December 3, 2021
  • 2 replies
  • 477 views

Here's a tough one (I think).

Is it possible to calculate the geometric centre of a mask via expression? I need to sample a colour in that exact position on every frame.

Thanks, Wim

This topic has been closed for replies.

2 replies

Mylenium
Legend
December 3, 2021

Should be easy enough by adding all vertices to an array and then iterate through them, push the calculated centers into a new array and then repeat this iteratively until you arrive at only three points for the final centroid. Here's a quick non-AE example that I found that provides the base to work from:

 

https://stackoverflow.com/questions/65188028/centroid-of-a-polygon-script

 

This would then need to be expanded with teh specific AE stuff to grab the mask data and create proper arrays to retrieve the data.

 

Mylenium

Mylenium
Legend
December 3, 2021

Entireley depends on what you mean by "center". Center of gravity? Shortest distance to the perimeter? Center of the circumscribing circle? Averaged center in a virtual grid? Intersection of all vertex connections? Center of the bounding box? Others? See, it's all down to what you exactly see as the "center" and depending on what shape(s) your mask(s) have, the answer could be simple or infinitely complicated. More info and perhaps a screenshot would perhaps help to provide a more straightforward answer.

 

Mylenium

Inspiring
December 3, 2021

Hi, it's the centre of gravity I'm after. Or the 'centroid', in mathspeak.

m1b
Community Expert
Community Expert
December 13, 2021

Here's an example written for Illustrator. Might be easy to adapt to AE.

- Mark