Files
LAFF/Programming/Week11/CompressPicture_Answer.m
Julien Lengrand-Lambert d4dce71599 Create repo
2018-05-02 09:13:08 +02:00

57 lines
1.4 KiB
Matlab
Executable File

% Close all figures
close all
% Read the picture in building.png into matrix B
[ B, map ] = imread( 'building.png' );
% Show the picture in a figure
imshow( B );
% Choose the (approximate) number of columns you want to use for the
% aproximation.
k = 30;
% Extract the number of rows and columns in B
[ m, n ] = size( B )
% B starts as an integer array. To do the computations, we need to
% make it into floating point numbers.
B = double( B );
% Pick A as the matrix consisting of about k equally spaced columns of
% B.
stride = round( n / k );
A = B( :, 1:stride:n );
% Replace the comments below with their respective operations from the notebook
% C = A^T A
C = A' * A;
% V = A^T B
V = A' * B;
% Overwrite C with its LU factorization
C = LU_unb_var5( C );
% Extract the unit lower triangular matrix L and upper triangular matrix U.
L = tril( C, -1 ) + eye( size( C ) );
U = triu( C );
% Solve L(UX) = V, overwriting V with X
V = L \ V;
V = U \ V;
% Create a new figure in which you show the approximated picture. Notice
% that A and V are matrices of doubles, which much be converted to unsigned
% 8 byte integers for imshow to work.
figure
imshow( uint8( A * V ) );
% The two figures may be on top of each other. You may have to move the
% second one next to the first one.
% Play around by changing the number of columns you use for the
% approximation!