how do I detect on which side is the object if I trace a line the medium

Asked

Viewed 187 times

0

My friends, I’m discovering computer vision. I’m programmed in python and wanted to know how I do to identify in which position the object is in relation to a line drawn in the middle of the screen. Being more direct, if a straight line is in the middle of the screen as I do to know if the detected object is on the left or right?

  • 1

    You already have some code written that you can show?

  • Creating a [mcve] with images and code would help your question to be answered.

1 answer

1

General case 2D:

First let’s consider some parameters.

  • The line is vertical and defined by points A and B
  • The object is entirely on one side or on the other side of the line
  • You have the information of some point P belonging to the object

To define the side the object is on, we reconnect the vector product between the vectors defined by AB and AP:

cross

where:

  • xab
  • xap
  • yab
  • yap

The result of the vector product for two dimensions, can return a negative value, positive or null. If null, the P point in question is above the line. For the positive and negative case, one of them will be on one side of the line and the other case will be on the other side of the line.

In Python this product can be calculated as follows:

import numpy as np

A = np.array([1,2])
B = np.array([5,7])
P = np.array([3,4])

res = np.cross(B-A, P-A)

Exit:

array(-2)

For the specific case above, we have that point P is on the right side of the line defined by AB.

Specific case (vertical line)

In a simpler situation where the dividing line is vertical, there is only the need to compare the coordinated x of objects in relation to the line.

Case the coordinated x the chosen P point of the object is larger than the coordinate x line, this object is on the right of the line. If it is smaller, it is on the left. If it is equal, the point is on top of the line

Browser other questions tagged

You are not signed in. Login or sign up in order to post.