# Detect if object is above line

**URL:** <https://forum.opencv.org/t/detect-if-object-is-above-line/5109>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [September 8, 2021, 8:07pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109 "2021-09-08T20:07:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mars](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mars](https://forum.opencv.org/u/mars)\
**Post date:** [September 8, 2021, 8:07pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/1 "2021-09-08T20:07:46Z")

</div>

As per the title, can you suggest a way to identify if the object to the right of the picture below is above/below the line marked with the blue arrows. Unfortunately I can’t post them as two separate pictures because new users are limited to one embedded media per post.

 ![tracking](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/5/5712ca12a0e58422cbd975fe700978037ddb0c53.png)

Thanks!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 8, 2021, 8:12pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/2 "2021-09-08T20:12:47Z")

</div>

please do not [duplicate](https://forum.opencv.org/t/detecting-curved-line/5108) your questions here, thank you.

---

<div class="post-metadata">

**Author:** ![mars](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mars](https://forum.opencv.org/u/mars)\
**Post date:** [September 8, 2021, 8:14pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/3 "2021-09-08T20:14:02Z")

</div>

There are two separate problems. One is detecting the curved line, which I’m 90% there on and the other is working out if an object is above/below the detected line.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 8, 2021, 8:40pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/4 "2021-09-08T20:40:27Z")

</div>

okay so… you have to extrapolate the line?

it’s not a straight line. apply whatever curve model you like that seems to fit well.

what even do we see in that picture? it’s abstract art.

---

<div class="post-metadata">

**Author:** ![mars](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mars](https://forum.opencv.org/u/mars)\
**Post date:** [September 8, 2021, 8:47pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/5 "2021-09-08T20:47:13Z")

</div>

No, sorry. Because of the rules of the forum I can’t post two embedded media in the same post so I had to merge the two side by side in one picture. Essentially the object to be tracked will always be above/below the line I pointed out and no extrapolation of said line is needed.

Essentially, the camera starts with a static background from which I extract my line. As the objects start to come into view, I isolate them and need to determine if, at any point when they are in view, they move above the line.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 8, 2021, 8:49pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/6 "2021-09-08T20:49:15Z")

</div>

maybe I should lobby harder for new users to be allowed more pictures…the limit annoys everyone.

anyway, you could use imgur, dropbox, google drive, … whatever is convenient.

---

<div class="post-metadata">

**Author:** ![mars](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mars](https://forum.opencv.org/u/mars)\
**Post date:** [September 8, 2021, 8:53pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/7 "2021-09-08T20:53:37Z")

</div>

Because I’ve been deep in this for quite a few hours, my brain foolishly assumed everyone has the same overview of the problem that I do. Hope everything is clear now? In any case, here are the two pictures separately:

[Fixed line](https://drive.google.com/file/d/1OBDxJbmFTePe9lEsSuguWL_kSISxQwMA/view?usp=sharing)  
[Moving object whose position in relationship to the line need to be determined](https://drive.google.com/file/d/1YmCOwAi2Bks5cJElVeGzGYiIZleFxcWw/view?usp=sharing)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 8, 2021, 9:45pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/8 "2021-09-08T21:45:06Z")

</div>

per-pixel binary/boolean operation.

calculate the intersection (`bitwise_and`). then see if the intersection is empty or not (`countNonZero`).

equivalent operations are provided by numpy, so you get to choose.

[https://docs.opencv.org/4.5.2/d0/d86/tutorial\_py\_image\_arithmetics.html](https://docs.opencv.org/4.5.2/d0/d86/tutorial_py_image_arithmetics.html)

a three-level illustration:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/0eda0f6acde0785186dd559b326120c7147b3fa5.png)

white area is intersection. both masks are true there.

---

<div class="post-metadata">

**Author:** ![mars](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mars](https://forum.opencv.org/u/mars)\
**Post date:** [September 9, 2021, 6:13am UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/9 "2021-09-09T06:13:25Z")

</div>

Great, thanks! One more question. When you said in the other thread about the correct tools for a certain problem, that got me thinking on how I recognize the outline of the moving objects. That part is done again with a blow torch and a hammer:

1. Convert each frame to grayscale
2. Apply an absDiff() on two consecutive frames.
3. Apply a Cloe morphological transformation on the diff
4. Binary threshold to a value which seems to give out decent results
5. Apply a blur transformation

It’s not ideal, but it works. Main problem is getting a “tail” on the position of the object in the current frame and the fact that the outline is not always consistent - depending on the color of the object.

Here’s a few pictures of the objects being tracked. Getting the outline right (especially at the bottom of the object) is critical. [Obj1](https://drive.google.com/file/d/1fPcIWtglmCTRQ3Dd8DrRRrOp7s8KbaiB/view?usp=sharing), [obj2](https://drive.google.com/file/d/1OggJs7EzluCElr1T1XAwOOz6ApbDIAdT/view?usp=sharing)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 9, 2021, 2:51pm UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/10 "2021-09-09T14:51:18Z")

</div>

that’s a good first attempt…

I would however recommend something from the `bgsegm` module. same principle (differences), but it builds a “model” (picture) of the background rather than using the previous frame.

you might need morphology operations and/or blur to clean that up too.

[https://docs.opencv.org/master/d1/dc5/tutorial\_background\_subtraction.html](https://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html)

[https://docs.opencv.org/master/d8/d38/tutorial\_bgsegm\_bg\_subtraction.html](https://docs.opencv.org/master/d8/d38/tutorial_bgsegm_bg_subtraction.html)

---

<div class="post-metadata">

**Author:** ![mars](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mars](https://forum.opencv.org/u/mars)\
**Post date:** [September 12, 2021, 6:45am UTC](https://forum.opencv.org/t/detect-if-object-is-above-line/5109/11 "2021-09-12T06:45:17Z")

</div>

Thanks. Very good resources. Have switched over to KNN and works well. One thing I can’t seem to do is get a decent filter to fill in the object properly and get rid of the noise.

[Raw output of KNN](https://imgur.com/mRmXu4g)  
[Processed mask](https://imgur.com/4htMhRd)

The processed mask is obtained with an open morphology done with ellipse kernel (3x5)
