Computer Vision Pi

by dcolemans in Circuits > Software

1115 Views, 7 Favorites, 0 Comments

Computer Vision Pi

IMG_20210304_141201.jpg
IMG_20210304_151547.jpg

I’m not a baker. I’m not a math maven. What can I possibly do for Pi Day? Why not build a system for calculating Pi using the Monte Carlo “needle dropping” method.

According to Wikipedia, the origins of this needle dropping Monte Carlo approach are from an 18th century chap named Georges-Louis Leclerc, Comte de Buffon. He wanted to know the probability that a needle dropped onto a surface with uniform lines would cross or touch one of the lines. He came up with the formula P = 2 L /π T where L is the needle length and T is the distance between lines. So by dropping many (N) needles and counting the ones that cross (H), one can approximate Pi with the formula π = 2 L N/ T H.

Great! But to get anywhere near Pi one would have to know how many needles were dropped and count how many crossed lines. This tedious task is just asking to be automated via computer vision. My approach was to use a small camera with computer vision built in called an OpenMV camera. It connects to a PC via USB and comes with a feature rich development interface for programming its built in Micro-Python engine.

The following Instructable follows my attempts to use OpenMV camera to count the needles and approximate Pi. I would be using the built-in method Get_Line_Segments to find all the needles.

Supplies

OpenMV camera

Bread crate, wooden slat, and clamp

1/2 inch brads

​The Frame

IMG_20210304_141201.jpg

The camera needed to be held steady above the lines and needles to get a reliable view. I used a bread crate, put a wooden slat across, and attached my OpenMV camera with a clamp. A board with white paper at the bottom held the needles.

Oh yes, I 3d printed a holder for the OpenMV cam to hold the cam securely and provide a clamping surface. This is optional since one can clamp the cam directly to the slat.

​Needles and Lines

IMG_20210304_141316.jpg

I tried different approaches to see what would work best for the computer vision. For the even lines, pencil lines were not easily visible and pen lines were so wide that each drawn line gave two computer lines. The best bet seemed to be tape. The boundary between tape and no tape gave a single line. But the camera found each line broken into lots of segments. The camera image was 160 pixels wide and 120 pixels high.

It finally dawned on me that I didn’t need real lines. I just needed to decide on the distance between lines and use “imaginary lines” to see if the needles crossed. After I decided on the distance between lines, I could simply code line locations into my Python program.

For needles, I settled on ½ inch brads. I scattered a few under the camera, ran the Python program and printed out all found segments. The diagnostic print showed that the brads were about 10 pixels long. (Not all actual brads were recognized by the program but it would be fine just to work with the ones that were found.)

With 10 pixel brads, I decided that the vertical imaginary lines would be 15 pixels apart.

​Counting Needles

PinPython.jpg

The Python program is a modification of the find_line_segments example, supplied with the IDE (programming environment).

The program continuously loops, taking a snapshot of the brads, looking for all line segments.

For each segment found, it adds to a counter, then looks through all imaginary vertical lines to see if that segment crosses a line. If so, it adds to another counter.

After looking at all segments, the program prints out the number of brads found, the number that cross a line, pi calculated from that snapshot.

It also prints an averaged pi calculated from a rolling total of all snapshots. To give it some variety so different results go into the average, I jiggled the paper to rearrange the brads slightly.

And, as the Count of Buffon would say, et voila! The results are not too close to Pi, but it was fun trying.