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)
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]:
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))
Out[3]:
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")