Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Write the line as y=m*x+b, put in the x of the centroid, and if it changes from above the line to below, or vice-versa, then it has crossed the line.

A rectangle is to avoid noise. If you aren't quite sure of the true center of the blob, you only increment your counter when a blob goes from below the first to above the second, and vice-versa.

Write the line as y=m*x+b, put in the x of the centroid, and if it changes from above the line to below, or vice-versa, then it has crossed the line.

A rectangle is to avoid noise. If you aren't quite sure of the true center of the blob, you only increment your counter when a blob goes from below the first to above the second, and vice-versa.

EDIT:

Point2f prevCenter;
Point2f blobCenter;
bool crossTop = false;
bool crossBottom = false;

int topLine = 100;
int bottomLine = 90;

if (!crossBottom &&
    ((prevCenter.y < bottomLine && blobCenter.y >= bottomLine)
        || (prevCenter.y >= bottomLine && blobCenter.y < bottomLine)))
{
    crossBottom = true;
    if (crossTop)
    {
        counter++;
    }
}

if (!crossTop &&
    ((prevCenter.y < topLine && blobCenter.y >= topLine)
        || (prevCenter.y >= topLine && blobCenter.y < topLine)))
{
    crossTop = true;
    if (crossBottom)
    {
        counter++;
    }
}