-1

I am doing a singular value decomposition of a greyscale image (read: big matrix) via numpy.linalg. Thing is, when I try to reconstruct the image from the decomposition I get a matrix of absurd values which cannot correspond to the pixels of the source image. Google doesn't give any answers as to what may be the problem and the code itself is way too simple for any mistakes to be made writing it.

The code and the image are as follows: image

import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import svd
from skimage.io import imread

img = imread(r'chain_small.JPG')
img = img.mean(axis=2)

U, S, VT = svd(img, full_matrices = False)

print(U @ np.diag(S) @ VT)

Here are the three outputs at three consecutive runs:

1: [[ 159014.89394671   39487.99098681   -9874.57609635 ...  -71737.85515213
  -128874.27699761   -3386.92849387]
 [ 203461.80899605   75002.13673717  126260.20741683 ...  158491.47548145
   157982.81363556  137954.09746291]
 [ 293467.76004879  -90981.28699457  -28559.21588133 ...  -72627.56336045
   -57172.40331332  -25768.69870728]
 ...
 [  29835.15592901   20815.81893506   -5236.87874038 ...  -19871.19469429
    29210.33682942   15554.95475245]
 [-104265.48680219  -17915.06944458    5938.52625767 ...   15497.27973241
    -8433.1313703    -9386.58231012]
 [ -70906.08392422  -15034.78054189    5742.7956658  ...   -1110.09557333
   -13252.87395276  -19236.4320781 ]]

2: [[-1.40339175e+05  1.84856533e+03 -1.70047632e+04 ...  4.07286857e+04
   8.27626972e+04  8.77105170e+02]
 [ 9.23440572e+05  1.61186883e+03  7.16404929e+04 ... -1.41672490e+05
   3.35168593e+05 -4.94743352e+03]
 [ 2.29648889e+05  9.32584084e+04 -3.03421382e+04 ...  3.09541683e+05
   2.39098724e+04 -7.89459012e+03]
 ...
 [ 5.31486822e+04  2.55513683e+04 -5.36843672e+03 ... -4.51257913e+03
   1.40963817e+04  1.04165914e+04]
 [-5.33436869e+04 -1.47708525e+04  1.70761244e+04 ... -1.82554352e+04
  -4.00233256e+04 -5.65965532e+04]
 [-3.01219658e+04 -1.66921447e+04  1.73071052e+04 ... -3.37167442e+04
  -4.62436316e+04 -6.80044548e+04]]

3: [[ 170683.38688212   28358.22126302  -23356.74822456 ...   88247.14000447
   -25268.54838009  -59126.11297314]
 [ 548066.38756864  -65846.99861054   64624.47642912 ...   31491.42901022
   -24204.73172982   19110.93935304]
 [ 667140.05662532   39098.12090819   37824.13446388 ... -119091.87577846
   249393.0348422    54838.74200844]
 ...
 [  11244.29360774    5379.79454404   -8576.07360678 ...    4032.56408047
    28892.94580266    7560.6402372 ]
 [ -80855.17281102  -10303.81468742   14164.90681016 ...   17284.69756787
   -10799.40786149  -33744.18482325]
 [ -48865.0672561   -10991.37326434   13474.09716008 ...    1738.80247443
   -17218.39061216  -43756.29097246]]

Apologies for the sheer stupidity of the question and formatting but I am genuinely lost on this one.

2 Answers2

0

What is 'a' variable in your code? I can't test code because 'a' isn't defined. Suspect at this place you need img.

U, S, VT = svd(img, full_matrices = False)

instead

U, S, VT = svd(a, full_matrices = False)
  • You're right, there should be 'img' instead of 'a'. It was defined as a 3x4 numpy array for testing purposes and the program reconstructed it just fine, that's how I know there is no issue with the code itself. I suppose I forgot to change it when copying and pasting the important bits of code here, sorry about that. I didn't change anything else though so this would work with 'a' swapped for 'img'. – Александр Асратян Jun 04 '23 at 09:28
0

The issue is "solved". Still not sure what caused it, but I ran the program on another machine and it worked just fine. Probably an issue with Python or hardware.