# Laser pattern rangefinding using a diffraction grating

**URL:** https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564
**Category:** Python
**Tags:** calib3d, raspberrypi
**Created:** [June 6, 2026, 2:49pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564 "2026-06-06T14:49:11Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![salukikev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salukikev/32/14294_2.png) [@salukikev](https://forum.opencv.org/u/salukikev)
#### Post date: [June 6, 2026, 2:49pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/1 "2026-06-06T14:49:11Z")

</div>

Hi all! I’m experimenting with a RPI5-1gb and green laser diffraction grating (nearly coaxial with Pi camera module 3). The goal is to reference the projected pattern, and calculate range and angles based upon the scale and angles of the pattern shown. As you can see I’ve had some success but so far its not been robust and not consistent over a dynamic moving non-flat surface. I’m coding in Python but thinking that somebody(s) much smarter than me has probably refined a version of this approach long before now and I’d like to reference or learn from that approach before going down this same road and duplicating all that effort. I’m actually pretty new to all of this and of course have been vastly helped along with AI tools. Anyway, thanks for any suggestions!

 ![TrackProject](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/0/04814c37aa447333e5bba2e7e29f7dcdec9d705e.jpeg)

---

<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: [June 6, 2026, 3:22pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/2 "2026-06-06T15:22:26Z")

</div>

how do you imagine you’d calculate the distance?

---

<div class="post-metadata">

### Author: ![salukikev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salukikev/32/14294_2.png) [@salukikev](https://forum.opencv.org/u/salukikev)
#### Post date: [June 6, 2026, 3:38pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/3 "2026-06-06T15:38:40Z")

</div>

That part should be relatively easy since the scale grows at a known rate with distance.

---

<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: [June 6, 2026, 6:41pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/4 "2026-06-06T18:41:38Z")

</div>

and how is the camera going to see this?

> [@salukikev](#):
>
> green laser diffraction grating (nearly coaxial with Pi camera module 3)

that sounds to me as if the laser and the camera are physically linked.

are they, or are they not?

what you have described so far does not constitute a clear and unmistakable “picture” of the situation.

perhaps show the situation instead of describing it.

---

<div class="post-metadata">

### Author: ![salukikev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salukikev/32/14294_2.png) [@salukikev](https://forum.opencv.org/u/salukikev)
#### Post date: [June 6, 2026, 8:56pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/5 "2026-06-06T20:56:20Z")

</div>

![Diagram1](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/123aedf6e42552e455c2cdb9674b37cc9a0a6e73.jpeg)

Ok. Here it is. Yes, the laser and camera are mounted to the same platform which is itself mounted to a servo actuator for pitch control.

---

<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: [June 7, 2026, 6:08am UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/6 "2026-06-07T06:08:28Z")

</div>

given that situation, your camera will see the pattern be the same apparent scale (size in the image) no matter the distance.

did you expect that?

---

<div class="post-metadata">

### Author: ![salukikev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salukikev/32/14294_2.png) [@salukikev](https://forum.opencv.org/u/salukikev)
#### Post date: [June 7, 2026, 3:40pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/7 "2026-06-07T15:40:58Z")

</div>

I _didn’t_ expect that and I’m not sure I’m in agreement (yet), but its an interesting point to consider thanks for bringing it up- my original presumption was wrong. In my mind the ranging was defined by the difference between the divergence angle of the laser and the FOV angle of the camera, but it seems like that ends up cancelling out within the present equation(which I think is your point). That said, as I was continuing my discussion with (Claude) AI, the point being made is that the separation between points is what’s being used to triangulate and derive ranging and so it’s (allegedly) still valid. I’m interested if you agree with this reasoning/quote:

**Why your system actually works:**

The camera lens nodal point and the laser emission point are NOT at exactly the same physical location — they’re close together on the same surface but separated by some small distance, maybe 2-5cm. That separation is a real triangulation **baseline**. It’s small, which is why our ranging has needed a correction factor, but it’s real and it’s what’s actually enabling the distance measurement.

---

<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: [June 7, 2026, 4:38pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/8 "2026-06-07T16:38:21Z")

</div>

> [@salukikev](#):
>
> the separation between points

if you are planning to make something like a Kinect, where the _optical axes_ of the projector and the camera are _parallel_, but have a specific lateral distance, “baseline”, one could say an “[inter-pupillary distance](https://en.wikipedia.org/wiki/Pupillary_distance)”, then that makes depth perception possible.

this only works because the optical axes _do not_ coincide. the _more_ distance, the _more accurate_ this becomes.

This relies on _ **parallax** _, not scale. The scale will be the same, hence meaningless. the geometry of that is _similar triangles_, or close enough to it (if IPD/baseline goes to zero or distance goes to infinity).

if you took the AI’s “separated by some small distance” to mean you should get those arbitrarily close, that is not so. aim for a _large_ baseline, as large as feasible, not a small one. there is a practical limit to the distance, where the cones of the one eye and the other eye/projector don’t overlap at a near enough distance.

You should get your hands on a decent book. _Multiple-View Geometry_ is one of the old tomes (mid 2000s). every library probably has one. if you google for it, you’ll probably find its full text on the internet. there are other books on computer vision with less of a focus and depth on mult-view geometry. as long as the book covers multi-view geometry at all, it should help you.

you should also look for a structured _class_ or course on computer vision. this is complex stuff. you should not wade into this on your own.

---

<div class="post-metadata">

### Author: ![salukikev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salukikev/32/14294_2.png) [@salukikev](https://forum.opencv.org/u/salukikev)
#### Post date: [June 7, 2026, 5:42pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/9 "2026-06-07T17:42:59Z")

</div>

Yes- I understand the principal more clearly now and this iteration was designed with a different agenda in mind with regards to placement. I could maybe double the present distance given my revised understanding but maybe it means that I just don’t do the laser ranging that I’d hoped(I do understand the separation distance value). The laser is still useful for determining angle geometry but the whole laser notion was meant primarily as a learning experiment and to that end it served its purpose. I’m not looking to commercialize anything here or build a career out of this so I’m content with the experience as is. I already know an easy solution is to use an ultrasonic sensor or similar off the shelf assembly here, but I wanted to learn something new(particularly I wanted to play with opencv and maybe I lost sight of the goal!). I’m not dismissing your suggestion for the course or book- maybe if I find myself with more time in the future. Thanks for your help!

---

<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: [June 7, 2026, 8:21pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/10 "2026-06-07T20:21:23Z")

</div>

> [@salukikev](#):
>
> ultrasonic sensor

if you’re considering that, you should also consider cheap time-of-flight sensor modules. single ray or low-resolution array (8x8, VL53L5CX), not a “time-of-flight camera”. the VL53L0X / VL53L1X is a popular module you can get on aliexpress for a few bucks a piece. they can achieve distance resolution of 1-2 millimeters.

and there is nothing stopping you from playing with the parallax calculations and the laser diffraction grating. I personally would start with a plain laser point instead of the reticle. that is trivial to locate in the camera image. the rest is subtraction and division.

---

<div class="post-metadata">

### Author: ![salukikev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/salukikev/32/14294_2.png) [@salukikev](https://forum.opencv.org/u/salukikev)
#### Post date: [June 7, 2026, 9:28pm UTC](https://forum.opencv.org/t/laser-pattern-rangefinding-using-a-diffraction-grating/24564/11 "2026-06-07T21:28:47Z")

</div>

Thanks! Funny you should mention that as I’d placed an order for a pack of VL53L0X after your last msg, seems like a part I should have on hand even if I don’t use it for this project. Another notion I was mulling was use of a sharp edged round, white spotlight. I could hypothetically calculate using the varying elliptical profiles for angle orientation and brightness for ranging (which could apply to the laser as well but it sounds like either effort at trying to correlate an overall brightness to distance value would be a dicey and inconsistent headache). This would allow the target area to be more useful as both a live feed as well as a sensory data source. I guess I’ll just have to use the right tool for the right job after all. haha.
