Here is a picture of a turkey that we liked.


In [1]:
using PyPlot
TurkeyPic = download("http://www.unhinderedliving.com/turkey.png", "turkey.png");
img=imread(TurkeyPic)
imshow(img)


PyPlot not found
while loading In[1], in expression starting on line 1
 in require at loading.jl:39

Color images have three planes, for the red, green and blue colors respectively.


In [2]:
imshow(hcat([img[:,:,i] for i=1:3]...), cmap="hot")


Out[2]:
PyObject <matplotlib.image.AxesImage object at 0x5d9a510>

We can calculate the SVD of each color plane, filter out all but the most important few singular values and vectors, and constructe the filtered image.


In [3]:
@time svds=map(i->svd(img[:,:,i]), 1:3);
img_filtered(k)=cat(3, [svds[i][1][:,1:k].*svds[i][2][1:k]'*svds[i][3][:,1:k]' for i=1:3]...)
imshow(img_filtered(30))


elapsed time: 0.734046496 seconds (26844060 bytes allocated)
Out[3]:
PyObject <matplotlib.image.AxesImage object at 0x34402d0>

Here is a video showing how the filtered image varies with the number of singular values and vectors retained.


In [20]:
#########################################
# Generate video                        #
# WARNING: This takes some time to run! #
#########################################
using PyCall
@pyimport matplotlib.animation as anim
if false #Change this to actually generate the video
video = anim.FFMpegWriter(extra_args=["-vcodec", "libx264", "-pix_fmt", "yuv420p"])
video[:setup](gcf(), "turkey_svd.mp4")
nsvds=length(svds[1][2]); scale=size(img,2)/nsvds
@time for k=1:nsvds #Compute every possible SVD filtering
    progress_bar = cat(3, [ones(10)*[zeros(int(k*scale)); ones(int((nsvds-k)*scale))]' for i=1:3]...)
    imshow([img_filtered(k), progress_bar])
    video[:grab_frame]()
end
video[:finish]()
end

In [14]:
;ls -l *.mp4
embed_video(filename)=display("text/html", string("""<video autoplay controls><source src="data:video/x-m4v;base64,""",
                            base64(open(readbytes,filename)),"""" type="video/mp4"></video>"""))
embed_video("turkey_svd.mp4")


bash: no job control in this shell
-rw-rw-r-- 1 ec2-user ec2-user   172226 Nov  8 23:27 blocksvd.mp4
-rw-rw-r-- 1 ec2-user ec2-user 17802402 Nov  9 00:37 turkey_svd.mp4
-rw-rw-r-- 1 ec2-user ec2-user    52700 Nov  8 21:47 writer_test.mp4

The black progress bar on the bottom shows the fraction of singular values and vectors that were kept after filtration.