# Find distance from color blue

**URL:** <https://forum.opencv.org/t/find-distance-from-color-blue/10051>\
**Category:** Python\
**Created:** [September 1, 2022, 4:47pm UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051 "2022-09-01T16:47:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dev\_101](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dev_101/32/6486_2.png) [@Dev\_101](https://forum.opencv.org/u/Dev_101)\
**Post date:** [September 1, 2022, 4:47pm UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/1 "2022-09-01T16:47:13Z")

</div>

Hello,  
For a project I need to find the distance from a blue line of tape.  
Some background info is I am trying to create an autonomous model car and need the distance between the camera and the lane.  
How would I go about doing this?

Thanks,  
Dev\_101

 ![Screen Shot 2022-09-01 at 12.45.21 PM](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/2/2be9269708901772a26dbe6b99d2abc65c5cfd0f.png)  
)

---

<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 1, 2022, 5:13pm UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/2 "2022-09-01T17:13:07Z")

</div>

got the camera calibrated? know the width of the tape? can you find the tape (segmentation mask, measure width in pixels)?

---

<div class="post-metadata">

**Author:** ![Dev\_101](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dev_101/32/6486_2.png) [@Dev\_101](https://forum.opencv.org/u/Dev_101)\
**Post date:** [September 1, 2022, 6:46pm UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/3 "2022-09-01T18:46:40Z")

</div>

Hello,  
Sorry for being very in specific. I have never done anything like this before due to me being 12. That said this is the camera I would use: [https://www.amazon.com/Camera-Module-Keenso-Million-Pixels-Automatically/dp/B07WJY2SNZ](https://www.amazon.com/Camera-Module-Keenso-Million-Pixels-Automatically/dp/B07WJY2SNZ).  
Hope that helps,  
Dev\_101

---

<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 2, 2022, 4:01am UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/5 "2022-09-02T04:01:55Z")

</div>

oh ok.

the geometry is basically just similar triangles or [intercept theorem](https://en.wikipedia.org/wiki/Intercept_theorem).

you can skip more involved camera “calibration”. just take a picture of something you can measure physically (width of tape, distance to camera) and in pixels (literally selection rectangle in mspaint will do). choice of physical units doesn’t matter, just has to be consistent.

```auto
focal length [px] = width [px] / width [m] * distance [m]

```

that’s the single constant for the camera (and lens) that’ll relate angles to pixels. angles because it’s a camera, that’s all it sees, and because there’s an angle hidden in the projection equation (see also: camera matrix):

```auto
width[px] = f[px] * width[m] / distance[m]

```

where `tan(angle) = width/distance`. the calculations don’t involve actual angles most of the time, just `f`.

amazon link says 72 degrees field of view (diagonally), so I would expect `f ~ 2230` or so, because `f * tan(72/2 * pi/180) == hypot(2592/2, 1944/2)`

I’d recommend first working on a single picture. since that’s a “pi camera”, there are many ways to take a single picture. when your code works on a single picture, you can easily extend it to work on (live) video. trying to _develop_ while everything moves is just needless complication.

for the programming, you might want to start with

[https://docs.opencv.org/4.x/df/d9d/tutorial\_py\_colorspaces.html](https://docs.opencv.org/4.x/df/d9d/tutorial_py_colorspaces.html)

or whatever you need that leads up to that.

that should give you a “mask” image of your colored tape. you can measure on that by slicing one row of pixels (plot that, see what it looks like) and finding the indices of the first and last positive elements. you _can_ write your own loop for that, or [use numpy](https://stackoverflow.com/questions/47269390/how-to-find-first-non-zero-value-in-every-column-of-a-numpy-array)

all of that relates to “augmented reality”, where the task is to find the pose (distance, orientation) of a square of known size. OpenCV comes with an `aruco` module. that’s basically what you’re doing, just with a dimension more/less (tape vs square…)

from the equation you’ll see that distance depends strongly on how accurately you can measure pixel distances. since that has practical limits, you can and should use larger physical objects, i.e. make the tape wider rather than narrower.

practical tip: work with jupyter notebook/jupyter lab, rather than plain python scripts. notebooks are a bit more interactive and exploratory than trying to develop an entire script by restarting it all the time. notebooks are also usable from Visual Studio Code. a less complicated notebook of my recent ones (inline matplotlib graphs and images): [so-72854648.ipynb · GitHub](https://gist.github.com/crackwitz/e1ba1ce7a6fba446288275d91f66261c). to show numpy arrays as images inline, I use [a bit of custom code](https://gist.github.com/crackwitz/975e81dc4ed48c67af0515cb0924ceb9) in my [`PYTHONSTARTUP` script](https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP) that works similar to the `cv2_imshow` shim available in google colab. oh, yes, you can work in google colab too, but that’s running on a server, so you won’t have access to any local devices (your camera).

---

<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 2, 2022, 9:09am UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/6 "2022-09-02T09:09:37Z")

</div>

> [@Dev\_101](#):
>
> an autonomous model car and need the distance between the camera and the lane.

I thought about that… and if that blue tape is for lane markings, and the camera _doesn’t_ look straight down on the road, then you don’t have _one_ distance to the tape, you have a view _along_ the blue tape, like so:

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/c1ed79dfd9be43e9e1435979aa7a71a456fb0501.jpeg)

([5,682 Dash Cam Stock Video Footage - 4K and HD Video Clips | Shutterstock](https://www.shutterstock.com/video/search/dash-cam))

and in such a view, if you wanted to work with road markings… it’s not as easy anymore.

for a model car in a racetrack or something, you could make the situation simple and give it continuous/solid left and right boundaries, or one center track.

then you could do like a Lego Mindstorms lane following approach, take a horizontal slice of the picture, find the left and right lines, and steer so they’re symmetric in the view.

_real_ road markings are often dashed or missing entirely. some simple, and unsuccessful, approaches still try to handle this with low level image processing, find the lines, extract curves delineating lanes, and feed that into some path planning… and then you have Tesla, implementing serious machine cognition.

---

<div class="post-metadata">

**Author:** ![Dev\_101](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dev_101/32/6486_2.png) [@Dev\_101](https://forum.opencv.org/u/Dev_101)\
**Post date:** [September 2, 2022, 2:55pm UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/7 "2022-09-02T14:55:20Z")

</div>

Hello,

Could I use a two cameras one on each side of the model car to measure the lanes, and if so would that make it easier?

Thanks,  
Dev 101

---

<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 2, 2022, 3:16pm UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/8 "2022-09-02T15:16:35Z")

</div>

you can but that won’t change anything. multiple cameras are required if you need actual depth perception, and lane detection doesn’t need that.

start by putting down some tape and snapping a picture from the model car’s view. picture from a phone camera is just fine for prototyping. it’s ok to post it here.

check this out for an impression: [An image-processing robot for RoboCup Junior - Raspberry Pi](https://www.raspberrypi.com/news/an-image-processing-robot-for-robocup-junior/)

that’s doing the “row of pixels” thing, but apparently then follows the line a few steps ahead to react better to bends in the road. also that’s just evaluating a single lane-center line. doing that for two lane boundary lines is pretty much the same idea though.

---

<div class="post-metadata">

**Author:** ![Dev\_101](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dev_101/32/6486_2.png) [@Dev\_101](https://forum.opencv.org/u/Dev_101)\
**Post date:** [September 4, 2022, 12:55am UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/9 "2022-09-04T00:55:23Z")

</div>

Ok, I get what the machine is supposed to do by calculating the curvature of the lines.

Originally what I was going to do was measure the distance between both lines, subtract it and then turn it a little and move if it was off center - but i guess this is a better approach.

How would I program this though, I am only 12 years old and have not explored image recognition a lot so if there is a very simple free tutorial that would be very useful.

Thanks So much for helping me with this.

Dev 101

---

<div class="post-metadata">

**Author:** ![Dev\_101](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/dev_101/32/6486_2.png) [@Dev\_101](https://forum.opencv.org/u/Dev_101)\
**Post date:** [September 7, 2022, 12:42am UTC](https://forum.opencv.org/t/find-distance-from-color-blue/10051/10 "2022-09-07T00:42:29Z")

</div>

Hello,  
I did some more exploring and I think it is a more viable option to calculate the curvature of the lines rather than following it.

There is nothing specific on this though so can you explain how ai would do this (in simple terms)

The resource that most explains my idea is this article: [Advanced Lane Detection for Autonomous Vehicles using Computer Vision techniques | by Raj Uppala | Towards Data Science](https://towardsdatascience.com/advanced-lane-detection-for-autonomous-vehicles-using-computer-vision-techniques-f229e4245e41)

Thanks,  
Dev\_101
