using Images, FileIO, LinearAlgebra, Plots # Specify the path to your local image file img_path = "C:\\Users\\User\\Downloads\\SVD_PAPER\\cats_medium.jpg" img = load(img_path) img_gray = float.(Gray.(img)) function rank_approx(F::SVD, k) U, S, V = F # Ensure k does not exceed the dimensions k = min(k, size(U, 2), size(V, 2)) # Approximation using the first k singular values M = U[:, 1:k] * Diagonal(S[1:k]) * V[:, 1:k]' clamp01!(M) return M end # Compute SVD of grayscale image svdfactor = svd(img_gray) # Create compressed images for k = 10, 50, 100 ks = [10, 50, 100] imgs = map(ks) do k Gray.(rank_approx(svdfactor, k)) end plot(mosaicview(img_gray, imgs...; nrow=1, npad=10))