First time here? Check out the FAQ!

Ask Your Question
0

Intersection of centroid and line/rectangle

asked Apr 19 '16

kartik gravatar image

I'm currently making a traffic count system using opencv and python and what I exactly intend to do is that,to increment the counter as soon as the centroid of the blob passes or crosses a line or a small rectangle. My question is how to do this as I don't know how to implement cv2.lines or cv2.rectangle in this. Please see this video and if anybody could tell how exactly the intersection or counting is working in this video: https://www.youtube.com/watch?v=z1Cvn...

Thanks in advance.

Preview: (hide)

1 answer

Sort by » oldest newest most voted
1

answered Apr 19 '16

Tetragramm gravatar image

updated Apr 20 '16

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++;
    }
}
Preview: (hide)

Comments

Thanks so much man! Umm, so i'll have to use two lines you're saying ? Sorry, i'm a bit confused.

kartik gravatar imagekartik (Apr 19 '16)edit

The rectangle is two lines. You use that if your position is noisy. If your centroid path is smooth and it never goes backwards, then you'll be fine with one line.

Tetragramm gravatar imageTetragramm (Apr 19 '16)edit

Yes, mine is a bit noisy and the centroid does go back. So, i'm supposed to make two line equations and then put centroid x value into these two equations and check?

kartik gravatar imagekartik (Apr 19 '16)edit

That would be best. Let's assume blobs are only going up and down, and your lines are horizontal.

I'm editing the answer with some basic code. If your lines are diagonal, replace the constants with the equation for the line.

Tetragramm gravatar imageTetragramm (Apr 20 '16)edit

Question Tools

1 follower

Stats

Asked: Apr 19 '16

Seen: 2,654 times

Last updated: Apr 19 '16