In the following code I am using Python's pillow/PIL library. I have successfully been able to read in an image file and store it as a matrix, access the red, blue, green channel which is an unsigned integer unit8 and convert each channel to float64.
However now I am looking to see if I did the next part correct. In my code below in addition to the above details I have applied the svd from numpy.linalg to each channel. Have I done this correctly? Also how might I use it to compute the rank-k approximation? Any help is greatly appreciated as I am not sure how I would compute the rank-k approximation if I implemented the svd from numpy.linalg to each channel correctly
Here is my code:
import numpy as np
from PIL import Image
img = Image.open('house.jpg')
image = np.array(img)
arr[20,30]
red = np.float64(image[:,:, 0])
green = np.float64(image[:,:,1])
blue = np.float64(image[:,:,2])
Applying it to the red channel:
P, D, Q = np.linalg.svd(red, full_matrices=False)
red_a = np.matmul(np.matmul(P, np.diag(D)), Q)
print(np.std(red), np.std(red_a), np.std(red - red_a))
resulting in:
58.34304856581507 58.34304856581494 9.996264017664882e-13
Applying it to the green channel:
P, D, Q = np.linalg.svd(green, full_matrices=False)
green_a = np.matmul(np.matmul(P, np.diag(D)), Q)
print(np.std(green), np.std(green_a), np.std(green - green_a))
resulting in:
51.90828777139797 51.90828777139794 2.5106850520519223e-13
finally applying it to the blue channel:
P, D, Q = np.linalg.svd(blue, full_matrices=False)
blue_a = np.matmul(np.matmul(P, np.diag(D)), Q)
print(np.std(blue), np.std(blue_a), np.std(blue - blue_a))
resulting in:
77.28319292372947 77.28319292372947 1.4848230006060346e-12