ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Count how many objects within a given bounding area

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Thomas Hannen
Count how many objects within a given bounding area
on Dec 5, 2019 at 6:25:42 pm

Hi,
I'd like to count how many objects are within a given area, so that I could produce something a bit like this:

https://twitter.com/electoralreform/status/1025290216473616385

So that for a given region I can count how many (eg blue houses) are within that region.

Been googling around a lot, but really not sure where to start... SampleImage? CountItems? I've written basic expressions before but can't seem to find the right question to search for.

Many thanks,
Tom


Return to posts index

Andrei Popa
Re: Count how many objects within a given bounding area
on Dec 6, 2019 at 9:27:35 am

Try this:

Draw your area as a Shape Layer. Make sure to add fill to it.
Name it "Area" for the expression to work. Choosing another name means you must change the name in the expression too.

Make a text layer. Add this expression to the text field.


layerCount = 0;
startCount = 2; //index of the first counted layer
stopCount = 5; //index of the last counted layer
target = thisComp.layer("Area");
for(var i = startCount; i<=stopCount;i++){
if (target.sampleImage(thisComp.layer(i).transform.position,[1,1],true,time)[3] == 1) {
layerCount++;
}
}
layerCount;


Andrei
My Envato portfolio.


Return to posts index

Thomas Hannen
Re: Count how many objects within a given bounding area
on Dec 6, 2019 at 9:52:24 am

That's great - many thanks!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2020 CreativeCOW.net All Rights Reserved
[TOP]