OpenCV GUI CanvasCV has line crossing detection

OpenCV GUI CanvasCV

OpenCV takes you a long way, but you have some distance to go by yourself to complete your projects. You can handle the GUI task by using a full blown GUI toolkit, but still there are some specific solutions that you would need from a GUI toolkit for CV. CanvasCV wants to make it easier for you with the LineCrossing shape.

First, if you missed the previous post on CanvasCV then see here.

Why the OpenCV GUI CanvasCV needs LineCrossing?

It’s a common task in CV to want to know if something you track crossed a line, and sometimes you even want to know the direction.

canvascv::LineCrossing is a GUI shape component in CanvasCV which can:

  • Be drawn on the screen with the mouse.
  • Has an attached label for naming.
  • Has a visual crossing direction attached to it.
  • You can persist it to a file.

Here is a screenshot from the CanvasCV project.

So how would you use it?

In this example you want to count people coming in and out in a footage from a over head camera pointing down. We’ll assume you have a Tracker, which on some events asks a CountingManager to evaluate which lines are crossed and handle counters.

Let’s say you want the user to draw LineCrossing objects on the screen, and handle the counting in the code. We’ll add  our own primitive CountingManager that does that.

  • First you need to know when the user created and deleted the lines
    • The CTOR will register callbacks for create and delete notifications.
  • We’ll maintain a list of LineCrossing objects and an integer count paired to that line.
    • Using a std::pair for this is primitive, but sufficient here.
  • When the Tracker (not show here) needs to know if the line segment was crossed it invokes our evaluatePerson() method and pass it the movement vector of the person.
    • That vector could start where the Person was first detected, or from the last couple of detections – you decide.
  • evaluatePerson() simply queries the LineCrossing::isCrossedBySegment() API.

See the code:

CanvasCV main loop

The mainloop will do something like this (see the CanvasCV tutorials):

 Example video

In the video below the count increments with the direction and decrements against the direction.

The top-left point of the Text widget should pass through a line for it to be counted.


  • CanvasCV fills in the gap with a GUI framework for OpenCV, trying to reduce development time for common tasks.
  • The full code for this tutorial is available here.
  • See the project home page.

Hope you find this useful,

Sagi Zeevi

A software developer brain surgeon ... if software only had a brain. An electronics hobbyist heart surgeon ... if electronics only had a heart.

You may also like...

Leave a Reply

Scroll Up