Desmos is a graphing calculator that people sometimes misuse for creating amazing graphics. I had created some graphs before :) Roger Federer, Pikachu, Deoxys

For creating these graphs, I had used simple tools mostly like ellipses, parabolas, circles, and straight lines. Believe me! It is a lot of fun. But, it takes a lot of time too.

So, I was thinking of automating it for a long time. I came across this inspirational video on YouTube that is a full fledged Desmos graph generator which uses Bezier curves to produce amazing Graphics. The video: How I animate stuff on Desmos Graphing Calculator. You can see the equations on the left and the plots as well

For this project, I am aiming for something simple though. I want to plot an image just using straight lines and produce those equations which can be imported into Desmos. Let’s get straight into the code.

Image Processing

The whole project is based on simple image processing operations:

1. canny edge detection of an input image
2. detecting contours in the image and selecting which ones to keep based on a criterion (rejected the contours which had too few points(< 3) )
3. for each contour, opencv returns a set of coordinates defining the contour.
4. for each contour, I divided the contour into pairs of points. For example, if a contour is defined by the points [(1,1),(2,2),(3,3),(4,4)], we get 2 sets [(1,1),(2,2)] and [(3,3),(4,4)]
5. For each set, say [(1,1),(2,2)], I fitted a line using y = mx + c. These equations are then pasted in an equation array.

Function for breaking a list into chunks:

Generating equations:

Plotting the equations:

Results Gallery

Here are some results from my experiment.