*(Or, How to Find an Equation for Every Picture)*

- Login
- Register

Work for a Member company and need a Member Portal account? Register here with your company email address.

*(Or, How to Find an Equation for Every Picture)*

When developing examples for my lambda-2D programming language, prof. Zach gave me an interesting challenge: can you draw a program in the shape of the Future Sketches logo, that outputs the Future Sketches logo?

Half of the challenge was of course to arrange the symbols in the program to resemble the logo, but the other half struck me as an interesting problem on its own: How to describe a logo with a program?

It might sound obvious; One could of course iterate through every pixel of the logo and state whether it is white or black -- but that would be boring. What would be some ways to describe an image, without explicitly describing the image?

For the self-drawing lambda-2D program, I ended up making it a neat little demo for run-length encoding. But the search for more interesting and implicit methods to describe an image goes on.

The idea of having an equation of a 3D surface, that, when thresholded along Z-axis, produces a binary image resembling the logo came into mind. I vaguely recall that there're those things called Fourier synthesis and discrete cosine transform, which could be useful for this sort of thing. But then a much more evil and hideous thought surfaced: Could I simply fit a polynomial to a bunch of points that I know are on the surface?

We've all used the least squares method to fit polynomials to a bunch of 2D points, surely it'll work for 3D points as well? As long as we're happy to go into higher and higher order polynomials, we could express images of higher and higher compexity. The sheer brute-force mentality of this idea excites me.

A quick surf on the Internet confirms that the least squares method is indeed capable of doing that, albeit it seems prone to numerical accuracy issues. This particular StackOverflow answer helped me figure out the functions I need to call in numpy to make it happen.

It is rather easy to understand: say you have points (x0,y0,z0), (x1,y1,z1), and (x2,y2,z2) and want to fit a 2nd order polynomial to them. So eventually you want an equation that looks like:

and M is [a,b,c,d,e,f,g,h,i]^T, and Z is [z0,z1,z2]^T. (Please excuse the lack of proper formatting due to limitations of the Media Lab website editor).

The system would be over-determined when you have many points, that's why we use the least-squares method, which is a way of saying "If we can't make every point perfectly happy, we make them mostly quite happy overall".

I've previously written a linear algebra library from scratch in JavaScript, in which I implemented a function for least squares approximation. However my linear algebra skills has since deteriorated, so please forgive me (and let me know) if I made any errors in the above simplified explanation!

Now it's time to prepare the points to feed into our solver! Given a binary image of dimension N x N, we can genearte N^2 points, whose x and y coordinates are their position (column, row) in the image, and z can be either -1 (black) or 1 (white). Though z can be in fact any two values for black and white, it seems that normalizing around 0 gives best numerical accuracy. (Yes, in case you don't know already, due to the way computers store floating point numbers, math is best done when operands are close to zero.) For the same reason, the x and y coordinates are normalized to (-0.5, 0.5) range.

Next we need to determine the order of the polynomial needed. Here's my intuition: the graph of y=x has no valley or hills. The graph of y=x^2 has one valley. The graph of y=x^3+x^2 has one valley and one hill. The graph of y=x^4-x^3-x^2 has two valleys and one hill. And so on. Therefore, if your image has N valleys or hills on either X or Y axis, you probably need a polynomial of order somewhere around N+1.

Playing around with the numbers confirmed my hypothesis: with the 7x7 Future Sketches logo, the optimal order seems to be of 8.

Now there's another issue. At first, I made one point for each pixel, totalling 49 points for the 7x7 logo. It turns out that our least squares solver is very "sneaky" and can "cheat" the system. When the resultant surface is visualized at original resolution 7x7, each pixel checks out: they're fitted. However, if we upscale the surface, we'll see that the in-between values might not behave as we would imagine, and could end up being any arbitrary value. Here's a simple example to explain the situation:

Say we want a function y=f(x) to such that f(3 <= x <= 5) = 1. So we tell the computer: "Hey! Find a function that satisfies f(3)=f(4)=f(5)=1." The computer says: "OK, here's one". So we want to test if it's alright. We give it x=3 and get y=1; we give it x=4 and get y=1. Good! However, if we give it x=3.5, we got y=-99999 instead of 1! Technically the computer has done what we've asked, but it's not what we really meant originally. Once you understand this example the solution becomes very obvious: simply give it more points!

But how many more points do we need, such that the computer can no longer betray us? Turns out doubling the samples on each axis is suffice. Recall that the number of valleys and hills a polynomial can make is limited by its order, so when you have enough points, the computer runs out of extra hills and valleys to betray you.

Additionally, I added a 1-pixel border of black pixels around the logo, such that the polynomial always have a smooth outline when thresholded.

With these successful experiments in python, next I worked to port the algorithm to openFrameworks, where I can conduct even more interesting experiments, with shaders!

Eigen is a C++ linear algebra library that I heard a lot of good things about. It's also header only, so it should be quite convenient to include into an openFrameworks project. Therefore I decided to give it a try. I followed the examples on their documentation for least square approximation. The SVD method doesn't quite compile (perhaps a typo in the example?) but the QR method worked well. So that's cool.

Given "data", a flattened array of pixel values 0/1 of dimension W * W, this code finds a matrix "m" containing the coefficients for the polynomial surface.

Next, I wrote a shader to display the surface given all 81 coefficients. (Yeah, it's pretty crazy).

Moreover, we inadvertently discovered a new method to interpolate between images: instead of interpolating the value of each pixel, we can interpolate the coefficient of the polynomials!

I computed the polynomial for each of the 21 Media Lab group logos + the Media Lab logo, and you can interpolate between any two of them through interpolating polynomial coefficients.