To get credit for the lab, please show the completed work to a lab instructor.
- Understand the concept of SVD and its application to image processing.
- Implement an interactive web application that allows users to compress an image using SVD and adjust the number of singular values retained (rank).
- Explore the effect of SVD rank on the compressed image.
Similar to Lab 2, you can use the Makefile
to install all dependencies. In your terminal, simply run:
make install
This will automatically install the necessary packages listed in requirements.txt
, including:
- Flask
- Numpy
- Scikit-learn
- Pillow
An image is represented as a matrix where each element corresponds to the intensity of a pixel. In the case of an RGB image, there are three such matrices, one for each color channel (Red, Green, Blue).
The core idea of using SVD for image compression is that we can decompose the image matrix into three other matrices, and by retaining only the most important singular values (those that contribute the most to the image's structure), we can compress the image by reducing its data without significantly affecting its appearance.
Your task is to complete the following functions in the provided script:
- (TODO)
load_image(image_path)
: This function should load the image from a file and return it as a NumPy array. You will need to use thePillow
library for this task. You can reuse the code from Lab 2. image_compression_svd(image_np, rank)
: This function compresses an image by applying SVD separately to each color channel. It has been inplemented for you and will callcompress_channel_svd(channel_matrix, rank)
for single channel processing.- (TODO)
compress_channel_svd(channel_matrix, rank)
: This function applies SVD to compress a single image channel. It should return a compressed single channel image. (You may use NumPy functions for SVD computation.) save_result(original_image_np, quantized_image_np, output_path)
This function has been provided for you. Once the original and compressed images are prepared, the function will save them side by side in a single image file.
- You can test the code locally by running the Python script directly and specifying an image path. Modify
__main__
inimage_compression_impl.py
to load the image, apply compression, and save the results. Make sure you provide paths for both input and output images. - Choose your favorite image as input and experiment with different values of
rank
and observe the effect on image quality. We also have a sample image 'examples/example.jpg' for you to use.
Example Workflow
-
Select your favorite image file (e.g.,
'favorite_image.png'
) and place it in your working directory. -
In
__main__
:- Set
image_path
to your selected image file. - Set
output_path
to where you want to save the side-by-side result (e.g.,'compressed_image.png'
). - Choose an appropriate number of sigular values retained (
rank
), e.g., 8.
- Set
-
Run the script in your terminal:
python image_compression_impl.py
-
Check the output image to see the original and compressed versions side by side.
Once the environment is set up, you can start the Flask application by running:
make run
This will start the Flask server and make the interactive application available locally at http://127.0.0.1:3000
.
The interactive module allows you to upload an image and select the SVD rank for compression through a web interface.
- Open your browser and go to
http://127.0.0.1:3000
. - Upload your favorite image using the provided interface.
- Enter SVD rank for compression.
- Click the "Submit" button to perform the image compression.
- The original and compressed images will be displayed side by side on the page.
- Ensure that the image loaded is in RGB format.
- Test with your favorite image and experiment with different values for
rank
to observe the impact of compression. - If you encounter issues loading or saving images, check the installation of
Pillow
and the file format compatibility. - If you encounter issues running the Flask server, ensure that
flask
is correctly installed and check the command line output for error messages.