// Instructions: // Try different values of p. import edu.gwu.lintool.*; public class ImageExample { public static void main (String[] argv) { // Extract a greyscale image into a matrix A: ImageTool imTool = new ImageTool (); int[][] pixels = imTool.imageFileToGreyPixels ("Sylvester.png"); double[][] A = convertToDouble (pixels); // Get the SVD. LinResult L = LinToolLibrary.computeSVDShortForm (A); int r = L.rank; // We'll now try different outerproducts, up through the rank. // Vary p: p=1, 5, 10, 50, 100, r int p = 50; double[][] B = outerProduct (p, L.U, L.Sigma, L.V); // Convert matrix back to image: int[][] pixels2 = convertToInt (B); imTool.writeToFile (pixels2, "testimage-" + p + ".png"); // Examine actual image in the same directory. } static double[][] outerProduct (int p, double[][] U, double[][] Sigma, double[][] V) { int m = U.length; int n = V.length; double[][] S = new double [m][n]; for (int k=0; k 255) A[i][j] = 255; } } return A; } }