Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Last active April 17, 2016 13:57
Show Gist options
  • Save unixpickle/4cf0dad14656be183f3a1ba4d5136ec8 to your computer and use it in GitHub Desktop.
Save unixpickle/4cf0dad14656be183f3a1ba4d5136ec8 to your computer and use it in GitHub Desktop.
Image Unblur

This example shows how we might blur and unblur 4x4 images. Let b0 through b15 be pixels in a blurred image. For example, the pixel at coordinates (2,3) is b14, since 14 is 2+3*4. Likewise, let the pixels of an unblurred image be x0 through x15.

Whenever we blur an image x into a blurred image b, we average each pixel of x with the pixels around it, giving the corresponding pixel of b. For example:

b5 = 1/5*x1 + 1/5*x4 + 1/5*x5 + 1/5*x6 + 1/5*x9

The output pixel b5 is at (1,1), so we compute it by averaging the pixels at (1,0), (1,2), (2,1), (0,1), and (1,1). This formula is similar for all the other pixels. Thus, we get:

b0  = 1/5*x0 + 1/5*x1 +   0*x2 + 1/5*x3 + 1/5*x4 +   0*x5 +   0*x6 +   0*x7 +   0*x8 +   0*x9 +   0*x10 +   0*x11 + 1/5*x12 +   0*x13 +   0*x14 +   0*x15
b1  = 1/5*x0 + 1/5*x1 + 1/5*x2 +   0*x3 +   0*x4 + 1/5*x5 +   0*x6 +   0*x7 +   0*x8 +   0*x9 +   0*x10 +   0*x11 +   0*x12 + 1/5*x13 +   0*x14 +   0*x15
b2  = 0*x0 + 1/5*x1 + 1/5*x2 + 1/5*x3 +   0*x4 +   0*x5 + 1/5*x6 +   0*x7 +   0*x8 +   0*x9 +   0*x10 +   0*x11 +   0*x12 +   0*x13 + 1/5*x14 +   0*x15
b3  = 1/5*x0 +   0*x1 + 1/5*x2 + 1/5*x3 +   0*x4 +   0*x5 +   0*x6 + 1/5*x7 +   0*x8 +   0*x9 +   0*x10 +   0*x11 +   0*x12 +   0*x13 +   0*x14 + 1/5*x15
b4  = 1/5*x0 +   0*x1 +   0*x2 +   0*x3 + 1/5*x4 + 1/5*x5 +   0*x6 + 1/5*x7 + 1/5*x8 +   0*x9 +   0*x10 +   0*x11 +   0*x12 +   0*x13 +   0*x14 +   0*x15
b5  = 0*x0 + 1/5*x1 +   0*x2 +   0*x3 + 1/5*x4 + 1/5*x5 + 1/5*x6 +   0*x7 +   0*x8 + 1/5*x9 +   0*x10 +   0*x11 +   0*x12 +   0*x13 +   0*x14 +   0*x15
b6  = 0*x0 +   0*x1 + 1/5*x2 +   0*x3 +   0*x4 + 1/5*x5 + 1/5*x6 + 1/5*x7 +   0*x8 +   0*x9 + 1/5*x10 +   0*x11 +   0*x12 +   0*x13 +   0*x14 +   0*x15
b7  = 0*x0 +   0*x1 +   0*x2 + 1/5*x3 + 1/5*x4 +   0*x5 + 1/5*x6 + 1/5*x7 +   0*x8 +   0*x9 +   0*x10 + 1/5*x11 +   0*x12 +   0*x13 +   0*x14 +   0*x15
b8  = 0*x0 +   0*x1 +   0*x2 +   0*x3 + 1/5*x4 +   0*x5 +   0*x6 +   0*x7 + 1/5*x8 + 1/5*x9 +   0*x10 + 1/5*x11 + 1/5*x12 +   0*x13 +   0*x14 +   0*x15
b9  = 0*x0 +   0*x1 +   0*x2 +   0*x3 +   0*x4 + 1/5*x5 +   0*x6 +   0*x7 + 1/5*x8 + 1/5*x9 + 1/5*x10 +   0*x11 +   0*x12 + 1/5*x13 +   0*x14 +   0*x15
b10 = 0*x0 +   0*x1 +   0*x2 +   0*x3 +   0*x4 +   0*x5 + 1/5*x6 +   0*x7 +   0*x8 + 1/5*x9 + 1/5*x10 + 1/5*x11 +   0*x12 +   0*x13 + 1/5*x14 +   0*x15
b11 = 0*x0 +   0*x1 +   0*x2 +   0*x3 +   0*x4 +   0*x5 +   0*x6 + 1/5*x7 + 1/5*x8 +   0*x9 + 1/5*x10 + 1/5*x11 +   0*x12 +   0*x13 +   0*x14 + 1/5*x15
b12 = 1/5*x0 +   0*x1 +   0*x2 +   0*x3 +   0*x4 +   0*x5 +   0*x6 +   0*x7 + 1/5*x8 +   0*x9 +   0*x10 +   0*x11 + 1/5*x12 + 1/5*x13 +   0*x14 + 1/5*x15
b13 = 0*x0 + 1/5*x1 +   0*x2 +   0*x3 +   0*x4 +   0*x5 +   0*x6 +   0*x7 +   0*x8 + 1/5*x9 +   0*x10 +   0*x11 + 1/5*x12 + 1/5*x13 + 1/5*x14 +   0*x15
b14 = 0*x0 +   0*x1 + 1/5*x2 +   0*x3 +   0*x4 +   0*x5 +   0*x6 +   0*x7 +   0*x8 +   0*x9 + 1/5*x10 +   0*x11 +   0*x12 + 1/5*x13 + 1/5*x14 + 1/5*x15
b15 = 0*x0 +   0*x1 +   0*x2 + 1/5*x3 +   0*x4 +   0*x5 +   0*x6 +   0*x7 +   0*x8 +   0*x9 +   0*x10 + 1/5*x11 + 1/5*x12 +   0*x13 + 1/5*x14 + 1/5*x15

This is a system of equations which always has one solution. For instance, suppose we have an image b like,

b0  = 0.140440
b1  = 0.013673
b2  = 0.745688
b3  = 0.159971
b4  = 0.190304
b5  = 0.960329
b6  = 0.034791
b7  = 0.370286
b8  = 0.226288
b9  = 0.374386
b10 = 0.943962
b11 = 0.315647
b12 = 0.438816
b13 = 0.946629
b14 = 0.431666
b15 = 0.147535

Then we can solve for the original x's using elimination, substitution, or some automated tool:

x0  =  0.42494
x1  =  1.43648
x2  = -1.22967
x3  = -0.43912
x4  =  0.29136
x5  = -1.17935
x6  =  2.26087
x7  =  0.64682
x8  =  0.76775
x9  =  1.99229
x10 = -0.32472
x11 = -0.90850
x12 = -1.01146
x13 =  0.61596
x14 =  1.69987
x15 =  1.39688

The above values of x are the only ones which satisfy the system of equations, since the system is fully determined. Notice how these "pixels" do not comprise a valid image, since some of them are greater than 1 and many are less than 0. Thus, despite the fact that our b's were inside the range 0-1, the corresponding x's are not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment