Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Count how many objects within a given bounding area

  • Count how many objects within a given bounding area

    Posted by Thomas Hannen on December 5, 2019 at 6:25 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

    Thomas Hannen replied 6 years, 5 months ago 2 Members · 2 Replies
  • 2 Replies
  • Andrei Popa

    December 6, 2019 at 9:27 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.

  • Thomas Hannen

    December 6, 2019 at 9:52 am

    That’s great – many thanks!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy