I have two (black and white) images of identical size - let's say 128x128 pixels. I'm interested in expressing Im2 in terms of components of Im1 such that as opposed to specifying Im2, I can specify some values (less than number of pixels in Im2, otherwise what's the point) which, together with the knowledge of Im1 would allow me to construct an approximation of Im2. Let me demonstrate by giving you my first idea (which, spoiler alert, didn't work). In pythonish pseudocode:

# Singular Value Decomposition of Im1 and Im2
U1, S1, V1 = svd(Im1)
# Function to minimise
def fit_function(S):
    Im_fit = U1*diag(S)*V1 
    diff = Im2-Im_fit
    return sqrt(mean(diff^2)) # root-mean-square difference
# Find S which fits Im_fit to Im2
Sfit = myLittleMagicSolver(fit_fun)

In other words; use singular value decomposition on Im1 and Im2. Then find numbers other than singular values of Im1 which together with U1 and V1 matrices approximate Im2. This way I could specify only 128 values - the main diagonal of the fitted S matrix.

This, however, does not work. My fitted images are nowhere close to their target of Im2. My question is then, is that a problem someone has tackled already? I am not fussed about the SVD/PCA approach. I just want to be able to express Im2 in terms of Im1.

My intended use is to train a neural network to recognize patterns in my particular dataset where one image is an input to the network, and multiple images are the output. This, of course, makes for a pretty hefty neural net. I, therefore, want to teach the network to recognize the components of the output images in terms of the input instead.

  • $\begingroup$ This is not my area, but I'd say that the answer will depend on the correlation between the two images. Could you explain if there is any? $\endgroup$
    – MBaz
    Commented Oct 12, 2017 at 0:10
  • $\begingroup$ @MBaz You can assume none or, at leat, inconsistent. Imagine grayscale of 'lena' (a lady in a hat) and 'mandrill' (a close up face of a baboon). $\endgroup$ Commented Oct 12, 2017 at 0:13
  • $\begingroup$ With no correlation, I don't see a way -- but if you succeed you'll have an awesome image compressor. $\endgroup$
    – MBaz
    Commented Oct 12, 2017 at 0:23
  • $\begingroup$ I think you need to consider the mutual information between the 2 images, as a source and receiver. $\endgroup$
    – user28715
    Commented Oct 12, 2017 at 16:22
  • $\begingroup$ @StanleyPawlukiewicz I am not entirely sure what you mean. Could you expand on your suggestion? $\endgroup$ Commented Oct 12, 2017 at 16:31

1 Answer 1


I found an answer which is good-enough for me. As @Stanley Pawlukiewicz has pointed out in the comments, this is hard to do for a general case when there is little correlation between the images. I, however, want to work with real images of actual things in the real world. This means there will be a lot of low frequency components (that's why jpeg compression works). If I now formulate the problem as: $$ I = U_{s} S V_{s}' $$ where $I$ is the matrix representing the image, $U_{s}$ and $V_{s}$ are the matrices of the svd decomposition of the source image, and we are trying to find $S$. Then, the inverse problem (which is the one we actually want to solve) is: $$ S = U_{s}^{-1}I(V_{s}')^{-1} $$ This is the easy part (using Matlab, python or otherwise).

I then took a good look at how are the solutions ($S$) to this problem are distributed, and they tend to cluster around the left and upper edge of the S matrix. Therefore, I can approximate S by the subset of S centered around these values. I (slightly arbitrarily) have chosen to use a quarter of a superellipse to set the cutoff value with a nice constant to play with. I can get S down to 10% of its elements and retain most of the broad features of the image, which is all I wanted.

I've put my code up online so you can check it out. Hope this helps someone. :)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.